<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    showFooter
    :title="getTitle"
    width="500px"
    @ok="handleSubmit"
  >
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts" setup>
import {ref, computed, unref} from 'vue';
import {BasicForm, useForm} from '/@/components/Form/index';
import {formSchema} from './dictdata.data';
//import {set} from '/@/api/platform/system/controller/dictdata';
import {BasicModal, useModalInner} from '/@/components/Modal';

const emit = defineEmits(['success', 'register']);
const isUpdate = ref(true);
const [registerForm, {resetFields, setFieldsValue, validate}] = useForm({
  labelWidth: 100,
  schemas: formSchema,
  showActionButtonGroup: false,
});

const [registerModal, {setModalProps, closeModal}] = useModalInner(async (data) => {
  await resetFields();
  setModalProps({confirmLoading: false});
  isUpdate.value = !!data?.isUpdate;

  if (unref(isUpdate)) {
    await setFieldsValue({
      ...data.record,
    });
  }
});

const getTitle = computed(() => (!unref(isUpdate) ? '新增字典项' : '编辑字典项'));

async function handleSubmit() {
  try {
    const values = await validate();
    setModalProps({confirmLoading: true});
    //await set(values);
    closeModal();
    emit('success');
  } finally {
    setModalProps({confirmLoading: false});
  }
}


</script>
