<template>
  <div class="check-content-wrapper">
    <div class="check-content-tools-wrapper">
      <el-button type="primary" @click="createcheckcontent">新增</el-button>
      <el-button type="warning" :disabled="editchekcontentdisable" @click="editcheckcontent">编辑</el-button>
      <el-button type="danger" :disabled="delcheckcontentdisable" @click="delcheckcontent">删除</el-button>
    </div>
    <div class="check-content-table-wrapper">
      <el-table
        :data="hdCheckContentTableData"
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
        ></el-table-column>
        <el-table-column
          prop="hdtype"
          label="隐患类别"
          width="240"
        ></el-table-column>
        <el-table-column
          prop="hdcheckcontent"
          label="隐患检查内容"
          width="480"
        ></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="hdtype"
          label="隐患类别"
        >
          <el-select v-model="addFormInfo.hdtype">
            <el-option
              v-for="item in hdTypes"
              :key="item.id"
              :label="item.hdtype"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          prop="hdcheckcontent"
          label="隐患检查内容"
        >
          <el-input v-model="addFormInfo.hdcheckcontent" maxlength="100"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="info" @click="cancelAddHdCheckContent">取消</el-button>
        <el-button type="primary" @click="confirmAddHdCheckContent">确定</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="hdtype"
          label="隐患类别"
        >
          <el-select v-model="editFormInfo.hdtype">
            <el-option
              v-for="item in hdTypes"
              :key="item.id"
              :label="item.hdtype"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          prop="hdcheckcontent"
          label="隐患检查内容"
        >
          <el-input v-model="editFormInfo.hdcheckcontent"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="info" @click="cancelEditHdCheckContent">取消</el-button>
        <el-button type="primary" @click="confirmEditHdCheckContent">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      hdCheckContentTableData: [
        {
          id: 1,
          hdTypeId: 1,
          hdtype: '类别1',
          hdcheckcontent: '隐患检查内容1'
        },
        {
          id: 2,
          hdTypeId: 2,
          hdtype: '类别2',
          hdcheckcontent: '隐患检查内容2'
        }
      ],
      multiSelection: [],
      addDialogVisible: false,
      editchekcontentdisable: true,
      delcheckcontentdisable: true,
      addFormInfo: {
        hdtype: '',
        hdtypeId: -1,
        hdcheckcontent: ''
      },
      hdTypes: [
        {
          id: 1,
          hdtype: '类别1'
        },
        {
          id: 2,
          hdtype: '类别2'
        },
        {
          id: 3,
          hdtype: '类别3'
        }
      ],
      addFormRules: {
        hdtype: [
          {
            required: true,
            message: '请选择隐患类别',
            trigger: 'change'
          }
        ],
        hdcheckcontent: [
          {
            required: true,
            message: '请输入检查内容',
            trigger: 'blur'
          }
        ]
      },
      editDialogVisible: false,
      editFormInfo: {
        id: -1,
        hdtypeId: 1,
        hdtype: '',
        hdcheckcontent: ''
      },
      editFormRules: {
        hdtype: [
          {
            required: true,
            message: '请选择隐患类别',
            trigger: 'change'
          }
        ],
        hdcheckcontent: [
          {
            required: true,
            message: '请输入检查内容',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    handleSelectionChange(val) {
      this.multiSelection = val
      this.editchekcontentdisable = true
      this.delcheckcontentdisable = true
      if (this.multiSelection.length === 1) {
        this.editchekcontentdisable = false
      }
      if (this.multiSelection.length > 0) {
        this.delcheckcontentdisable = false
      }
    },
    cancelAddHdCheckContent() {
      this.addDialogVisible = false
    },
    confirmAddHdCheckContent() {
      this.$refs.addform.validate((valid) => {
        if (valid) {
          //axios
        }
      })
    },
    createcheckcontent() {
      this.addDialogVisible = true
      this.addFormInfo = {
        hdtype: '',
        hdcheckcontent: ''
      }
    },
    editcheckcontent() {
      this.editDialogVisible = true
      this.editFormInfo = {...this.multiSelection[0]}
      console.log(this.editFormInfo, 'this.editFormInfo')
    },
    delcheckcontent() {
      this.$confirm('此操作将删除所选项, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // axios
      }).catch(() => {
        // 取消删除
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    cancelEditHdCheckContent() {
      this.editDialogVisible = false
    },
    confirmEditHdCheckContent() {
      console.log(this.editFormInfo, 'this.editFormInfo')
      console.log(typeof this.editFormInfo.hdTypeId)
      this.$refs.editform.validate((valid) => {
        if (valid) {
          // this.editDialogVisible
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .check-content-wrapper {
    width: 80%;
    margin: 30px auto;
    padding: 30px;
    border-radius: 30px;
    box-shadow: 0 0 9px 1px #eee;
    .check-content-tools-wrapper {
      display: flex;
      justify-content: flex-start;
      padding: 20px;
      border-radius: 20px;
      margin: 30px auto;
      width: 80%;
      box-shadow: 0 0 9px 1px #eee;
    }
    .check-content-table-wrapper {
      display: flex;
      justify-content: flex-start;
      padding: 20px;
      border-radius: 20px;
      margin: 30px auto;
      width: 80%;
      box-shadow: 0 0 9px 1px #eee;
    }
  }
</style>