<template>
  <div class="app-container">
    <el-card>
      <el-button type="primary" icon="el-icon-plus" @click="handleAddinspectTable"
        >新增</el-button
      >
    </el-card>
    <el-card style="margin-top: 10px">
      <el-table
        v-loading="listLoading"
        :data="inspectTableList"
        border
        fit
        stripe
        highlight-current-row
        max-height="600"
      >
        <el-table-column type="index" width="50" />
         <el-table-column label="检查类别">
          <template slot-scope="scope">{{ scope.row.cate_name }}</template>
        </el-table-column>
         <el-table-column label="序号">
          <template slot-scope="scope">{{ scope.row.sortnum }}</template>
        </el-table-column>

        <el-table-column label="检查要点">
          <template slot-scope="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column label="检查类型">
          <template slot-scope="scope">{{ scope.row.type }}</template>
        </el-table-column>

        <el-table-column label="取证要求">
          <template slot-scope="scope">{{ scope.row.require }}</template>
        </el-table-column>

       
      

        <el-table-column
          align="center"
          label="操作"
          width="200px"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
             :disabled="!checkPermission(['inspectTable'])"
              type="primary"
              size="small"
              icon="el-icon-edit"
              @click="handleEdit(scope)"
            />
            <el-button
             :disabled="!checkPermission(['inspectTable'])"
              type="danger"
              size="small"
              icon="el-icon-delete"
              @click="handleDelete(scope)"
            />
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog
      :visible.sync="dialogVisible"
      :title="dialogType === 'edit' ? '编辑资料' : '新增资料'"
    >
      <el-form
        ref="Form"
        :model="inspectTable"
        label-width="80px"
        label-position="right"
        :rules="rule1"
      >
        <el-form-item label="检查要求" prop="name">
          <el-input  type="textarea"
            :rows="4" v-model="inspectTable.name" placeholder="检查要求" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
            <el-select
          v-model="inspectTable.type"
          placeholder="类型"
          clearable
          style="width: 140px"
          @change="handleFilter"
        >
          <el-option
            v-for="item in stateOptions"
            :key="item.key"
            :label="item.name"
            :value="item.key"
          />
        </el-select>
        </el-form-item>
      <el-form-item label="类别" prop="cate">
          <el-cascader
            v-model="inspectTable.cate"
            :options="typeOptions"
            :props="{ emitPath: false }"
            clearable
            style="width: 100%"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="取证要求" prop="desc">
          <el-input
            type="textarea"
            :rows="4"
            v-model="inspectTable.require"
            placeholder="详情"
          />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number
            v-model="inspectTable.sortnum"
            :min="1"
            :max="999"
          ></el-input-number>
        </el-form-item>
      
      </el-form>
      <div style="text-align: right">
        <el-button type="danger" @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirm('Form')">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  getQualityinspectList,
  createQualityinspect,
  deleteQualityinspect,
  updateQualityinspect,
} from "@/api/qualityinspect";
import { genTree } from "@/utils";
import checkPermission from "@/utils/permission";
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { getDictList, getDictTypeList } from "@/api/dict";
const defaultinspectTable = {
  name: "",
  require: "",
  type: null,
  sortnum: null,
};
export default {
  components: { Pagination, Treeselect },
  data() {
    return {
      inspectTable: defaultinspectTable,
      inspectTableList: [],
      typeOptions: [],
       stateOptions: [
        { key: "合规", name: "合规" },
        { key: "体系", name: "体系" }
      ],
      listLoading: true,
      dialogVisible: false,
      dialogType: "new",
      rule1: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
      },
      filterOrgText: "",
      treeLoding: false,
    };
  },
  computed: {},
  watch: {
    filterOrgText(val) {
      this.$refs.tree.filter(val);
    },
  },
  created() {
    this.getList();
    this.getTypeAll();
  },
  methods: {
    checkPermission,

    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },

    getList() {
      this.listLoading = true;
      getQualityinspectList().then((response) => {
        if (response.data) {
          this.inspectTableList = response.data;
        }
        this.listLoading = false;
      });
    },
    getTypeAll() {
      getDictList({ type__code: "inspect_category" }).then((res) => {
        this.typeOptions = genTree(res.data);
      });
    },

    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleAddinspectTable() {
      this.inspectTable = Object.assign({}, defaultinspectTable);
      this.dialogType = "new";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["Form"].clearValidate();
      });
    },
    handleEdit(scope) {
      this.inspectTable = Object.assign({}, scope.row); // copy obj
      this.dialogType = "edit";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["Form"].clearValidate();
      });
    },
    handleDelete(scope) {
      this.$confirm("确认删除?", "警告", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "error",
      })
        .then(async () => {
          await deleteQualityinspect(scope.row.id);
          this.getList();
          this.$message.success("成功");
        })
        .catch((err) => {
          console.error(err);
        });
    },
    async confirm(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          const isEdit = this.dialogType === "edit";
          if (isEdit) {
            updateQualityinspect(this.inspectTable.id, this.inspectTable).then((res) => {
              if (res.code >= 200) {
                this.getList();
                this.dialogVisible = false;
                this.$message.success("成功");
              }
            });
          } else {
            createQualityinspect(this.inspectTable).then((res) => {
              if (res.code >= 200) {
                this.getList();
                this.dialogVisible = false;
                this.$message.success("成功");
              }
            });
          }
        } else {
          return false;
        }
      });
    },
  },
};
</script>
