<script setup lang="ts">
import { ref } from "vue";
import { useDetail } from "/@/hooks/core/useDetail";
import { useAsync } from "/@/hooks/core/useAsync";
import { httpDetail, httpAdd, httpOldAdd } from "/@/api/purchase/orderPay";
import BasicDescriptions from "/@/components/BasicDescriptions";
import ReconciliationForm from "/@/components/ReconciliationForm";
import ChooseForm from "./component/choose-form.vue";
import { useRouter } from "vue-router";
import { purchpayColumns, purchpayColumnsNew  } from "/@/utils/details/_purchase";
import { PURCHASE_OLD_APPLY_DATE } from "/@/utils/constants"


const { push } = useRouter();

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



const isOld = ref(false)
const { id, isDetail, title, collapses } = useDetail({
  baseName: "卖出回款",
  collapseLen: 5
});

const { run: detail, data } = useAsync<Record<string, string>>({ 
  initalData: {},
  success({ payNo = '' } = {}){
    const date = payNo.slice(3, 9)
    isOld.value = Number(date) < PURCHASE_OLD_APPLY_DATE
  }
});



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

const requesetDetail = () => detail(httpDetail({ dzNo: id.value }));
const handleCreate = (data, isOld) => create(isOld ? httpOldAdd(data) : 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">
            <!-- 付款详情 -->
            <BasicDescriptions
              :data="data" 
              v-if="isDetail"
              :columns="(!data.cgdlist || data.cgdlist.length === 0) ? purchpayColumns : purchpayColumnsNew" 
            />

            <!-- 创建：选择对账单 -->
            <ChooseForm v-else @create="handleCreate" />
          </ElCollapseItem>

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