<!--
 * @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="dictName">
        <a-input :maxlength="20" :disabled="addType === 'edit'" placeholder="请输入字典名称" show-word-limit
          v-model:value="formData.dictName"></a-input>
      </a-form-item>
      <a-form-item label="字典类型" name="dictType">
        <a-input :maxlength="20" placeholder="请输入字典类型" show-word-limit v-model:value="formData.dictType">
        </a-input>
      </a-form-item>
      <a-form-item label="是否全局" name="appId">
        <a-radio-group v-model:value="isOverall" defaultValue="0">
          <a-radio value="0">是</a-radio>
          <a-radio value="1">否</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="状态" name="status">
        <obit-dict dictCode="sys_normal_disable" type="radio" placeholder="请选择" v-model:value="formData.status">
        </obit-dict>
      </a-form-item>
      <a-form-item label="备注" name="remark">
        <a-textarea :maxlength="100" placeholder="请输入备注" show-word-limit v-model:value="formData.remark">
        </a-textarea>
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import { computed, inject, reactive, 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,
    },
    appId: {
      type: String
    },
  },
  setup (props, context) {
    let http = inject('http')
    let store = useStore()
    let _data = reactive({
      showModal: props.open,
      formData: {},
      isOverall: '',
      formRef: null,
      rules: {
        dictName: [
          { required: true, message: '请输入字典名称', trigger: 'blur' }
        ],
        dictType: [
          { required: true, message: '请输入字典类型', trigger: 'blur' }
        ],
        isOverall: [
          { 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(() => {
          _data.formData.appId = _data.isOverall == '0' ? '0' : props.appId
          let param = util.util.cloneObj(_data.formData)
          http.saveSystemSysCode(param, props.addType).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)
        _data.isOverall = _data.formData.appId == '0' ? '0' : '1'
      } else {
        _data.formData.status = '0'
        _data.isOverall = '0'
      }
    })
    return {
      ..._computed,
      ...toRefs(_data),
      ..._methods
    }
  },
}
</script>
<style lang="less">
</style>