<!-- 组织机构信息维护 -->
<template>
  <div>
    <dialog-edit :visible.sync="dialogVisible" :editModel="editModel" :form="form" :api="api" :width="width" :autoSave="autoSave"
                 :title="$t('enterpriseInfoManage.organization.organizationEditTitle')" :formRules="formRules" @onSaved="onSaved">
      <dym-form :componentList="componentList" :formValue.sync="form"></dym-form>
    </dialog-edit>
  </div>
</template>

<script>
import BaseEdit from '@/pages/baseEditPage'
import { regExp } from '@/validator/index'
import { DialogEdit, dymForm } from 'newepoch-components'
import organizationApi from '@/api/enterpriseInfoManage/organizationApi.js'
export default {
  extends: new BaseEdit(),
  name: 'organizationEdit',
  components: {
    dymForm,
    DialogEdit
  },
  data () {
    return {
      width: '600px',
      autoSave: false,
      componentList: [
        // 企业名称
        /* {
          value: 'enterpriseName',
          disabled: true,
          componentName: 'el-input',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.organization.enterpriseName,
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          cols: [24, 24, 24, 24],
          prop: 'enterpriseName'
        }, */
        // 上层组织机构
        {
          value: 'parentOrgName',
          disabled: true,
          componentName: 'el-input',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.organization.upperOrganization,
          // placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          placeholder: '',
          cols: [24, 24, 24, 24],
          prop: 'parentOrgName'
        },
        // 组织机构名称
        {
          value: 'orgName',
          disabled: false,
          componentName: 'el-input',
          maxlength: 100,
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.organization.organizationName,
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          cols: [24, 24, 24, 24],
          prop: 'orgName'
        },
        // 排序序号
        {
          value: 'orgSeq',
          disabled: false,
          componentName: 'el-input',
          maxlength: 3,
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.organization.sortNumber,
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          cols: [24, 24, 24, 24],
          prop: 'orgSeq'
        },
        // 职责描述
        {
          value: 'orgDescribe',
          disabled: false,
          componentName: 'el-input',
          type: 'textarea',
          maxlength: 2000,
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.organization.dutyDesc,
          placeholder: this.$i18n.messages[this.$i18n.locale].common.pleaseInput,
          cols: [24, 24, 24, 24],
          prop: 'orgDescribe'
        }
      ],
      formRules: {
        // 组织机构名称
        orgName: [{ required: true, message: this.$i18n.messages[this.$i18n.locale].regMsg.organization.pleaseOrgName, trigger: 'blur' },
          { validator: this.strLen, trigger: 'blur' }],
        // 排序序号
        orgSeq: [{ required: true, message: this.$i18n.messages[this.$i18n.locale].regMsg.organization.pleaseOrgSeq },
          { validator: this.orgSeqCheck, trigger: 'blur' }]
        // 职责描述
        // orgDescribe: [{ required: true, message: this.$i18n.messages[this.$i18n.locale].regMsg.organization.pleaseOrgDescribe, trigger: 'blur' }]
      }
    }
  },
  computed: {
    api () {
      return organizationApi.organTree
    }
  },
  methods: {
    strLen (rule, value, callback) {
      if (!this.UtilJs.isEmpty(value)) {
        if (value.length > 20) {
          callback(new Error(this.$i18n.messages[this.$i18n.locale].regMsg.organization.tooLong))
        }
        callback()
      } else {
        callback(new Error(this.$i18n.messages[this.$i18n.locale].regMsg.organization.pleaseOrgName))
      }
    },
    // 序号校验
    orgSeqCheck: function (rule, value, callback) {
      if (!this.UtilJs.isEmpty(value)) {
        if (regExp.sequence.test(value)) {
          callback()
        } else {
          callback(new Error(this.$i18n.messages[this.$i18n.locale].regMsg.organization.orgSeq))
        }
      } else {
        callback(new Error(this.$i18n.messages[this.$i18n.locale].regMsg.organization.pleaseOrgSeq))
      }
    },
    // 保存
    onSaved (submitForm) {
      let $this = this
      if ($this.editModel === 'E') {
        $this.api.update(submitForm.keyId, submitForm).then(res => {
          if (res && res.data) {
            $this.success(this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.organization.updateSuccess)
            $this.dialogVisible = false
            $this.$emit('operateSuccess', this.form)
          } else {
            $this.error(this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.organization.updateFailure)
          }
        })
      } else if ($this.editModel === 'C') {
        $this.api.addOrg(submitForm).then(res => {
          if (res && res.data) {
            $this.success(this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.organization.createSuccess)
            $this.dialogVisible = false
            $this.$emit('operateSuccess', res.data)
          } else {
            $this.error(this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.organization.createFailure)
          }
        })
      }
    }
  }
}
</script>

<style scoped>

</style>
