<script setup lang="ts">
import { ref } from 'vue';
import { useVbenModal } from '@vben/common-ui';
import { cloneDeep } from '@vben/utils';
import { useVbenForm } from '#/adapter/form';
import { carChargeInfo, carChargeRefund } from '#/api/property/carCharge';
import { defaultFormValueGetter, useBeforeCloseDiff } from '#/utils/popup';
import { modalSchemaRefund } from './data';
import { renderDict } from "#/utils/render";
import { Descriptions, DescriptionsItem, Divider } from "ant-design-vue";
import type { CarChargeVO } from "#/api/property/carCharge/model";

const emit = defineEmits<{ reload: [] }>();

const isUpdate = ref(false);
const record = ref<CarChargeVO>();
const [BasicForm, formApi] = useVbenForm({
  commonConfig: {
    formItemClass: 'col-span-2',
    labelWidth: 80,
    componentProps: {
      class: 'w-full',
    }
  },
  schema: modalSchemaRefund(),
  showDefaultActions: false,
  wrapperClass: 'grid-cols-2',
});

const { onBeforeClose, markInitialized, resetInitialized } = useBeforeCloseDiff(
  {
    initializedGetter: defaultFormValueGetter(formApi),
    currentGetter: defaultFormValueGetter(formApi),
  },
);

const [BasicModal, modalApi] = useVbenModal({
  class: 'w-[550px]',
  fullscreenButton: false,
  onBeforeClose,
  onClosed: handleClosed,
  onConfirm: handleConfirm,
  onOpenChange: async (isOpen) => {
    if (!isOpen) return null;
    modalApi.modalLoading(true);
    const { id } = modalApi.getData() as { id?: number | string };
    isUpdate.value = !!id;
    if (id) {
      record.value = await carChargeInfo(id);
      await formApi.setValues(record.value);
    }
    await markInitialized();
    modalApi.modalLoading(false);
  },
});

async function handleConfirm() {
  try {
    modalApi.lock(true);
    const { valid } = await formApi.validate();
    if (!valid) return;
    const data = cloneDeep(await formApi.getValues());
    // 可根据 carCharge 业务需要补充字段
    await carChargeRefund(data);
    resetInitialized();
    emit('reload');
    modalApi.close();
  } catch (error) {
    console.error(error);
  } finally {
    modalApi.lock(false);
  }
}

async function handleClosed() {
  await formApi.resetForm();
  resetInitialized();
}
</script>

<template>
  <BasicModal title="退费">
    <Descriptions v-if="record" size="small" :column="2" :labelStyle="{width:'80px'}">
      <DescriptionsItem label="车牌号">
        {{ record?.carNumber }}
      </DescriptionsItem>
      <DescriptionsItem label="收费项目">
        {{ record?.costItemsId }}
      </DescriptionsItem>
      <DescriptionsItem label="计费起始">
        {{ record?.starTime }}
      </DescriptionsItem>
      <DescriptionsItem label="计费结束">
        {{ record?.endTime }}
      </DescriptionsItem>
      <DescriptionsItem label="车位">
        {{ record?.location }}
      </DescriptionsItem>
      <DescriptionsItem label="业主">
        {{ record?.personId }}
      </DescriptionsItem>
      <DescriptionsItem label="说明" :span="2">
        {{ record?.remark }}
      </DescriptionsItem>
    </Descriptions>
    <Divider/>
    <BasicForm/>
  </BasicModal>
</template>