<!--
 * @Author: 曹捷
 * @Date: 2019-08-19 15:24:34
 * @LastEditors: 曹捷
 * @LastEditTime: 2021-03-16 14:14:19
 * @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="configName">
        <a-input :maxlength="20" placeholder="请输入参数名称" show-word-limit v-model:value="formData.configName"></a-input>
      </a-form-item>
      <a-form-item label="参数键名" name="configKey">
        <a-input :maxlength="20" placeholder="请输入参数键名" show-word-limit v-model:value="formData.configKey"></a-input>
      </a-form-item>
      <a-form-item label="参数键值" name="configValue">
        <a-input :maxlength="20" placeholder="请输入参数键值" show-word-limit v-model:value="formData.configValue"></a-input>
      </a-form-item>
      <a-form-item label="系统内置" name="configType">
        <a-radio-group v-model:value="formData.configType" defaultValue="Y">
          <a-radio value="Y">是</a-radio>
          <a-radio value="N">否</a-radio>
        </a-radio-group>
      </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'
export default {
  // inheritAttrs: false,
  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,
      rules: {
        configName: [
          { required: true, message: '请输入参数名称', trigger: 'blur' },
        ],
        configKey: [
          { required: true, message: '请输入参数键名', trigger: 'blur' },
        ],
        configValue: [
          { required: true, message: '请输入参数键值', trigger: 'blur' },
        ]
      },
    })
    let _computed = {
      showTitle: computed(() => props.addType === 'add' ? '添加参数' : '修改参数')
    }
    let _methods = {
      saveInfo () {
        _data.formRef.validate().then(() => {
          let param = util.util.cloneObj(_data.formData)
          http.saveSystemParams(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)
      } else {
        util.util.cleanObj(_data.formData)
      }
    })
    return {
      ..._computed,
      ...toRefs(_data),
      ..._methods
    }
  },
}
</script>
<style lang="less">
</style>