<template>
    <div class="subjectClass">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="" prop="moldName">
            <el-input
              v-model="queryParams.moldName"
              placeholder="请输入类型名称"
              clearable
              size="small"
            />
          </el-form-item>
          <p class="page-inquire-common-style-button" @click="handleQuery">查询</p>
          <p class="page-reset-common-style-button" @click="resetQuery">重置</p>
          <p class="page-submit-common-style-button"
             style="float: right;"
             @click="handleAdd"
             v-hasPermiRouter="['lab:classMold:add']"
          >新增</p>
        </el-form>
        <el-table class="table-box" v-loading="loading" border :data="classtypeList" @selection-change="handleSelectionChange">
          <el-table-column label="实验室类型" align="left" prop="moldName" />
          <el-table-column label="创建人" align="left" prop="createName"/>
          <el-table-column label="创建时间" align="left" prop="createTime">
            <template slot-scope="scope">{{ parseTime(scope.row.createTime,"{y}-{m}-{d} {h}:{i}") }}</template>
          </el-table-column>
          <el-table-column label="操作" align="left" class-name="small-padding fixed-width" width="160" v-if="tableButtonType">
            <template slot-scope="scope">
              <div class="table-button-box">
                <p class="table-button-null"></p>
                <p class="table-button-p"
                   v-hasPermiRouter="['lab:classMold:edit']"
                   @click="handleUpdate(scope.row)"
                >修改</p>
                <p class="table-button-p"
                   v-hasPermiRouter="['lab:classMold:del']"
                   @click="handleDelete(scope.row)"
                >删除</p>
                <p class="table-button-null"></p>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.page"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
        <!-- 添加或修改安全分级类型对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body
                   :close-on-click-modal="false">
          <el-form ref="form" :model="form" :rules="rules" label-width="110px">
            <el-form-item label="实验室类型" prop="moldName">
              <el-input v-model="form.moldName" maxlength="10" placeholder="请输入实验室类型名称" />
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer dialog-footer-box">
            <p class="dialog-footer-button-null"></p>
            <p class="dialog-footer-button-info" @click="cancel">取消</p>
            <p class="dialog-footer-button-primary" @click="submitForm">提交</p>
            <p class="dialog-footer-button-null"></p>
          </div>
        </el-dialog>
    </div>
</template>
<script>
    import { laboratoryClassMoldList,
      laboratoryClassMoldAdd,
      laboratoryClassMoldUpdate,
      laboratoryClassMoldDelete,
      laboratoryClassMoldDetail
    } from "@/api/integratedManagement/index";
    export default {
      name: "subjectClass",
      data() {
        return {
          tableButtonType:this.hasPermiDom(['lab:classMold:edit','lab:classMold:del']),
          // 遮罩层
          loading: true,
          // 选中数组
          ids: [],
          // 非单个禁用
          single: true,
          // 非多个禁用
          multiple: true,
          // 显示搜索条件
          showSearch: true,
          // 总条数
          total: 0,
          // 安全分级类型表格数据
          classtypeList: [],
          // 弹出层标题
          title: "",
          // 是否显示弹出层
          open: false,
          // 查询参数
          queryParams: {
            page: 1,
            pageSize:20,
            moldName: null,
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {
            moldName: [
              { required: true, message: "请输入实验室类型名称", trigger: "blur" },
              { required: true, message: "请输入安全类别名称", validator: this.spaceJudgment, trigger: "blur" }
            ],
          }
        };
      },
      created() {
        this.getList();
      },
      methods: {
        /** 查询安全分级类型列表 */
        getList() {
          this.loading = true;
          laboratoryClassMoldList(this.queryParams).then( response => {
            this.classtypeList =  response.data.records;
            this.total =  response.data.total;
            this.loading = false;
          });
        },
        // 取消按钮
        cancel() {
          this.open = false;
          this.reset();
        },
        // 表单重置
        reset() {
          this.form = {
            moldId: null,
            moldName: null,
          };
          this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
          this.queryParams.page = 1;
          this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
          // this.resetForm("queryForm");
          this.$set(this,'queryParams',{
            page: 1,
            pageSize:20,
            moldName:"",
          });
          this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
          this.ids = selection.map(item => item.id)
          this.single = selection.length!==1
          this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
          this.reset();
          this.title = "新增实验室类型";
          this.open = true;
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
          this.reset();
          laboratoryClassMoldDetail({id:row.moldId}).then( response => {
            this.$set(this,'form',{
              moldId:response.data.moldId,
              moldName:response.data.moldName,
            });
            this.title = "修改实验室类型";
            this.open = true;
          });
        },
        /** 提交按钮 */
        submitForm() {
          this.$refs["form"].validate(valid => {
            if (valid) {
              if (this.form.moldId != null) {
                laboratoryClassMoldUpdate(this.form).then( response => {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                });
              } else {
                laboratoryClassMoldAdd(this.form).then( response => {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                });
              }
            }
          });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
          const ids = row.id || this.ids;
          this.$confirm('是否确认删除实验室类型名称为"' + row.moldName + '"的数据项?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(() => {
            laboratoryClassMoldDelete({moldId:row.moldId}).then(response => {
              this.msgSuccess("操作成功");
              this.getList();
            });
          }).catch(() => {});
        },
        /** 导出按钮操作 */
        handleExport() {
          this.download('laboratory/classtype/export', {
            ...this.queryParams
          }, `laboratory_classtype.xlsx`)
        }
      }
    }
</script>

<style scoped lang="scss">
  .subjectClass{
    flex:1;
    display: flex!important;
    flex-direction: column;
    padding:20px!important;
    overflow: hidden;

  }
</style>
