<script lang="ts" setup>
import type { LicenseApi } from '#/api/license/license';

import { ref } from 'vue';

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

import { getLicenseHistory } from '#/api/license/license';

import Detail from '../components/detail.vue';

const formDataList = ref<LicenseApi.License[]>();

const activeKey = ref(0);

const [Modal, modalApi] = useVbenModal({
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      formDataList.value = undefined;
      return;
    }
    // 加载数据
    const data = modalApi.getData<LicenseApi.License>();
    if (!data || !data.id) {
      return;
    }
    modalApi.setState({ loading: true });
    try {
      formDataList.value = await getLicenseHistory(data.id);
    } finally {
      modalApi.setState({ loading: false });
    }
  },
});
</script>

<template>
  <Modal
    title="License历史"
    class="w-2/3"
    :show-cancel-button="false"
    :show-confirm-button="false"
  >
    <a-tabs v-model:active-key="activeKey" tab-scroll>
      <a-tab-pane
        v-for="(item, index) in formDataList"
        :key="index"
        :tab="formatDateTime(item.applicationTime as string)"
      >
        <Detail :form-data="item" />
      </a-tab-pane>
    </a-tabs>
  </Modal>
</template>
