<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 { dataFormSchemaAccessReturn } from './data';
  import {
    archivesAccessReturnAdd,
    archivesAccessReturnDetail,
  } from '/@/api/biz/archives/accessReturnApi';
  import { listDictModel } from '/@/api/common';
  import { formatDictValue } from '/@/utils';
  import dayjs from 'dayjs';

  const emit = defineEmits(['success', 'register']);

  const getTitle = ref(`通路转归`);
  const width = '35%';
  const isUpdate = ref(false);
  const rowId = ref();
  const vascularAccessId = ref();
  const bizDictOptions = reactive({
    accessType: [],
  });

  const { createMessage } = useMessage();
  const [registerForm, { setFieldsValue, resetFields, validate }] = useForm({
    labelWidth: 100,
    schemas: dataFormSchemaAccessReturn,
    showActionButtonGroup: false,
    baseColProps: {
      span: 12,
    },
  });
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async data => {
    await resetFields();
    setModalProps({ confirmLoading: false });
    bizDictOptions.accessType = await listDictModel({ dictCode: 'va_type' });
    isUpdate.value = !!data?.isUpdate;
    getTitle.value = `通路转归 ( ${data.record.name} | ${data.record.gender} | ${data.record.age} )`;
    vascularAccessId.value = data.accessId;
    console.log('🚀 ~ file: FormModalReturn.vue:56 ~  data.record:', data.record);
    if (unref(isUpdate)) {
      const resData = await archivesAccessReturnDetail(vascularAccessId.value);
      rowId.value = resData.id;
      resData.accessType = formatDictValue(bizDictOptions.accessType, resData.accessType);
      resData.accessSetUpTime = dayjs(resData.accessSetUpTime).format('YYYY-MM-DD');
      resData.returnTime = resData.returnTime || dayjs().format('YYYY-MM-DD');
      resData.useTime = dayjs(resData.returnTime).diff(resData.accessSetUpTime, 'day') + '天';
      await setFieldsValue({
        ...resData,
      });
    }
  });

  // 提交按钮事件
  async function handleSubmit() {
    try {
      const values = await validate();
      setModalProps({ confirmLoading: true });
      values.vascularAccessId = vascularAccessId.value;
      await archivesAccessReturnAdd({ ...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>
