<template>
  <div class="app-container">
    <el-row :gutter="6">
      <el-col :span="8">
            <el-card>
      <el-button type="primary" icon="el-icon-plus" @click="handleAddinspectTables"
        >创建模板</el-button
      >
    
      <el-table
      style="margin-top:4px"
        v-loading="listLoading"
        :data="inspecttemplateList"
        border
        fit
        stripe
        highlight-current-row
        max-height="600"
        @row-click="recorclickRow"
      >
        <el-table-column type="index" width="50" />
         <el-table-column align="center" label="检查模板名称">
          <template slot-scope="scope"  >
              {{ scope.row.name }}</template>
        </el-table-column>
        
      

        <el-table-column
          align="center"
          label="操作"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
             :disabled="!checkPermission(['inspectTable'])"
              type="primary"
              size="small"
              icon="el-icon-edit"
              @click="handleEdits(scope)"
            />
            <el-button
             :disabled="!checkPermission(['inspectTable'])"
              type="danger"
              size="small"
              icon="el-icon-delete"
              @click="handleDeletes(scope)"
            />
          </template>
        </el-table-column>
      </el-table>
    </el-card>
        </el-col>
        <el-col :span="16">
          <el-card >
      <el-button type="primary" icon="el-icon-plus" @click="handleAddinspectTable"
        >新增</el-button
      >
   
      <el-table
      style="margin-top:4px"
        v-loading="listLoading"
        :data="inspectTableList"
        border
        fit
        stripe
        highlight-current-row
        max-height="600"
      >
        <el-table-column type="index" width="50" />
         <el-table-column align="center" label="检查类别">
          <template slot-scope="scope">{{ scope.row.cate_name }}</template>
        </el-table-column>
         <el-table-column align="center" label="序号">
          <template slot-scope="scope">{{ scope.row.sortnum }}</template>
        </el-table-column>

        <el-table-column align="center" label="检查事项">
          <template slot-scope="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column align="center" label="检查类型">
          <template slot-scope="scope">{{ scope.row.type }}</template>
        </el-table-column>

        <el-table-column align="center" 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(['qualityinspect'])"
              type="primary"
              size="small"
              icon="el-icon-edit"
              @click="handleEdit(scope)"
            />
            <el-button
             :disabled="!checkPermission(['qualityinspect'])"
              type="danger"
              size="small"
              icon="el-icon-delete"
              @click="handleDelete(scope)"
            />
          </template>
        </el-table-column>
      </el-table>
    </el-card>
          </el-col>
      </el-row>

      
    <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>
    <el-dialog
      :visible.sync="dialogVisibles"
      :title="dialogTypes === 'edit' ? '编辑' : '新增'"
    >
      <el-form
        ref="Forms"
        :model="inspecttemplate"
        label-width="80px"
        label-position="right"
        :rules="rule1"
      >
        <el-form-item label="模板名称" prop="name">
          <el-input  v-model="inspecttemplate.name" placeholder="检查模板名称" />
        </el-form-item>
       
      
      </el-form>
      <div style="text-align: right">
        <el-button type="danger" @click="dialogVisibles = false">取消</el-button>
        <el-button type="primary" @click="confirms('Forms')">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {getInspecttemplateList,createInspecttemplate,updateInspecttemplate,deleteInspecttemplate,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 defaultinspecttemplate = {
  name: "",
};
const defaultinspectTable = {
  name: "",
  require: "",
  type: null,
  sortnum: null,
  template:null,
};
export default {
  components: { Pagination, Treeselect },
  data() {
    return {
      inspecttemplate: defaultinspecttemplate,
      inspecttemplateList: [],
      listLoading: true,
      dialogVisibles: false,
      dialogTypes: "new",
      rule1: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
      },
      treeLoding: false,
       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.getTypeAll();
    this.getLists();
  },
  methods: {
    checkPermission,

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

    getLists() {
      this.listLoading = true;
      getInspecttemplateList({pageoff:true}).then((response) => {
        if (response.data) {
          this.inspecttemplateList = response.data;
        }
        this.listLoading = false;
      });
    },
     recorclickRow(row) {
       
       this.template=row.id;
        this.listLoading = true;
      getQualityinspectList({pageoff:true,template:this.template}).then((response) => {
        if (response.data) {
          this.inspectTableList = response.data;
        }
        this.listLoading = false;
      });
      
    },
   
    handleAddinspectTables() {
      this.inspecttemplate = Object.assign({}, defaultinspecttemplate);
      this.dialogTypes = "new";
      this.dialogVisibles = true;
      this.$nextTick(() => {
        this.$refs["Forms"].clearValidate();
      });
    },
    handleEdits(scope) {
      this.inspecttemplate = Object.assign({}, scope.row); // copy obj
      this.dialogTypes = "edit";
      this.dialogVisibles = true;
      this.$nextTick(() => {
        this.$refs["Forms"].clearValidate();
      });
    },
    handleDeletes(scope) {
      this.$confirm("确认删除?", "警告", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "error",
      })
        .then(async () => {
          await deleteInspecttemplate(scope.row.id);
          this.getLists();
          this.$message.success("成功");
        })
        .catch((err) => {
          console.error(err);
        });
    },
    async confirms(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          const isEdit = this.dialogTypes === "edit";
          if (isEdit) {
            updateInspecttemplate(this.inspecttemplate.id, this.inspecttemplate).then((res) => {
              if (res.code >= 200) {
                this.getLists();
                this.dialogVisibles = false;
                this.$message.success("成功");
              }
            });
          } else {
            
            createInspecttemplate(this.inspecttemplate).then((res) => {
              if (res.code >= 200) {
                this.getLists();
                this.dialogVisibles = false;
                this.$message.success("成功");
              }
            });
          }
        } else {
          return 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) {
             this.inspectTable.template=this.template;
            updateQualityinspect(this.inspectTable.id, this.inspectTable).then((res) => {
              if (res.code >= 200) {
                this.dialogVisible = false;
                 this.getLists();
                this.$message.success("成功");
              }
            });
          } else {
            this.inspectTable.template=this.template;
            createQualityinspect(this.inspectTable).then((res) => {
              if (res.code >= 200) {
                this.dialogVisible = false;
                 this.getLists();
                this.$message.success("成功");
              }
            });
          }
        } else {
          return false;
        }
      });
    },
  },
};
</script>
