<script setup lang="ts">
import { ref } from "vue";
import { useDetail } from "/@/hooks/core/useDetail";
import { useAsync } from "/@/hooks/core/useAsync";
// import { ticketReturnColumns } from "./config/_details";
import { httpDetail, httpAdd } from "/@/api/purchase/ticketReturn";
import BasicDescriptions from "/@/components/BasicDescriptions";
import ReconciliationForm from "/@/components/ReconciliationForm";
import ReturnTciketForm from "./components/return-form.vue";
import Invoice from "/@/components/invoice";
import { useRouter } from "vue-router";
import { ticketReturnColumns } from "/@/utils/details/_purchase";

const { push } = useRouter();

const formRef = ref<InstanceType<typeof ReconciliationForm> | null>(null);

const { id, isDetail, title, collapses } = useDetail({
  baseName: "采购开票",
  collapseLen: 5
});

const { run: detail, data } = useAsync<Record<string, string>>({
  initalData: {}
});

const { run: create } = useAsync({
  success: () => push("/supply/supplyTicketReturn")
});

const requesetDetail = () => detail(httpDetail({ hpNo: id.value }));
const handleCreate = data => create(httpAdd(data));
if (isDetail.value) requesetDetail();
</script>

<template>
  <div class="padding__container">
    <ElTabs>
      <ElTabPane :label="title">
        <ElCollapse v-model="collapses">
          <ElCollapseItem name="1" :title="title">
            <!-- 回票详情 -->
            <basic-descriptions
              v-if="isDetail"
              :columns="ticketReturnColumns"
              :data="data"
            />
            <!-- 创建回票 -->
            <ReturnTciketForm v-else @create="handleCreate" />
          </ElCollapseItem>

          <div v-if="isDetail">
            <ElCollapseItem name="2" title="对账详情">
              <ReconciliationForm ref="formRef" readonly :id="data.payNo" />
            </ElCollapseItem>

            <ElCollapseItem name="3" title="发票详情">
              <Invoice type="purchase" :inv-number="data.invoiceNumber" />
            </ElCollapseItem>
          </div>
        </ElCollapse>
      </ElTabPane>
    </ElTabs>
  </div>
</template>
