<template>
  <el-dialog :visible.sync="visible" :title="title" width="600px">
    <el-form v-loading="loading" ref="form" :rules="rules" :model="form" label-position="top" label-width="110px" style="width: 500px;">
      <el-form-item label="行业类型" prop="INDUSTRY_TYPE">
        <el-cascader :props="industry_type_list_rule" v-model="form.INDUSTRY_TYPE" :options="industry_type_list" clearable style="width: 100%"/>
      </el-form-item>
      <el-form-item label="检查隐患类型" prop="HIDDEN_TYPES">
        <el-cascader :props="hidden_types_list_rule" v-model="form.HIDDEN_TYPES" :options="hidden_types_list" clearable style="width: 100%"/>
      </el-form-item>
      <el-form-item label="隐患区域(风险单元名称)" prop="RISK_UNIT_NAME">
        <el-input id="RISK_UNIT_NAME" ref="RISK_UNIT_NAME" v-model="form.RISK_UNIT_NAME" maxlength="255" placeholder="这里输入隐患区域(风险单元名称)..." title="隐患区域(风险单元名称)"/>
      </el-form-item>
      <el-form-item label="隐患部位" prop="HIDDEN_PART">
        <el-input id="HIDDEN_PART" ref="HIDDEN_PART" v-model="form.HIDDEN_PART" maxlength="255" placeholder="这里输入隐患部位..." title="隐患部位"/>
      </el-form-item>
      <el-form-item label="隐患描述" prop="INSPECTION_BASIS">
        <el-input id="INSPECTION_BASIS" ref="INSPECTION_BASIS" v-model="form.INSPECTION_BASIS" maxlength="255" placeholder="这里输入检查依据..." title="检查依据"/>
      </el-form-item>
      <el-form-item label="检查依据" prop="HIDDEN_DESCR">
        <el-input id="HIDDEN_DESCR" ref="HIDDEN_DESCR" v-model="form.HIDDEN_DESCR" maxlength="255" placeholder="这里输入隐患描述..." title="隐患描述"/>
      </el-form-item>
      <el-form-item label="依据内容" prop="INSPECTION_INFO">
        <el-input id="INSPECTION_INFO" ref="INSPECTION_INFO" v-model="form.INSPECTION_INFO" maxlength="255" placeholder="这里输入检查依据..." title="检查依据"/>
      </el-form-item>
      <el-form-item label="整改建议" prop="RECTIFYDESCR">
        <el-input id="RECTIFYDESCR" ref="RECTIFYDESCR" v-model="form.RECTIFYDESCR" maxlength="255" placeholder="这里输入整改建议..." title="整改建议"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
