<script setup lang="ts">
import { ref } from "vue";
import { useRouter } from "vue-router";
import { useDetail } from "/@/hooks/core/useDetail";
import { useAsync } from "/@/hooks/core/useAsync";
import ChooseForm from "./component/choose-form.vue";
import { useUserInfo } from "/@/hooks/core/useUser";
import { usePermission } from "/@/hooks/core/usePermission";
import BasicDescriptions from "/@/components/BasicDescriptions";
// import ReconciliationForm from "/@/components/ReconciliationForm";

import Descrptions from "../descriptions/index.vue";
import { httpDetail, httpAdd, httpStatus } from "/@/api/postSupplierOrder/sendOrderPay";

import {
  DefaultApprovalNode,
  ReceiptApprovalNode
} from "/@/components/ApprovalNode";

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

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

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

const formRef = ref<InstanceType<typeof Descrptions> | 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("/postSupplierOrder/sendOrderPay")
  success: () => push("/postSupplierOrder/postSupplierOrderCollection?tab=sendOrderPay_collection")
});

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

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

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

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

if (isDetail.value) requestDetail();
</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"
              :columns="sendPurchpayColumns"
              :data="data"
            />
            
            <ChooseForm v-else @create="handleCreate" />
          </ElCollapseItem>

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

            <!-- 审批节点 -->
            <template v-if="!isSuperUser">
              <ElCollapseItem
                name="3"
                title="业务公司业务审核"
                v-if="
                  String(data.status) === '1' && hasPermissionWithCode('052') && !isSuperUser
                "
              >
                <DefaultApprovalNode
                  approve-value="2"
                  reject-value="6"
                  @approval="handleApproval"
                />
              </ElCollapseItem>

              <ElCollapseItem
                name="4"
                title="业务公司财务审核"
                v-if="
                  String(data.status) === '2' && hasPermissionWithCode('053') && !isSuperUser
                "
              >
                <DefaultApprovalNode
                  approve-value="3"
                  reject-value="7"
                  @approval="handleApproval"
                />
              </ElCollapseItem>

              <ElCollapseItem
                name="5"
                title="上传回执"
                v-if="String(data.status) === '3' && hasPermissionWithCode('049') && !isSuperUser"
              >
                <ReceiptApprovalNode
                  approve-value="4"
                  @approval="handleApproval"
                />
              </ElCollapseItem>
            </template>
          </div>
        </ElCollapse>
      </ElTabPane>
    </ElTabs>
  </div>
</template>
