<template>
  <div class="compedent-outer-wrapper">
    <div class="compedent-tools-wrapper">
      <el-button type="primary" @click="addChargeDept">新增主管部门</el-button>
      <el-button type="warning" :disabled="disableChargeDeptEdit" @click="editChargeDept">编辑主管部门</el-button>
      <el-button type="danger" :disabled="disableChargeDeptDelete" @click="deleteChargeDept">删除主管部门</el-button>
    </div>
    <div class="compedent-inner-wrapper">
      <div v-for="item in chargeDepts" :key="item.icdId">
        <label><input type="checkbox" :checked="item.checked" @change="inputChange($event, item)">{{ '   ' + item.icdName }}</label>
      </div>
    </div>
    <el-dialog
      title="新增主管部门"
      :visible.sync="addChargeDeptDialogShow"
      :close-on-click-modal="false"
    >
      <el-form
        v-if="addChargeDeptDialogShow"
        ref="addChargeDept"
        :model="formInfo"
        :rules="rules"
      >
        <el-form-item
          label="主管部门名称"
          prop="icdName"
        >
          <el-input v-model="formInfo.icdName" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="info" @click="cancelAddCharge">取消</el-button>
        <el-button type="primary" :loading="addFormLoading" @click="submitAddChargeDept">提交</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="编辑主管部门"
      :visible.sync="editChargeDeptDialogShow"
      :close-on-click-modal="false"
    >
      <el-form
        v-if="editChargeDeptDialogShow"
        ref="editChargeDept"
        :model="editFormInfo"
        :rules="rules"
      >
        <el-form-item
          label="主管部门名称"
          prop="icdName"
        >
          <el-input v-model="editFormInfo.icdName" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="info" @click="cancelEditChargeDept">取消</el-button>
        <el-button type="primary" :loading="editFormLoading" @click="submitEditChargeDept">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getIndustryChargeDept, postIndustryChargeDept, putIndustryChargeDept, deleteIndustryChargeDept } from '@/api/industry.js'
export default {
  data() {
    return {
      chargeDepts: [
      ],
      disableChargeDeptEdit: true,
      disableChargeDeptDelete: true,
      formInfo: {
        icdName: ''
      },
      rules: {
        icdName: [
          {
            required: true,
            message: '请输入主管部门名称',
            trigger: 'blur'
          }
        ]
      },
      addChargeDeptDialogShow: false,
      editChargeDeptDialogShow: false,
      addFormLoading: false,
      editFormInfo: {
        icdName: '',
        icdId: -1
      },
      editFormLoading: false
    }
  },
  mounted() {
    this.initChargeDepts()
  },
  methods: {
    initChargeDepts() {
      getIndustryChargeDept().then(res => {
        this.chargeDepts = []
        res.content.forEach(item => {
          const tempObj = {
            icdId: item.icdId,
            icdName: item.icdName,
            checked: false
          }
          this.chargeDepts.push(tempObj)
          console.log(this.chargeDepts, 'this.chargeDepts this.chargeDepts')
        })
      })
    },
    inputChange($event, item) {
      item.checked = $event.target.checked
      this.disableChargeDeptEdit = true
      this.disableChargeDeptDelete = true
      if (this.chargeDepts.filter(item => item.checked === true).length === 1) {
        this.disableChargeDeptEdit = false
      }
      if (this.chargeDepts.filter(item => item.checked === true).length > 0) {
        this.disableChargeDeptDelete = false
      }
    },
    // 新增主管部门
    addChargeDept() {
      // 展示dialog
      this.formInfo.icdName = ''
      this.addChargeDeptDialogShow = true
    },
    cancelAddCharge() {
      //
      this.addChargeDeptDialogShow = false
      this.formInfo.icdName = ''
    },
    submitAddChargeDept() {
      this.addFormLoading = true
      postIndustryChargeDept(this.formInfo.icdName).then(res => {
        this.initChargeDepts()
        this.addFormLoading = false
        this.$message({
          type: 'success',
          message: '添加成功'
        })

        this.addChargeDeptDialogShow = false
      }, err => {
        console.log(err, 'err by postIndustryChargeDept')
        this.addFormLoading = false
        this.$message.error('添加失败')
        this.addChargeDeptDialogShow = false
      })
    },
    editChargeDept() {
      this.editFormInfo.icdName = this.chargeDepts.filter(item => item.checked)[0].icdName
      this.editFormInfo.icdId = this.chargeDepts.filter(item => item.checked)[0].icdId
      this.editChargeDeptDialogShow = true
    },
    cancelEditChargeDept() {
      this.editChargeDeptDialogShow = false
    },
    submitEditChargeDept() {
      this.editFormLoading = false
      putIndustryChargeDept(this.editFormInfo.icdId, this.editFormInfo.icdName).then(res => {
        console.log(res, 'putIndustryChargeDept')
        this.editFormLoading = true
        this.initChargeDepts()
        this.$message({
          type: 'success',
          message: '编辑成功'
        })
        this.editChargeDeptDialogShow = false
      }).catch(res1 => {
        console.log(res1, 'err by putIndustryChargeDept')
        this.editFormLoading = true
        this.$message.error('编辑失败')
        this.editChargeDeptDialogShow = false
      })
    },
    deleteChargeDept() {
      this.$confirm('此操作将删除所选主管部门', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteIndustryChargeDept(this.chargeDepts.filter(item => item.checked).map(item => item.icdId)).then(res => {
          this.initChargeDepts()
          this.$message({
            type: 'success',
            message: '删除成功'
          })
        }).catch(err => {
          console.log(err)
          this.$message.error('删除失败')
        })
      }).catch((err) => {
        console.log(err)
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
