<template>
  <!-- 入户安全检查项 -->
  <div>
    <actionBar menuTitle="入户安全检查项" :operateList="operateList" :formList="formList" :ruleForm="searchRuleForm"
      @openModel="openModel" @searchProtocol="searchProtocol">
    </actionBar>
    <tables :suspension="false" :tableList="tableList" :tableData="tableData" @buttonData="buttonData"></tables>
    <!-- 分页 -->
    <div class="paging_bottom" v-if="Total">
      <pagination :total="Total" :currentPage="Pagination.PageIndex" @changeSize="changeSize"
        @changeCurrent="changeCurrent">
      </pagination>
    </div>
    <el-dialog :title="staffTitle" :visible.sync="staffDialogVisible" width="800px" :close-on-click-modal="false"
      @close="closeDialog">
      <forms ref="childRules" :formNewList="formRuleList" :ruleForm="ruleForm" labelWidth="100px"></forms>
      <span slot="footer" class="dialog-footer">
        <el-button plain @click="staffDialogVisible = false">取 消</el-button>
        <el-button type="primary" :loading="confirmLoading" @click="handleAdd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    getInspectExpand,
    addInspectExpand,
    putInspectExpand,
    delInspectExpand
  } from '@/api/securityCheck'
  import actionBar from '@/components/actionBar'
  import tables from '@/components/tables'
  import pagination from '@/components/pagination'
  import forms from '@/components/forms'
  import {
    formRules,
    employee
  } from "./checkEntry.js";
  export default {
    components: {
      actionBar,
      tables,
      pagination,
      forms
    },
    data() {
      return {
        staffTitle: '添加',
        staffDialogVisible: false,
        Pagination: {
          PageIndex: 1,
          PageSize: 10,
        },
        operateList: [{
          type: 'add',
          title: '添加',
          icon: 'el-icon-plus',
        }],
        formList: [{
          type: 'input',
          label: '检查项',
          field: 'inspectExplain',
          placeholder: '检查项',
        }],
        searchRuleForm: {
          inspectExplain: '',
        },
        tableList: employee(),
        tableData: [],
        Total: 0,
        formRuleList: [],
        ruleForm: {
          inspectExplain: '',
        },
        enterpriseId: null,
        operationType: '',
        confirmLoading: false,
      }
    },
    mounted() {
      const dataList = formRules();
      this.formRuleList = dataList;
      this.getList()
    },
    methods: {
      // 搜索
      searchProtocol(value) {
        this.searchValue = value
        this.getList()
      },
      openModel(type) {
        this.operationType = type
        if (type == 'add') {
          this.staffTitle = '添加'
        }
        this.staffDialogVisible = true
      },
      // 获取入户安全检查列表
      getList() {
        var params = {
          page: this.Pagination.PageIndex,
          pageSize: this.Pagination.PageSize,
          ...this.searchValue
        }
        getInspectExpand(params).then(res => {
          if (res.code == 200) {
            this.tableData = res.data.list
            this.Total = res.data.count
          }
        })
      },
      handleAdd() {
        let flag = this.$refs['childRules'].validateForm();
        if (flag) {
          this.confirmLoading = true
          if (this.operationType == 'add') {
            var params = {
              ...this.ruleForm
            }
            addInspectExpand(params).then(res => {
              if (res.code == 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success'
                });
                this.getList()
              }
              this.staffDialogVisible = false
              this.confirmLoading = false
            }).catch(() => {
              this.confirmLoading = false
            })
          } else if (this.operationType == 'edit') {
            this.editItem()
          }
        } else {
          this.$message.error('表单信息不完整，请继续填写完整');
        }
      },
      editItem() {
        var params = {
          id: this.enterpriseId,
          ...this.ruleForm
        }
        params.price = Number(params.price)
        putInspectExpand(params).then(res => {
          if (res.code == 200) {
            this.$message({
              message: '操作成功',
              type: 'success'
            });
            this.getList()
          }
          this.staffDialogVisible = false
          this.confirmLoading = false
        }).catch(() => {
          this.confirmLoading = false
        })
      },
      // 删除
      deleteItem(id) {
        this.$confirm('此操作将永久删除该检查项, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delInspectExpand({
            id: id,
          }).then(res => {
            if (res.code == 200) {
              this.$message({
                message: '操作成功',
                type: 'success'
              });
              this.getList()
            }
          })
        }).catch(() => {});
      },
      buttonData(row, type) {
        this.enterpriseId = row.id
        this.operationType = type
        if (type == 'edit') {
          this.staffDialogVisible = true
          this.staffTitle = '编辑'
          this.$nextTick(() => {
            this.ruleForm.inspectExplain = row.inspectExplain
          })
        } else if (type == 'del') {
          this.deleteItem(row.id)
        }
      },
      changeSize(val) {
        this.Pagination.PageSize = val
        this.getList()
      },
      changeCurrent(val) {
        this.Pagination.PageIndex = val
        this.getList()
      },
      // 清空表单
      closeDialog() {
        this.$refs.childRules.resetCheck();
      }
    },
  }
</script>

<style lang="scss" scoped>
</style>
