<!--
 * @Author: 曹捷
 * @Date: 2019-08-19 15:24:34
 * @LastEditors: 曹捷
 * @LastEditTime: 2021-03-23 16:01:49
 * @Description: file content
 -->
<template>
  <a-modal class="obit-modal-default" v-if="showModal" :maskClosable="false" :title="showTitle" v-model:visible="showModal"
    @cancel="closeDialog" @ok="saveInfo" cancelText="取消" okText="确认" width="40%">
    <a-form :inline="true" :model="formData" :rules="rules" ref="formRef" :label-col="{span: 4}"
      :wrapper-col="{span:18}">
      <a-form-item label="机构名称" name="orgName">
        <a-input :maxlength="20" placeholder="请输入机构名称" show-word-limit v-model:value="formData.orgName"></a-input>
      </a-form-item>
      <a-form-item label="机构编码" name="orgCode">
        <a-input :maxlength="20" placeholder="请输入机构编码" show-word-limit v-model:value="formData.orgCode"></a-input>
      </a-form-item>
      <a-form-item label="上级机构">
        <obitOrg v-model:value="formData.parentId" :disabled="addType==='edit'"></obitOrg>
      </a-form-item>
      <a-form-item label="简称" name="shortName">
        <a-input :maxlength="20" placeholder="请输入简称" show-word-limit v-model:value="formData.shortName"></a-input>
      </a-form-item>
      <a-form-item label="排序" name="orderNo">
        <a-input :maxlength="20" placeholder="排序" show-word-limit v-model:value.number="formData.orderNo"></a-input>
      </a-form-item>
      <a-form-item label="描述" name="orgDesc">
        <a-textarea :maxlength="100" placeholder="请输入描述" show-word-limit v-model:value="formData.orgDesc">
        </a-textarea>
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import { computed, inject, onMounted, reactive, ref, toRefs, watch } from 'vue'
import { util } from '../../../entery'
import obitOrg from '../../../components/obit-org/obit-org.vue'
export default {
  // inheritAttrs: false,
  components: { obitOrg },
  props: {
    open: {
      type: Boolean
    },
    addType: {
      type: String,
    },
    detailsInfo: {
      type: Object,
    },
  },
  setup (props, context) {
    let http = inject('http')

    let _data = reactive({
      showModal: props.open,
      formData: {},
      formRef: null,
      orgList: [],
      rules: {
        orgName: [
          { required: true, message: '请输入机构名称', trigger: 'blur' },
        ],
        orgCode: [
          { required: true, message: '请输入机构编码', trigger: 'blur' },
        ],
        shortName: [
          { required: true, message: '请输入简称', trigger: 'blur' },
        ],
        // appType: [
        //   { required: true, message: '请选择应用类型', trigger: 'blur', type: 'integer' },
        // ],
        orderNo: [
          { required: true, message: '请输入排序', trigger: 'blur', type: 'number' },
        ],
      },
    })
    let _computed = {
      showTitle: computed(() => props.addType === 'add' ? '新增机构' : '修改机构')
    }
    let _methods = {
      getOrgList () {
        http.getAllOrgTree().then((res) => {
          _data.orgList = res
        })
      },
      saveInfo () {
        _data.formRef.validate().then(() => {
          let param = util.util.cloneObj(_data.formData)
          http.saveSystemOrgin(param).then((res) => {
            context.emit('reload')
            _methods.closeDialog()
          })
        })
      },
      closeDialog () {
        util.util.cleanObj(_data.formData)
        context.emit('update:open', false)
      },
    }
    watch(() => props.open, (value) => {
      if (value) {

        _data.formData = util.util.cloneObj(props.detailsInfo)
        if (props.addType === 'add') {
          _data.formData.orderNo = 0
        }
      } else {
        util.util.cleanObj(_data.formData)
      }
    })
    onMounted(() => {
      _methods.getOrgList()
    })
    return {
      ..._computed,
      ...toRefs(_data),
      ..._methods
    }
  },
}
</script>
<style lang="less">
</style>