import waves from '@/directive/waves'
export default {
  components: { Pagination },
  directives: { waves },
  props: {
    title: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      loading: false,
      form: {
        HIDDEN_LIBRARY_ID: '',
        INDUSTRY_TYPE_ONE: '', // 行业类型1(数据字典)
        INDUSTRY_TYPE_TWO: '', // 行业类型2(数据字典)
        INDUSTRY_TYPE_THREE: '', // 行业类型3(数据字典)
        INDUSTRY_TYPE_FOUR: '', // 行业类型4(数据字典)
        INDUSTRY_TYPE: [],
        HIDDEN_TYPES: [], // 检查隐患类型(数据字典)
        HIDDEN_TYPE_ONE: '', // 检查隐患类型(数据字典)
        HIDDEN_TYPE_TWO: '', // 检查隐患类型(数据字典)
        HIDDEN_TYPE_THREE: '', // 检查隐患类型(数据字典)
        RISK_UNIT_ID: '', // 隐患区域(风险单元)
        RISK_UNIT_NAME: '', // 隐患区域名称(风险单元名称)
        HIDDEN_PART: '', // 隐患部位
        HIDDEN_DESCR: '', // 隐患描述
        CREATOR_ID: '', // 创建人
        CREATOR_NAME: '', // 创建人名称
        CREATED_TIME: '', // 创建时间
        OPERATOR_ID: '', // 更改人id
        OPERATOR_NAME: '', // 更改人名称
        OPERAT_TIME: '', // 更改时间
        INSPECTION_BASIS: '', // 检查依据
        INSPECTION_INFO: '',
        CORP_INFO_ID: '', // 企业id
        ISDELETE: '', // 删除标志位
        STATUS: '', // 状态（0-编辑状态，1-已加入库）
        CORPINFO_ID: '',
        SOURCE: '0'
      },
      rules: {
        HIDDEN_TYPES: [{
          required: true,
          validator: (rules, value, callback) => {
            if (!value || value.length === 0 || value[0] === '') {
              return callback(new Error('检查隐患类型必选'))
            }
            return callback()
          },
          trigger: 'blur'
        }],
        RISK_UNIT_NAME: [{ required: true, message: '隐患区域名称(风险单元名称)不能为空', trigger: 'blur' }],
        HIDDEN_PART: [{ required: true, message: '隐患部位不能为空', trigger: 'blur' }],
        INSPECTION_BASIS: [{ required: true, message: '隐患描述不能为空', trigger: 'blur' }],
        RECTIFYDESCR: [{ required: true, message: '整改建议不能为空', trigger: 'blur' }],
        CORPINFO_ID: [{ required: true, message: '企业必选', trigger: 'blur' }],
        INDUSTRY_TYPE: [{
          required: true,
          validator: (rules, value, callback) => {
            if (!value || value.length === 0 || value[0] === '') {
              return callback(new Error('行业类型必选'))
            }
            return callback()
          },
          trigger: 'blur'
        }]
      },
      industry_type_list_rule: {
        value: 'DICTIONARIES_ID',
        label: 'NAME',
        children: 'children'
      },
      hidden_types_list_rule: {
        value: 'DICTIONARIES_ID',
        label: 'NAME',
        children: 'children',
        checkStrictly: true
      },
      industry_type_list: [],
      hidden_types_list: [],
      listQuery: {
        page: 1,
        limit: 10
      },
      corp_info_list: [],
      risk_unit_list: [],
      visible: false,
      info: {}
    }
  },
  methods: {
    init(e) {
      this.visible = true
      this.getRiskUnit()
      this.getDicTree()
      if (e) {
        this.form = e
        this.$set(this.form, 'INDUSTRY_TYPE', [this.form.INDUSTRY_TYPE_ONE, this.form.INDUSTRY_TYPE_TWO, this.form.INDUSTRY_TYPE_THREE, this.form.INDUSTRY_TYPE_FOUR])
        this.corp_info_list = [{ CORPINFO_ID: this.form.CORP_INFO_ID, CORP_NAME: this.form.CORP_INFO_NAME }]
        this.form.CORPINFO_ID = this.form.CORP_INFO_ID
        this.risk_unit_list = [{ RISKUNIT_ID: this.form.RISK_UNIT_ID, RISKUNITNAME: this.form.RISK_UNIT_NAME }]
        if (!this.form.HIDDEN_TYPES) this.form.HIDDEN_TYPES = []
        if (this.form.HIDDEN_TYPE_ONE) this.form.HIDDEN_TYPES.push(this.form.HIDDEN_TYPE_ONE)
        if (this.form.HIDDEN_TYPE_TWO) this.form.HIDDEN_TYPES.push(this.form.HIDDEN_TYPE_TWO)
        if (this.form.HIDDEN_TYPE_THREE) this.form.HIDDEN_TYPES.push(this.form.HIDDEN_TYPE_THREE)
      }
    },
    confirm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          this.form.INDUSTRY_TYPE_ONE = this.form.INDUSTRY_TYPE ? this.form.INDUSTRY_TYPE[0] : ''
          this.form.INDUSTRY_TYPE_TWO = this.form.INDUSTRY_TYPE ? this.form.INDUSTRY_TYPE[1] : ''
          this.form.INDUSTRY_TYPE_THREE = this.form.INDUSTRY_TYPE ? this.form.INDUSTRY_TYPE[2] : ''
          this.form.INDUSTRY_TYPE_FOUR = this.form.INDUSTRY_TYPE ? this.form.INDUSTRY_TYPE[3] : ''
          this.form.CORP_INFO_ID = this.form.CORPINFO_ID

          this.form.HIDDEN_TYPE_ONE = this.form.HIDDEN_TYPES ? this.form.HIDDEN_TYPES[0] : ''
          this.form.HIDDEN_TYPE_TWO = this.form.HIDDEN_TYPES ? this.form.HIDDEN_TYPES[1] : ''
          this.form.HIDDEN_TYPE_THREE = this.form.HIDDEN_TYPES ? this.form.HIDDEN_TYPES[2] : ''

          requestFN(
            '/hiddenLibrary/init', this.form
          ).then((data) => {
            this.loading = false
            this.close()
            this.$emit('beforeClose', '')
            this.$message.success('操作成功')
          }).catch((e) => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    },
    resetForm() {
      this.form = {
        INDUSTRY_TYPE_ONE: '', // 行业类型1(数据字典)
        INDUSTRY_TYPE_TWO: '', // 行业类型2(数据字典)
        INDUSTRY_TYPE_THREE: '', // 行业类型3(数据字典)
        INDUSTRY_TYPE_FOUR: '', // 行业类型4(数据字典)
        HIDDEN_TYPES: '', // 检查隐患类型(数据字典)
        RISK_UNIT_ID: '', // 隐患区域(风险单元)
        RISK_UNIT_NAME: '', // 隐患区域名称(风险单元名称)
        HIDDEN_PART: '', // 隐患部位
        HIDDEN_DESCR: '', // 隐患描述
        CREATOR_ID: '', // 创建人
        CREATOR_NAME: '', // 创建人名称
        CREATED_TIME: '', // 创建时间
        OPERATOR_ID: '', // 更改人id
        OPERATOR_NAME: '', // 更改人名称
        OPERAT_TIME: '', // 更改时间
        INSPECTION_BASIS: '', // 检查依据
        CORP_INFO_ID: '', // 企业id
        ISDELETE: '', // 删除标志位
        STATUS: '', // 状态（0-编辑状态，1-已加入库）
        SOURCE: '0'
      }
    },
    close() {
      this.visible = false
      this.form = {
        HIDDEN_LIBRARY_ID: '',
        INDUSTRY_TYPE_ONE: '', // 行业类型1(数据字典)
        INDUSTRY_TYPE_TWO: '', // 行业类型2(数据字典)
        INDUSTRY_TYPE_THREE: '', // 行业类型3(数据字典)
        INDUSTRY_TYPE_FOUR: '', // 行业类型4(数据字典)
        INDUSTRY_TYPE: '',
        HIDDEN_TYPES: '', // 检查隐患类型(数据字典)
        RISK_UNIT_ID: '', // 隐患区域(风险单元)
        RISK_UNIT_NAME: '', // 隐患区域名称(风险单元名称)
        HIDDEN_PART: '', // 隐患部位
        HIDDEN_DESCR: '', // 隐患描述
        CREATOR_ID: '', // 创建人
        CREATOR_NAME: '', // 创建人名称
        CREATED_TIME: '', // 创建时间
        OPERATOR_ID: '', // 更改人id
        OPERATOR_NAME: '', // 更改人名称
        OPERAT_TIME: '', // 更改时间
        INSPECTION_BASIS: '', // 检查依据
        INSPECTION_INFO: '',
        CORP_INFO_ID: '', // 企业id
        ISDELETE: '', // 删除标志位
        STATUS: '', // 状态（0-编辑状态，1-已加入库）
        CORPINFO_ID: '',
        SOURCE: '0'
      }
    },
    getRiskUnit() {
      requestFN(
        '/riskUnit/getCorpInfoList', {}
      ).then((data) => {
        this.corp_info_list = data.list
      }).catch((e) => {
        console.log(e)
      })
    },
    getCorpInfo(query) {
      if (query !== '') {
        requestFN(
          '/riskUnit/getCorpInfoList', { CORP_NAME: query }
        ).then((data) => {
          this.corp_info_list = data.list
        }).catch((e) => {
          console.log(e)
        })
      } else {
        requestFN(
          '/riskUnit/getCorpInfoList', { CORP_NAME: query }
        ).then((data) => {
          this.corp_info_list = data.list
        }).catch((e) => {
          console.log(e)
        })
      }
    },
    getDicTree() {
      this.loading = true
      const loadingLock = []
      requestFN(
        '/dictionaries/listAllDictToParIdCurrent', { parentId: 'f2598ba72e864eadabf0ca4b664d26b9' }
      ).then((data) => {
        this.industry_type_list = JSON.parse(data.tree)
        if (loadingLock.length === 1) {
          this.loading = false
        } else {
          loadingLock.push(1)
        }
      }).catch((e) => {
        this.loading = false
      })
      requestFN(
        '/dictionaries/listAllDictToParIdCurrent', { parentId: 'b4a7325f96af40808ec64c85c162933d' }
      ).then((data) => {
        this.hidden_types_list = JSON.parse(data.tree)
        if (loadingLock.length === 1) {
          this.loading = false
        } else {
          loadingLock.push(1)
        }
      }).catch((e) => {
        console.log(e)
      })
    }
  }
}

</script>
