<template>
  <BasicModal
    v-bind="$attrs"
    destroyOnClose
    @register="registerModal"
    :title="getTitle"
    :width="width"
    @ok="handleSubmit"
    :showFooter="true"
  >
    <BasicForm @register="registerForm" layout="vertical" class="!px-6 !pt-4" />
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref, reactive, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { dataFormSchemaVascularAccess } from './data';
  import {
    archivesVascularAccessDetail,
    archivesVascularAccessEdit,
    archivesVascularAccessAdd,
  } from '/@/api/biz/archives/vascularAccessApi';
  import { listDictModel } from '/@/api/common';
  import { formatDictValue } from '/@/utils';
  import { useUserStore } from '@/store/modules/user';
  import dayjs from 'dayjs';

  const emit = defineEmits(['success', 'register']);
  const userStore = useUserStore();
  const getTitle = ref(`血管通路`);
  const width = '35%';
  const isUpdate = ref(false);
  const rowId = ref();
  const patientBasicId = ref();
  const bizDictOptions = reactive({
    accessType: [],
  });
  const accessType = ref();

  const { createMessage } = useMessage();
  const [registerForm, { setFieldsValue, resetFields, validate }] = useForm({
    schemas: dataFormSchemaVascularAccess,
    showActionButtonGroup: false,
    baseColProps: {
      span: 24,
    },
  });
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async data => {
    console.log('🚀 ~ file: FormModal.vue:49 ~ data:', data);
    await resetFields();
    setModalProps({ confirmLoading: false });
    bizDictOptions.accessType = await listDictModel({ dictCode: 'va_type' });
    isUpdate.value = !!data?.isUpdate;
    patientBasicId.value = data.record.id;
    if (unref(isUpdate)) {
      const resData = await archivesVascularAccessDetail(patientBasicId.value);
      rowId.value = resData.id;
      patientBasicId.value = resData.patientBasicId;
      resData.accessType = resData.type;
      accessType.value = formatDictValue(bizDictOptions.accessType, resData.type);
      await setFieldsValue({
        ...resData,
      });
    } else {
      accessType.value = formatDictValue(bizDictOptions.accessType, data.record.accessType);
      await setFieldsValue({
        setUpHospital: userStore.getTenant?.name,
        setUpTime: dayjs(),
        accessType: data.record.accessType,
      });
    }
    getTitle.value = `${isUpdate.value ? '编辑' : '新建'}${accessType.value}   ( ${
      data.record.name
    } | ${data.record.gender} | ${data.record.age} )`;
  });

  // 提交按钮事件
  async function handleSubmit() {
    try {
      const values = await validate();
      setModalProps({ confirmLoading: true });
      values.patientBasicId = patientBasicId.value;
      values.type = values.accessType;
      delete values.accessType;
      !unref(isUpdate)
        ? await archivesVascularAccessAdd({ ...values })
        : await archivesVascularAccessEdit({ ...values, id: rowId.value });
      !unref(isUpdate) ? createMessage.success('新增成功！') : createMessage.success('编辑成功！');
      closeModal();
      emit('success', {
        isUpdate: unref(isUpdate),
        values: { ...values, id: rowId.value },
      });
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>
