<template>
  <basicModal @register="modalRegister" ref="modalRef" @ok="okModal">
    <template #default>
      <BasicForm @register="registerForm" class="pt-6 basicForm" />
    </template>
  </basicModal>
</template>

<script lang="ts" setup>
  import { ref, computed } from 'vue';
  import { BasicForm, useForm } from '@/components/Form/index';
  import { basicModal, useModal } from '@/components/Modal';
  import { addDictData, editDictData } from '@/api/system/dictionary';
  import type { FormSchema } from '@/components/Form/index';
  import { ElMessage } from 'element-plus';

  const emit = defineEmits(['change', 'register']);

  const message = ElMessage;

  const props = defineProps({
    title: {
      type: String,
      default: '添加字典项',
    },
  });

  //表单项配置
  const schemas: FormSchema[] = [
    {
      field: 'dictCode',
      hidden: true,
    },
    {
      field: 'dictType',
      hidden: true,
    },
    {
      field: 'dictLabel',
      component: 'Input',
      label: `字典项名称`,
      componentProps: {
        placeholder: `请输入字典项名称`,
      },
      rules: [
        {
          required: true,
          message: `字典项名称不能为空`,
          trigger: ['blur'],
        },
      ],
    },
    {
      field: 'dictValue',
      component: 'Input',
      label: `字典项值`,
      componentProps: {
        placeholder: `请输入字典项值`,
      },
      rules: [
        {
          required: true,
          message: `字典项值不能为空`,
          trigger: ['blur'],
        },
      ],
    },
    {
      field: 'dictSort',
      component: 'InputNumber',
      label: '排序',
      componentProps: {
        placeholder: '请输入排序',
      },
    },
    {
      field: 'status',
      component: 'RadioGroup',
      label: '状态',
      defaultValue: '0',
      componentProps: {
        options: [
          {
            label: '正常',
            value: '0',
          },
          {
            label: '停用',
            value: '1',
          },
        ],
      },
    },
    {
      field: 'remark',
      component: 'Input',
      label: '备注',
      componentProps: {
        placeholder: '请输入备注',
        type: 'textarea',
      },
    },
  ];

  const title = ref(props.title);

  //创建form
  const [registerForm, { submit, setFieldsValue, getFieldsValue }] = useForm({
    collapsed: false,
    labelWidth: 100,
    layout: 'horizontal',
    submitButtonText: '确定',
    showActionButtonGroup: false,
    schemas,
  });

  //弹窗
  const [modalRegister, { openModal, closeModal, setSubLoading, setProps }] = useModal({
    title,
    subBtuText: '确定',
    width: 450,
  });

  //提交
  async function okModal() {
    const formRes = await submit();
    if (formRes) {
      const formData = getFieldsValue();
      const params = {
        ...formData,
      };
      if (formData.dictCode) {
        editDictData(params)
          .then(() => {
            message.success('修改成功');
            emit('change');
            closeModal();
          })
          .catch(() => {
            setSubLoading(false);
          });
      } else {
        addDictData(params)
          .then(() => {
            message.success('添加成功');
            emit('change');
            closeModal();
          })
          .catch(() => {
            setSubLoading(false);
          });
      }
    } else {
      message.error('验证失败，请填写完整信息');
      setSubLoading(false);
    }
  }

  //导出方法
  defineExpose({
    openModal,
    closeModal,
    setFieldsValue,
    setProps,
  });
</script>
