<template>
  <basicModal
    @register="modalRegister"
    ref="modalRef"
    class="basicModal basicFormModal"
    @on-ok="handleSubmit"
    @on-close="handleClose"
  >
    <template #default>
      <n-form
        class="ls-form"
        ref="formRef"
        :model="formData"
        label-placement="left"
        label-width="85px"
      >
        <n-form-item
          label="参数名称"
          path="name"
          :rule="{ required: true, message: '请输入参数名称', trigger: 'blur' }"
        >
          <n-input v-model:value="formData.name" placeholder="请输入名称" clearable />
        </n-form-item>
        <n-form-item
          label="参数类型"
          path="type"
          :rule="{ required: true, message: '请选择参数类型', trigger: 'change' }"
        >
          <n-select
            v-model:value="formData.type"
            placeholder="请选择参数类型"
            :options="optionData.typeList"
          />
        </n-form-item>
        <n-form-item
          label="参数编码"
          path="code"
          :rule="{ required: true, message: '请输入参数编码', trigger: 'blur' }"
        >
          <n-input v-model:value="formData.code" placeholder="请输入参数编码" clearable />
        </n-form-item>
        <n-form-item
          label="参数值"
          path="code"
          :rule="{ required: true, message: '请输入参数值', trigger: 'blur' }"
        >
          <n-input v-model:value="formData.value" placeholder="请输入参数值" clearable />
        </n-form-item>
        <n-form-item label="参数状态" path="status">
          <n-radio-group v-model:value="formData.status" name="status">
            <n-radio :value="1">正常</n-radio>
            <n-radio :value="2">禁用</n-radio>
          </n-radio-group>
        </n-form-item>
        <n-form-item label="排序" path="sort">
          <n-input-number v-model:value="formData.sort" />
        </n-form-item>
        <n-form-item label="备注" path="note">
          <n-input
            v-model:value="formData.note"
            type="textarea"
            placeholder="请输入备注"
            clearable
          />
        </n-form-item>
      </n-form>
    </template>
  </basicModal>
</template>
<script lang="ts" setup>
  import { getParamDetail, paramAdd, paramUpdate } from '@/api/data/param';
  import { onMounted, reactive, ref } from 'vue';
  import { useMessage } from 'naive-ui';
  import { useModal } from '@/components/Modal';

  const emit = defineEmits(['success', 'update:visible']);
  const formRef = ref();

  /**
   * 定义表单参数
   */
  const message = useMessage();
  const formData = reactive({
    id: '',
    name: '',
    type: undefined,
    code: '',
    value: '',
    status: 1,
    sort: 0,
    note: '',
  });

  /**
   * 定义接收的参数
   */
  const props = defineProps({
    visible: {
      type: Boolean,
      required: true,
      default: false,
    },
    paramId: {
      type: Number,
      required: true,
      default: 0,
    },
  });
  const [modalRegister, { openModal, setSubLoading }] = useModal({
    title: props.paramId ? '编辑参数' : '添加参数',
    subBtuText: '确定',
    width: 600,
  });
  const optionData = {
    typeList: [
      { label: '系统', value: 0 },
      { label: '业务', value: 1 },
    ],
  };
  /**
   * 执行提交
   */
  const handleSubmit = async () => {
    formRef.value
      .validate()
      .then(async () => {
        props.paramId ? await paramUpdate(formData) : await paramAdd(formData);
        message.success('操作成功');
        emit('update:visible', false);
        emit('success');
      })
      .catch((error) => {
        setSubLoading(false);
      });
  };

  /**
   * 关闭窗体
   */
  const handleClose = () => {
    emit('update:visible', false);
  };

  /**
   * 设置表单数据
   */
  const setFormData = async () => {
    const data = await getParamDetail(props.paramId);
    for (const key in formData) {
      if (data[key] != null && data[key] != undefined) {
        //@ts-ignore
        formData[key] = data[key];
      }
    }
  };

  /**
   * 钩子函数
   */
  onMounted(() => {
    if (props.paramId) {
      setFormData();
    }
  });
  //导出方法
  defineExpose({
    openModal,
  });
</script>
