<template>
  <BasicModal @register="registerModal" :title="title" width="50%" v-bind="$attrs" @ok="handleSubmit">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts" setup>
import { computed, ref, unref } from 'vue';
import { BasicModal, useModalInner } from '/@/components/Modal';
import { BasicForm, useForm } from '/@/components/Form/index';
import { formSchema } from '../MesMaterialCategory.data';
import { saveOrUpdate } from '../MesMaterialCategory.api';
const emit = defineEmits(['register', 'success']);
const isUpdate = ref(true);
const [registerForm, { resetFields, setFieldsValue, validate, setProps }] = useForm({ labelWidth: 120, schemas: formSchema, showActionButtonGroup: false });
const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  await resetFields();
  setModalProps({ confirmLoading: false, showCancelBtn: data?.showFooter, showOkBtn: data?.showFooter });
  isUpdate.value = !!data?.isUpdate;
  if (unref(isUpdate)) await setFieldsValue({ ...data.record });
  setProps({ disabled: !data?.showFooter });
});
const title = computed(() => (!unref(isUpdate) ? '新增物料分类' : '编辑物料分类'));
async function handleSubmit() {
  try {
    const values = await validate();
    setModalProps({ confirmLoading: true });
    await saveOrUpdate(values, isUpdate.value);
    closeModal();
    emit('success');
  } finally {
    setModalProps({ confirmLoading: false });
  }
}
</script>
