<!--
 * @Author: 曹捷
 * @Date: 2019-08-19 15:24:34
 * @LastEditors: 曹捷
 * @LastEditTime: 2021-04-20 09:25:23
 * @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="codeField">
        <a-input :maxlength="20" :disabled="addType === 'edit'" placeholder="请输入字典类型" show-word-limit
          v-model:value="formData.codeField"></a-input>
      </a-form-item>
      <a-form-item label="是否通用" name="ifAll" v-if="addType==='add' && hasNomal">
        <a-switch checked-children="平台通用" un-checked-children="仅当前系统" v-model:checked="formData.ifAll" />
      </a-form-item>
      <a-form-item label="字典描述" name="codeDesc">
        <a-textarea :maxlength="100" placeholder="请输入字典描述" show-word-limit v-model:value="formData.codeDesc">
        </a-textarea>
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import { computed, inject, reactive, ref, toRefs, watch } from 'vue'
import { util } from '../../../entery'
import { useStore } from 'vuex'
export default {
  // inheritAttrs: false,
  props: {
    open: {
      type: Boolean
    },
    addType: {
      type: String,
    },
    detailsInfo: {
      type: Object,
    },
  },
  setup (props, context) {
    let http = inject('http')
    let store = useStore()

    let _data = reactive({
      showModal: props.open,
      formData: {},
      formRef: null,
      rules: {
        codeField: [
          { required: true, message: '请输入字典名称', trigger: 'blur' }
        ],
        codeDesc: [
          { required: true, message: '请输入字典描述', trigger: 'blur' }
        ]
      },
    })
    let _computed = {
      showTitle: computed(() => props.addType === 'add' ? '新增字典' : '修改字典'),
      hasNomal: computed(() => store.state.user.choiceApp.appCode === 'SYSTEM')
    }
    let _methods = {
      saveInfo () {
        _data.formRef.validate().then(() => {
          let param = util.util.cloneObj(_data.formData)
          http.saveSystemSysCode(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 && props.addType === 'edit') {
        _data.formData = util.util.cloneObj(props.detailsInfo)
      } else {
        util.util.cleanObj(_data.formData)
      }
    })
    return {
      ..._computed,
      ...toRefs(_data),
      ..._methods
    }
  },
}
</script>
<style lang="less">
</style>