<template>
  <div class="discover-wrapper">
    <div class="discover-tools-wrapper">
      <el-button type="primary" @click="createDiscover">新建</el-button>
      <el-button type="warning" :disabled="editdisabled" @click="editCDiscover">编辑</el-button>
      <el-button type="danger" :disabled="deldisabled" @click="delDiscover">删除</el-button>
    </div>
    <div class="discover-content-wrapper">
      <el-table
        :data="discoverTableData"
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
        ></el-table-column>
        <el-table-column
          prop="hdtype"
          label="隐患类别"
        ></el-table-column>
        <el-table-column
          prop="hdcheckcontent"
          label="检查内容"
        ></el-table-column>
        <el-table-column
          prop="hddiscover"
          label="发现问题"
        ></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.hdtype"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          prop="hdcontent"
          label="隐患检查内容"
        >
          <el-select v-model="addFormInfo.hdcontent">
            <el-option
              v-for="item in hdContents"
              :key="item.id"
              :label="item.hdcontent"
              :value="item.hdcontent"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          prop="hddiscover"
          label="发现问题"
        >
          <el-input
            type="textarea"
            autosize
            placeholder="请输入内容"
            maxlength="500"
            v-model="addFormInfo.hddiscover">
          </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="hdtype"
          label="隐患类别"
        >
          <el-select
            v-model="editFormInfo.hdtype"
          >
            <el-option
              v-for="item in hdTypes"
              :key="item.id"
              :label="item.hdtype"
              :value="item.hdtype"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          prop="hdcontent"
          label="隐患检查内容"
        >
          <el-select
            v-model="editFormInfo.hdcontent"
          >
            <el-option
              v-for="item in hdContents"
              :key="item.id"
              :label="item.hdcontent"
              :value="item.hdcontent"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          prop="hddiscover"
          label="发现问题"
        >
          <el-input
            type="textarea"
            autosize
            placeholder="请输入内容"
            maxlength="500"
            v-model="editFormInfo.hddiscover">
          </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 {
      discoverTableData: [
        {
          hdtype: '隐患类别1',
          hdtypeId: 1,
          hdcontent: '检查内容1',
          hdcontentId: 1,
          hddiscover: '发现问题1',
          hddiscoverId: 1
        },
        {
          hdtype: '隐患类别2',
          hdtypeId: 2,
          hdcontent: '检查内容2',
          hdcontentId: 2,
          hddiscover: '发现问题2',
          hddiscoverId: 2
        },
        {
          hdtype: '隐患类别3',
          hdtypeId: 3,
          hdcontent: '检查内容3',
          hdcontentId: 3,
          hddiscover: '发现问题3',
          hddiscoverId: 3
        }
      ],
      multiSelection: [],
      addDialogVisible: false,
      hdTypes: [
        {
          id: 1,
          hdtype: '类别1'
        },
        {
          id: 2,
          hdtype: '类别2'
        },
        {
          id: 3,
          hdtype: '类别3'
        }
      ],
      hdContents: [
        {
          id: 1,
          hdcontent: '检查内容1'
        },
        {
          id: 2,
          hdcontent: '检查内容2'
        },
        {
          id: 3,
          hdcontent: '检查内容3'
        }
      ],
      addFormInfo: {
        hdtype: '',
        hdtypeId: -1,
        hdcontent: '',
        hdcontentId: -1,
        hddiscover: '',
      },
      addFormRules: {
        hdtype: [
          {
            required: true,
            message: '请选择隐患类别',
            trigger: 'change'
          }
        ],
        hdcontent: [
          {
            required: true,
            message: '请选择隐患检查内容',
            trigger: 'change'
          }
        ],
        hddiscover: [
          {
            required: true,
            message: '请输入发现问题',
            trigger: 'blur'
          }
        ]
      },
      editDialogVisible: false,
      editFormInfo: {
        hdtype: '',
        hdtypeId: -1,
        hdcontent: '',
        hdcontentId: -1,
        hddiscover: '',
        hddiscoverId: -1
      },
      editFormRules: {
                hdtype: [
          {
            required: true,
            message: '请选择隐患类别',
            trigger: 'blur'
          }
        ],
        hdcontent: [
          {
            required: true,
            message: '请选择隐患检查内容',
            trigger: 'blur'
          }
        ],
        hddiscover: [
          {
            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
      }
    },
    createDiscover() {
      this.addDialogVisible = true
    },
    editCDiscover() {
      this.editDialogVisible = true
      this.editFormInfo = {...this.multiSelection[0]}
    },
    delDiscover() {
      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">
  .discover-wrapper {
    width: 80%;
    margin: 30px auto;
    padding: 30px;
    border-radius: 30px;
    box-shadow: 0 0 9px 1px #eee;
    .discover-tools-wrapper {
      width: 80%;
      margin: 30px auto;
      padding: 20px;
      border-radius: 20px;
      box-shadow: 0 0 9px 1px #eee;
    }
    .discover-content-wrapper {
      width: 80%;
      margin: 30px auto;
      padding: 20px;
      border-radius: 20px;
      box-shadow: 0 0 9px 1px #eee;
    }
  }
</style>