<script setup lang="ts">
import { ref } from "vue";
import { useDetail } from "/@/hooks/core/useDetail";
import { useAsync } from "/@/hooks/core/useAsync";
import { httpDetail, httpAdd, httpStatus } from "/@/api/purchase/ticketReturn";
import BasicDescriptions from "/@/components/BasicDescriptions";
import ReconciliationForm from "/@/components/ReconciliationForm";
import Invoice from "/@/components/invoice";
import { useRouter } from "vue-router";
import { useUserInfo } from "/@/hooks/core/useUser";
import { usePermission } from "/@/hooks/core/usePermission";
import { ticketReturnColumns } from "/@/utils/details/_purchase";

const pageName = "supplierTicketReturnDetail";
// { code: "020", name: "买方公司财务审核" },
const { hasPermissionWithCode } = usePermission(pageName);

const { push } = useRouter();

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

const { isSuperUser } = useUserInfo();

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

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

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

const { run: approval } = useAsync({
  success: () => refresh()
});

function refresh() {
  requesetDetail();
  formRef.value && formRef.value.refresh();
}

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

function handleApproval(data) {
  const params = {
    ...data,
    hpNo: id.value
  };

  approval(httpStatus(params));
}

if (isDetail.value) requesetDetail();
</script>

<template>
  <div class="padding__container">
    <ElTabs>
      <ElTabPane :label="title">
        <ElCollapse v-model="collapses">
          <ElCollapseItem name="1" :title="title">
            <!-- 回票详情 -->
            <BasicDescriptions
              v-if="isDetail"
              :columns="ticketReturnColumns"
              :data="data"
            />
          </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>
