<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '100px'">
      <el-form-item label="处理类别值" prop="categoryVal">
          <el-input-number v-model="dataForm.categoryVal"
                         controls-position="right"
                         :min="0">
        </el-input-number>
      </el-form-item>
      <el-form-item label="显示信息" prop="categoryLabel">
          <el-input v-model="dataForm.categoryLabel" placeholder="处理类别显示信息"></el-input>
      </el-form-item>
      <el-form-item label="类型编码" prop="categoryCode">
          <el-input v-model="dataForm.categoryCode" placeholder="类型编码"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="available">
          <el-select v-model="dataForm.available" clearable
                     placeholder="请选择">
            <el-option v-for="item in options"
                       :key="item.id"
                       :label="item.name"
                       :value="item.id">
            </el-option>
          </el-select>
      </el-form-item>
        <el-form-item label="机构类型"
                      prop="typeKeyList">
            <el-select v-model="dataForm.typeKeyList"
                       @change="changeOrgType"
                       multiple
                       placeholder="机构类型">
                <el-option v-for="item in secondOrgDictList"
                           :key="item.dictValue"
                           :label="item.dictName"
                           :value="item.dictValue">
                </el-option>
            </el-select>
        </el-form-item>
      <el-form-item label="排序"
                    prop="sort">
        <el-input-number v-model="dataForm.sort"
                         controls-position="right"
                         :min="0">
        </el-input-number>
      </el-form-item>
          <el-form-item label="乐观锁" prop="revision" v-if="false">
          <el-input v-model="dataForm.revision" placeholder="乐观锁"></el-input>
      </el-form-item>
          <el-form-item label="创建人" prop="createdBy" v-if="false">
          <el-input v-model="dataForm.createdBy" placeholder="创建人"></el-input>
      </el-form-item>
          <el-form-item label="创建时间" prop="createdTime" v-if="false">
          <el-input v-model="dataForm.createdTime" placeholder="创建时间"></el-input>
      </el-form-item>
          <el-form-item label="更新人" prop="updatedBy" v-if="false">
          <el-input v-model="dataForm.updatedBy" placeholder="更新人"></el-input>
      </el-form-item>
          <el-form-item label="更新时间" prop="updatedTime" v-if="false">
          <el-input v-model="dataForm.updatedTime" placeholder="更新时间"></el-input>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        categoryVal: '',
        categoryLabel: '',
        available: '',
        sort: '',
        revision: '',
        createdBy: '',
        createdTime: '',
        updatedBy: '',
        updatedTime: '',
        categoryCode: '',
        typeKeyList: []
      },
      secondOrgDictList: [],
      options: [{
        id: '0',
        name: '不可用'
      }, {
        id: '1',
        name: '可用'
      }]
    }
  },
  computed: {
    dataRule () {
      return {
        categoryVal: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        categoryLabel: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        available: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        sort: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        categoryCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        revision: [
          { required: false, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        createdBy: [
          { required: false, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        createdTime: [
          { required: false, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updatedBy: [
          { required: false, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updatedTime: [
          { required: false, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        typeKeyList: [
          { required: true, message: this.$t('validate.required'), trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
      this.getSecondOrgDicList()
    },
    // 改变机构类型
    changeOrgType (item) {
      var branch = ''
      var type_key = ''
      for (var i = 0; i < item.length; i++) {
        if (item[i] === 'branch_grid_party' || item[i] === 'branch_enterprise_party') {
          branch += (item[i] + ',')
        } else {
          type_key += (item[i] + ',')
        }
      }
      var key = branch + type_key
      if (key.length > 0) {
        key = key.substring(0, key.length - 1)
      }
      this.dataForm.typeKey = key
    },
    // 获取二级党委字典列表
    getSecondOrgDicList () {
      this.$http.get(`/sys/dict/listSimple/org_type`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.secondOrgDictList = res.data
      }).catch(() => { })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/events/handlecategory/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm.typeKey = ''
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
        if (this.dataForm.typeKey !== '' && this.dataForm.typeKey !== null) {
          this.dataForm.typeKeyList = this.dataForm.typeKey.split(',')
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/events/handlecategory/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
