<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" @ok="handleSubmit">
    <BasicForm @register="registerForm"/>
  </BasicModal>
</template>

<script lang="ts" setup>
import {ref, computed, unref} from 'vue';
import {BasicModal, useModalInner} from '/@/components/Modal';
import {doctorFormSchema} from './doctor.data';
import {BasicForm, useForm} from '/@/components/Form';
import {set} from '/@/api/platform/system/controller/doctor';
const isUpdate = ref(true);
// 定义emit
const emit = defineEmits(['success', 'register']);
/**
 * 表单
 */
const [registerForm, {resetFields, setFieldsValue, validate}] = useForm({
  labelWidth: 100,
  schemas: doctorFormSchema,
  showActionButtonGroup: false,
  actionColOptions: {
    span: 23,
  },
});
/**
 * 表单参数
 */
const [registerModal, {setModalProps, closeModal}] = useModalInner(async (data) => {
  resetFields();
  setModalProps({confirmLoading: false});
  isUpdate.value = !!data?.isUpdate;
  console.log(data.record);
  if (unref(isUpdate)) {
    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>