<template>
  <div class="inspection-wrapper">
    <div class="inspection-tools-wrapper">
      <el-button type="primary" @click="createInspection">新增检查依据</el-button>
      <el-button type="warning" :disabled="editdisabled" @click="editInspection">编辑检查依据</el-button>
      <el-button type="danger" :disabled="deldisabled" @click="delInspection">删除检查依据</el-button>
    </div>
    <div class="inspectoin-content-wrapper">
      <el-table
        :data="hdInspcetionTableData"
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
        ></el-table-column>
        <el-table-column
          prop="hdinspection"
          label="检查依据"
          width="390"
        ></el-table-column>
      </el-table>
    </div>
    <el-dialog
      title="新增检查依据"
      :visible.sync="addDialogVisible"
      width="50%"
    >
      <el-form
        ref="addform"
        :model="addFormInfo"
        label-width="80px"
        :rules="addFormRules"
        v-if="addDialogVisible"
      >
        <el-form-item
          prop="hdinspection"
          label="检查依据"
        >
          <el-input
            type="textarea"
            autosize
            placeholder="请输入内容"
            v-model="addFormInfo.hdinspection">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="info" @click="cancelAddForm">取消</el-button>
        <el-button type="primary" @click="submitAddForm">提交</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="编辑检查依据"
      :visible.sync="editDialogVisible"
      width="50%"
    >
      <el-form
        ref="editform"
        :model="editFormInfo"
        label-width="80px"
        :rules="editFormRules"
        v-if="editDialogVisible"
      >
        <el-form-item
          prop="hdinspection"
          label="检查依据"
        >
          <el-input
            type="textarea"
            autosize
            placeholder="请输入内容"
            v-model="editFormInfo.hdinspection">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="info" @click="cancelEditForm">取消</el-button>
        <el-button type="primary" @click="submitEditForm">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      hdInspcetionTableData: [
        {
          id: 1,
          hdinspection: '依据内容1'
        },
        {
          id: 2,
          hdinspection: '依据内容2'
        },
        {
          id: 3,
          hdinspection: '依据内容3'
        }
      ],
      multiSelection: [],
      addDialogVisible: false,
      addFormInfo: {
        hdinspection: ''
      },
      addFormRules: {
        hdinspection: [
          {
            required: true,
            message: '请输入检查依据',
            trigger: 'blur'
          }
        ]
      },
      editDialogVisible: false,
      editFormInfo: {
        hdinspection: '',
        id: -1
      },
      editFormRules: {
        hdinspection: [
          {
            required: true,
            message: '请输入检查依据',
            trigger: 'blur'
          }
        ]
      },
      editdisabled: true,
      deldisabled: true
    }
  },
  methods: {
    handleSelectionChange(val) {
      this.multiSelection = val
      this.editdisabled = true
      this.deldisabled = true
      if (this.multiSelection.length === 1) {
        this.editdisabled = false
      }
      if (this.multiSelection.length > 0) {
        this.deldisabled = false
      }
    },
    createInspection() {
      this.addDialogVisible = true
    },
    editInspection() {
      this.editDialogVisible = true
      this.editFormInfo = {...this.multiSelection[0]}
    },
    delInspection() {
      this.$confirm('此操作将删除所选项, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // axios
      }).catch(() => {
        // 取消删除
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    cancelAddForm() {
      this.addDialogVisible = false
    },
    submitAddForm() {
      this.$refs.addform.validate((valid) => {
        if(valid) {}
      })
    },
    cancelEditForm() {
      this.editDialogVisible = false
    },
    submitEditForm() {
      this.$refs.editform.validate((valid) => {
        if(valid) {}
      })
    }
  }
}
</script>

<style lang="scss">
  .inspection-wrapper {
    width: 80%;
    margin: 30px auto;
    border-radius: 30px;
    padding: 30px;
    box-shadow: 0 0 9px 1px #eee;
    .inspection-tools-wrapper {
      width: 80%;
      margin: 30px auto;
      border-radius: 20px;
      padding: 20px;
      box-shadow: 0 0 9px 1px #eee;
    }
    .inspectoin-content-wrapper {
      width: 80%;
      margin: 30px auto;
      border-radius: 20px;
      padding: 20px;
      box-shadow: 0 0 9px 1px #eee;
    }
  }
</style>