<template>
  <div class="hd-type-wrapper">
    <div class="hd-type-tools-wrapper">
      <el-button type="primary" @click="createHdType">新增</el-button>
      <el-button type="warning" :disabled="isEditHdTypeEnable" @click="editHdType">编辑</el-button>
      <el-button type="danger" :disabled="isDelHdTypeEnable" @click="delHdType">删除</el-button>
    </div>
    <div class="hd-type-table-wrapper">
      <el-table
        :data="hdTypeTable"
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
        ></el-table-column>
        <el-table-column
          prop="type"
          label="隐患类别"
          width="240"
        ></el-table-column>
      </el-table>
    </div>
    <el-dialog
      title="添加风险类别"
      :visible.sync="addDialogVisible"
      width="50%"
    >
      <el-form
        ref="addform"
        :model="addFormInfo"
        label-width="80px"
        :rules="addHdTypeRules"
        v-if="addDialogVisible"
      >
        <el-form-item
          prop="type"
          label="隐患类别"
        >
          <el-input v-model="addFormInfo.type" maxlength="50"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="info" @click="cancelAddHdType">取消</el-button>
        <el-button type="primary" @click="confirmAddHdType">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="编辑隐患类别"
      :visible.sync="editDialogVisible"
      width="50%"
    >
      <el-form
        ref="editform"
        :model="editFormInfo"
        label-width="80px"
        :rules="editHdTypeRules"
        v-if="editDialogVisible"
      >
        <el-form-item
          prop="type"
          label="隐患类别"
        >
          <el-input v-model="editFormInfo.type" maxlength="50"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="info" @click="cancelEditHdType">取消</el-button>
        <el-button type="primary" @click="confirmEditHdType">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      hdTypeTable: [
        {
          id: 1,
          type: '类别1'
        },
        {
          id: 2,
          type: '类别2'
        }
      ],
      multipleSelection: [],
      isEditHdTypeEnable: true,
      isDelHdTypeEnable: true,
      addDialogVisible: false,
      addFormInfo: {
        type: ''        
      },
      addHdTypeRules: {
        type: [
          {
            required: true,
            message: '请输入隐患类别',
            trigger: 'blur'
          }
        ]
      },
      editDialogVisible: false,
      editFormInfo: {
        id: -1,
        type: ''
      },
      editHdTypeRules: {
        type: [
          {
            required: true,
            message: '请输入隐患类别',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val
      this.isEditHdTypeEnable = true;
      this.isDelHdTypeEnable = true;
      if (this.multipleSelection.length === 1) {
        this.isEditHdTypeEnable = false
      }
      if (this.multipleSelection.length > 0) {
        this.isDelHdTypeEnable = false
      }
    },
    createHdType() {
      this.addDialogVisible = true
    },
    editHdType() {
      this.editFormInfo = {...this.multipleSelection[0]}
      this.editDialogVisible = true
    },
    delHdType() {
      this.$confirm('此操作将删除所选项, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // axios
      }).catch(() => {
        // 取消删除
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    cancelAddHdType() {
      this.addDialogVisible = false
    },
    confirmAddHdType() {
      this.$refs.addform.validate((valid) => {
        if (valid) {
          // axios
        }
      })
      // validate form
      // this.addDialogVisible = false
    },
    cancelEditHdType() {
      this.editDialogVisible = false
    },
    confirmEditHdType() {
      this.$refs.editform.validate((valid) => {
        if (valid) {
          // axios
        }
      })
      // validate form
      // this.editDialogVisible = false
    }
  }
}
</script>

<style lang="scss" scoped>
  .hd-type-wrapper {
    width: 80%;
    border-radius: 30px;
    margin: 30px auto;
    padding: 30px;
    box-shadow: 0 0 9px 1px #eee;
    .hd-type-tools-wrapper {
      width: 80%;
      margin: 30px auto;
      padding: 10px;
      display: flex;
      flex-direction: row;
      box-shadow: 0 0 9px 1px #eee;
      border-radius: 10px;
    }
    .hd-type-table-wrapper {
      width: 80%;
      margin: 30px auto;
      padding: 10px;
      box-shadow: 0 0 9px 1px #eee;
      border-radius: 10px;
    }
  }
</style>