<script setup lang="ts">
import { computed, onMounted, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import RelatedOrder from "../basic-claim/related-order.vue";
import ReceiptPayment from "../basic-claim/receipt-payment.vue";
import { httpPayAdd, httpInfo } from "/@/api/InvoiceSales/capitalClaim";
import { useResponseHandle, useAsync } from "/@/hooks";
import { useUserInfo } from "/@/hooks/core/useUser";
import { usePermission } from "/@/hooks/core/usePermission";

const pageName = "claimDetail";
const { query } = useRoute();
const { push } = useRouter();
const tradNo = computed(() => query.id as string);

const { hasPermissionWithCode } = usePermission(pageName);
const { isSuperUser } = useUserInfo();

const treadOrderList = ref<Array<Record<string, string>>>([]);
const tradeDetail = ref<Record<string, string>>({});
const responseHandle = useResponseHandle();

const { run: createSaleRun } = useAsync({
  success: () => push("/InvoiceSales/capitalClaim")
});

//新增销售订单付款
const requesetCreateSalePay = orderArr => {
  createSaleRun(
    httpPayAdd({
      tradNo: tradNo.value,
      orderArr
    })
  );
};

async function requesetTradeDetail() {
  const { data, message, code } = await httpInfo({
    tradNo: tradNo.value
  });

  responseHandle({
    code,
    message,
    handler: () => (tradeDetail.value = data)
  });
}

function initalData() {
  if (!tradNo.value) return;
  requesetTradeDetail();
}

//传入id初始化详情
onMounted(() => initalData());
</script>
<template>
  <div>
    <ReceiptPayment :trade-info="tradeDetail" />

    <template v-if="!isSuperUser && hasPermissionWithCode('002')">
      <RelatedOrder
        :trade-order-list="treadOrderList"
        @create-btn-click="requesetCreateSalePay"
      />
    </template>
  </div>
</template>
