
<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <!-- <el-form-item label="父级分类id" prop="parentId">
          <el-input
            v-model="queryParams.parentId"
            placeholder="请输入父级分类id"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item> -->
      <el-form-item label="培训分类名称" prop="trainingCategoryName">
        <el-input
          v-model="queryParams.trainingCategoryName"
          placeholder="请输入培训分类名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="培训分类编码" prop="trainingCategoryCode">
        <el-input
          v-model="queryParams.trainingCategoryCode"
          placeholder="请输入培训分类编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="培训分类排序" prop="trainingCategorySort">
        <el-input
          oninput="value=value.replace(/[^\d]/g,'')"
          maxlength="5"
          v-model="queryParams.trainingCategorySort"
          placeholder="请输入培训分类排序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:trainingCategory:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:trainingCategory:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:trainingCategory:remove']"
          >删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:trainingCategory:export']"
          >导出</el-button>
        </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="trainingCategoryList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键id" align="center" prop="id" />
        <el-table-column label="父级分类id" align="center" prop="parentId" /> -->
      <el-table-column
        label="培训分类名称"
        align="center"
        prop="trainingCategoryName"
      />
      <el-table-column
        label="培训分类编码"
        align="center"
        prop="trainingCategoryCode"
      />
      <!-- <el-table-column label="培训分类图片路径" align="center" prop="trainingCategoryPictureUrl" /> -->
      <el-table-column
        label="培训分类排序"
        align="center"
        prop="trainingCategorySort"
      />
      <!-- <el-table-column label="分类概述" align="center" prop="remark" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:trainingCategory:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:trainingCategory:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学习培训分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="110px"
        :inline="true"
      >
        <!-- <el-form-item label="父级分类id" prop="parentId">
            <el-input v-model="form.parentId" placeholder="请输入父级分类id" />
          </el-form-item> -->
        <el-form-item label="培训分类名称" prop="trainingCategoryName">
          <el-input
            v-model="form.trainingCategoryName"
            maxlength="50"
            placeholder="请输入培训分类名称"
          />
        </el-form-item>
        <!-- <el-form-item label="培训分类编码" prop="trainingCategoryCode">
            <el-input v-model="form.trainingCategoryCode" placeholder="请输入培训分类编码" />
          </el-form-item> -->
        <el-form-item label="培训分类图片" prop="trainingCategoryPictureUrl">
          <stationAcatar
            style="width: 208px"
            @imgUrl="imgUrl"
            :img="form.trainingCategoryPictureUrl"
            :type="'trainingCategoryPictureUrl'"
          />
          <!-- <el-input v-model="form.trainingCategoryPictureUrl" type="textarea" placeholder="请输入内容" /> -->
        </el-form-item>
        <el-form-item label="培训分类排序" prop="trainingCategorySort">
          <el-input
            oninput="value=value.replace(/[^\d]/g,'')"
            v-model.number="form.trainingCategorySort"
            maxlength="5"
            placeholder="请输入培训分类排序"
          />
        </el-form-item>
        <el-form-item label="分类概述" prop="remark">
          <el-input
            type="textarea"
            :rows="5"
            style="width: 340px"
            placeholder="请输入分类概述"
            v-model="form.remark"
          ></el-input>
          <!-- <el-input v-model="form.remark" placeholder="请输入分类概述" /> -->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
import {
  listTrainingCategory,
  getTrainingCategory,
  delTrainingCategory,
  addTrainingCategory,
  updateTrainingCategory,
} from "@/api/system/trainingCategory";
import stationAcatar from "../stationAvatar/index.vue";
import { updatePicture } from "@/api/system/station";

export default {
  components: { stationAcatar },
  name: "TrainingCategory",
  data() {
    return {
      // 遮罩层
      loading: true,
      imgsurl: { pictureUrlList: [] },

      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学习培训分类表格数据
      trainingCategoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parentId: null,
        trainingCategoryName: null,
        trainingCategoryCode: null,
        trainingCategoryPictureUrl: null,
        trainingCategorySort: null,
      },
      imgone: "",
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        trainingCategoryName: [
          {
            required: true,
            message: "请输入培训分类名称",
            trigger: "blur",
          },
        ],
        remark: [
          {
            required: true,
            message: "请输入分类概述",
            trigger: "blur",
          },
        ],
        trainingCategoryPictureUrl: [
          {
            required: true,
            message: "请选择培训分类图片",
            trigger: "blur",
          },
        ],
        trainingCategorySort: [
          {
            required: true,
            message: "请选择培训分类排序",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    imgUrl(imgUrl) {
      this.form.trainingCategoryPictureUrl = imgUrl;
    },
    /** 查询学习培训分类列表 */
    getList() {
      this.loading = true;
      listTrainingCategory(this.queryParams).then((response) => {
        this.trainingCategoryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      var obj = { pictureUrlList: [] };
      if (this.imgone != this.form.trainingCategoryPictureUrl) {
        if (this.form.trainingCategoryPictureUrl) {
          obj.pictureUrlList.push(this.form.trainingCategoryPictureUrl);
        }
      }
      if (obj.pictureUrlList.length > 0) {
        updatePicture(obj).then((res) => {});
      }
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parentId: null,
        trainingCategoryName: null,
        trainingCategoryCode: null,
        trainingCategoryPictureUrl: null,
        trainingCategorySort: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学习培训分类";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getTrainingCategory(id).then((response) => {
        this.form = response.data;
        this.imgone = this.form.trainingCategoryPictureUrl;
        this.open = true;
        this.title = "修改学习培训分类";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateTrainingCategory(this.form).then((response) => {
              if (response.code == 200) {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              }
            });
          } else {
            addTrainingCategory(this.form).then((response) => {
              if (response.code == 200) {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除学习培训分类编号为"' + ids + '"的数据项？')
        .then(function () {
          return delTrainingCategory(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/trainingCategory/export",
        {
          ...this.queryParams,
        },
        `trainingCategory_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
  