<script setup lang="ts">
import { ref } from "vue";
import { useDetail } from "/@/hooks/core/useDetail";
import { useAsync } from "/@/hooks/core/useAsync";
import BasicDescriptions from "/@/components/BasicDescriptions";
// import ReconciliationForm from "/@/components/ReconciliationForm";
import Descriptions from "../descriptions/index.vue"
import ChooseForm from "./component/choose-form.vue";
import { ReceiptApprovalNode } from "/@/components/ApprovalNode";
import { useRouter } from "vue-router";
import { useUserInfo } from "/@/hooks/core/useUser";
import { usePermission } from "/@/hooks/core/usePermission";
// import { httpOldDetail as httpPayDetail } from  "/@/api/postSupplierOrder/purchPayRelive";

import { httpOldDetail as httpPayDetail } from "/@/api/purchase/orderPay";

import {
  sendPurchpayColumns,
  sendPurchpayReliveColumns
} from "/@/utils/details/_purchase";

import {
  httpDetail,
  httpOldAdd as httpAdd,
  httpOldStatus as httpStatus
} from "/@/api/purchase/purchPayRelive";

const { push } = useRouter();
const pageName = "sendOrderPayReliveDetail";

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

const { isSuperUser } = useUserInfo();

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

const { id, isDetail, title, collapses } = useDetail({
  baseName: "采购付款解除",
  collapseLen: 5
});

const { run: detail, data } = useAsync<Record<string, string>>({
  initalData: {},
  success: ({ orderCode }) => payDetailRun(httpPayDetail({ DzNo: orderCode }))
});

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

const { run: create } = useAsync({
  // success: () => push("/postSupplierOrder/sendOrderPayRelive")
  success: () => push("/postSupplierOrder/postSupplierOrderCollection?tab=sendOrderPayRelive_collection")
});

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

const requesetDetail = () => detail(httpDetail({ returnCode: id.value }));
const handleCreate = data => create(httpAdd(data));
const handleApproval = data =>
  approval(httpStatus({ returnCode: id.value, ...data }));

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

<template>
  <div class="padding__container bg-white">
    <ElTabs>
      <ElTabPane :label="title">
        <ElCollapse v-model="collapses">
          <ElCollapseItem name="1" :title="title">
            <!-- 解除付款详情 -->
            <BasicDescriptions
              v-if="isDetail"
              class="mb-2"
              :columns="sendPurchpayReliveColumns"
              :data="data"
            />

            <!-- 创建：选择付款解除 -->
            <ChooseForm v-else @create="handleCreate" />
          </ElCollapseItem>

          <ElCollapseItem v-if="isDetail" name="4" title="运单付款详情">
            <!-- 采购付款详情 -->
            <BasicDescriptions
              :columns="sendPurchpayColumns"
              :data="payDetailData"
            />
          </ElCollapseItem>

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

            <ElCollapseItem
              name="3"
              title="业务公司财务审核"
              v-if="
                String(data.status) === '1' &&
                !isSuperUser &&
                hasPermissionWithCode('053')
              "
            >
              <ReceiptApprovalNode
                no-time
                has-remark
                approve-value="2"
                reject-value="3"
                img-prop="returnImg"
                @approval="handleApproval"
              />
            </ElCollapseItem>
          </div>
        </ElCollapse>
      </ElTabPane>
    </ElTabs>
  </div>
</template>
