<script lang="ts" setup>
import { ref } from 'vue';

import { useVbenModal } from '@vben/common-ui';

import { message, Alert } from 'ant-design-vue';

import { useVbenForm } from '#/adapter/form';

import {createDoctor, updateDoctor} from '../api';
import { infoModalFormProps } from '../config/form';



const isUpdate = ref(false);
const gridApi = ref();

const [Form, formApi] = useVbenForm(infoModalFormProps);

const [Modal, modalApi] = useVbenModal({
  closeOnClickModal: false,
  fullscreenButton: false,
  draggable: true,
  onCancel() {
    modalApi.close();
  },
  onConfirm: async () => {
    formApi.validate().then(async (e: any) => {
      if (e.valid) {
        const values = await formApi.getValues();
        modalApi.setState({ loading: true, confirmLoading: true });
        const submitApi = isUpdate.value ? updateDoctor : createDoctor;
        submitApi(values)
          .then(() => {
            message.success('保存成功');
            gridApi.value?.reload();
            modalApi.close();
          })
          .finally(() => {
            modalApi.setState({ loading: false, confirmLoading: false });
          });
      }
    });
    await formApi.validateAndSubmitForm();
  },
  onOpenChange(isOpen: boolean) {
    gridApi.value = isOpen ? modalApi.getData()?.gridApi : null;
    if (isOpen) {
      const { values, update } = modalApi.getData<Record<string, any>>();
      if (values) {
        isUpdate.value = update;
        // getPharmacistEditInfoApi(values.id).then((res) => {
        //   formApi.setValues(res);
        // });
        formApi.setValues(values);

        // const img = 'https://img2.baidu.com/it/u=3046024791,55863177&fm=253&fmt=auto&app=138&f=JPEG';
        // formApi.setValues({
        //   name: '测试药师',
        //   mobile: '15110000000',
        //   idcard: '134111000022225555',
        //   type: 1,
        //   sign_type: 2,
        //   qualification: img,
        //   practicing: img,
        //   title: img,
        //   card_up: img,
        //   card_down: img,
        //   sign_image: 'http://demo-1.nailaoyun.cn/p/image/qm1.png',
        //   avatar: img,
        // });
      } else {
        // const img = 'https://img2.baidu.com/it/u=3046024791,55863177&fm=253&fmt=auto&app=138&f=JPEG';
        // formApi.setValues({
        //   name: '测试药师',
        //   mobile: '15110000000',
        //   idcard: '134111000022225555',
        //   type: 1,
        //   sign_type: 2,
        //   qualification: img,
        //   practicing: img,
        //   title: img,
        //   card_up: img,
        //   card_down: img,
        //   sign_image: 'http://demo-1.nailaoyun.cn/p/image/qm1.png',
        //   avatar: img,
        // });
      }

    }
  },
});
</script>
<template>
  <Modal
    :title="`${isUpdate === true ? '编辑' : '新增'}医生`"
    class="w-[60%]"
  >
    <!--    <Alert-->
    <!--      description="* 添加的时候确认好药师信息，暂时不支持修改"-->
    <!--      message="重要提示"-->
    <!--      type="error"-->
    <!--    />-->
    <Form />
  </Modal>
</template>
