<!--安全分类-->
<template>
  <div class="class-type-page">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="关键字" prop="typeName">
        <el-input
          v-model="queryParams.typeName"
          placeholder="请输入分类名称"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item style="float: right">
        <el-col :span="1.5">
          <p class="add-button-one-120"
             @click="handleAdd"
             v-hasPermi="['laboratory:classtype: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="classtypeList" @selection-change="handleSelectionChange">
      <!--<el-table-column label="自增id" align="left" prop="id" v-if="false"/>-->
      <el-table-column label="安全分类" align="left" prop="typeName" />
      <!--<el-table-column label="备注" align="left" prop="remark" />-->
      <el-table-column label="创建人" align="left" prop="createBy" />
      <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"
               v-hasPermiAnd="['laboratory:classtype:query','laboratory:classtype:edit']"
               @click="handleUpdate(scope.row)"
            >修改</p>
            <p class="table-min-button"
               v-hasPermi="['laboratory:classtype:remove']"
               @click="handleDelete(scope.row)"
            >删除</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="110px">
        <el-form-item label="安全类别名称" prop="typeName">
          <el-input v-model="form.typeName" maxlength="10" placeholder="请输入安全类别名称" />
        </el-form-item>
        <!--<el-form-item label="备注" prop="remark">-->
          <!--<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 { listClasstype, getClasstype, delClasstype, addClasstype, updateClasstype } from "@/api/laboratory/classtype";

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

<style scoped lang="scss">
  .class-type-page {
    flex:1;
    display: flex!important;
    flex-direction: column;
    padding:20px!important;
    .button-box{
      display: flex;
      width:190px;
      margin:0 auto;
    }
  }
</style>
