<!--安全分级-->
<template>
  <div class="app-container class-ified-page">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="分级名称" prop="classifiedName">
        <el-input
          v-model="queryParams.classifiedName"
          placeholder="请输入分级名称"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item style="float: right;">
        <el-col :span="1.5">
          <p class="add-button-one-120"
             style="margin-right:20px;"
             @click="handleAdd"
             v-hasPermi="['laboratory:classified:add']"
          ><i class="el-icon-plus"></i>新增</p>
        </el-col>
      </el-form-item>
      <el-form-item>
        <p class="inquire-button-one" @click="handleQuery">查询</p>
        <p class="reset-button-one" @click="resetQuery">重置</p>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" border :data="classifiedList" @selection-change="handleSelectionChange">
      <!--<el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="自增id" align="left" prop="id" v-if="false"/>
      <el-table-column label="部门名称" align="left" prop="deptName" v-if="false"/>
      <el-table-column label="部门id" align="left" prop="deptId" v-if="false"/>
      <el-table-column label="创建人" align="left" prop="userId" v-if="false"/>
      <el-table-column label="分级名称" align="left" prop="classifiedName" />
      <el-table-column label="颜色" align="left" prop="classifiedColor" >
        <template slot-scope="scope">
          <p :style="'width:40px;height:23px;margin:0;backAnimation:'+scope.row.classifiedColor"></p>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="left" prop="remark" />
      <el-table-column label="分级名称" align="left" prop="createTime" />
      <el-table-column label="操作" align="left" class-name="small-padding fixed-width" width="160" v-if="tableButtonType">
        <template slot-scope="scope">
          <div class="button-box">
            <p class="table-min-button"
               @click="handleUpdate(scope.row)"
               v-hasPermiAnd="['laboratory:classified:query','laboratory:classified:edit']"
            >修改</p>
            <p class="table-min-button"
               @click="handleDelete(scope.row)"
               v-hasPermi="['laboratory:classified:remove']"
            >删除</p>
          </div>
        </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="80px">
        <el-form-item label="分级名称" prop="classifiedName">
          <el-input v-model="form.classifiedName" maxlength="4" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="颜色" prop="classifiedColor">
          <el-color-picker v-model="form.classifiedColor"></el-color-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" 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 {
        addClassified,
        delClassified,
        getClassified,
        listClassified,
        updateClassified
    } from "@/api/laboratory/classified";

    export default {
  name: "Classified",
  data() {
    return {
      tableButtonType:this.hasPermiDom(['laboratory:classified:query','laboratory:classified:edit','laboratory:classified:remove']),
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 安全分级表格数据
      classifiedList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptName: null,
        deptId: null,
        userId: null,
        classifiedName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        classifiedName: [
          { required: true, message: "请输入分级名称", trigger: "blur" },
          { required: true, message: "请输入分级名称", validator: this.spaceJudgment, trigger: "blur" }
        ],
        classifiedColor: [
          { required: true, message: "请选择颜色", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询安全分级列表 */
    getList() {
      this.loading = true;
      listClassified(this.queryParams).then( response => {
        this.classifiedList =  response.rows;
        this.total =  response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptName: null,
        deptId: null,
        createTime: null,
        userId: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remark: null,
        classifiedName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.resetForm("queryForm");
      this.$set(this,'queryParams',{
        pageNum: 1,
        pageSize: 10,
        classifiedName:"",
      });
      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
      getClassified(id).then( response => {
        this.form =  response.data;
        this.open = true;
        this.title = "修改安全分级";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateClassified(this.form).then( response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClassified(this.form).then( response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除安全分级编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delClassified(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('laboratory/classified/export', {
        ...this.queryParams
      }, `laboratory_classified.xlsx`)
    }
  }
};
</script>


<style scoped lang="scss">
  .class-ified-page {
    display: flex!important;
    flex-direction: column;
    box-shadow: 0 0 8px 2px rgba(0, 0, 0, 0.1);
    padding:20px!important;
    .button-box{
      display: flex;
      width:190px;
      margin:0 auto;
    }
  }
</style>
