<script setup lang="ts">
import { computed, onMounted, ref, watchEffect } from "vue";
import { useRoute } from "vue-router";
import { useResponseHandle } from "/@/hooks";
import { httpDetail, httpStatus } from "/@/api/InvoiceSales/capitalPool";
import { httpInfo } from "/@/api/InvoiceSales/capitalClaim";
import BasicDescriptions from "/@/components/BasicDescriptions";
import { approval_process } from "../approval-process";
import { CAPITAL_COLUMNS } from "/@/utils/details/tragelog";
import PaymentReceipt from "../basic-claim/receipt-payment.vue";
import { ORDER_COLUMNS, GOOD_COLUMNS } from "/@/utils/details/sale";

const { query } = useRoute();
const responseHandle = useResponseHandle();
const capitalDetail = ref<any>({});
const tradeDetail = ref<any>({});
const collapses = ref(["1", "2", "3", "4", "5"]);
const loading = ref(false);

const logNo = computed(() => query.id);

//当前的审核流程
const currentProcess = computed(() => {
  const { status } = capitalDetail.value;
  return approval_process[status];
});

//资金认领详情
async function requesetCapitalDetail() {
  loading.value = true;
  const { code, message, data } = await httpDetail({
    logNo: logNo.value
  });
  loading.value = false;

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

//资金详情
async function requesetTradeDetail() {
  const { data, message, code } = await httpInfo({
    tradNo: tradeNo.value
  });

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

const tradeNo = computed(() => {
  const { tradNo } = capitalDetail.value;
  return tradNo;
});

//资金认证状态
async function requesetCapitalStatus(otherParams) {
  const { code, message } = await httpStatus({
    logNo: logNo.value,
    ...otherParams
  });

  responseHandle({
    code,
    message,
    handler: () => requesetTradeDetail()
  });
}

//初始化数据
function initalData() {
  if (!logNo.value) return;
  requesetCapitalDetail();
}

onMounted(() => initalData());

watchEffect(() => tradeNo.value && requesetTradeDetail());
</script>

<template>
  <div class="capital__content" bg-white v-loading="loading">
    <ElTabs>
      <ElTabPane label="详情">
        <ElCollapse v-model="collapses">
          <ElCollapseItem title="资金详情" name="1">
            <PaymentReceipt :trade-info="tradeDetail" />
          </ElCollapseItem>
          <!-- 关联订单 -->
          <ElCollapseItem title="关联订单信息" name="2">
            <BasicDescriptions
              :columns="ORDER_COLUMNS"
              :data="capitalDetail.orderinfo ? capitalDetail.orderinfo : {}"
              :col-number="2"
            />
          </ElCollapseItem>

          <ElCollapseItem title="商品信息" name="3">
            <BasicDescriptions
              :columns="GOOD_COLUMNS"
              :data="capitalDetail.orderinfo ? capitalDetail.orderinfo : {}"
              :col-number="2"
            />
          </ElCollapseItem>
          <!-- 详情 -->
          <ElCollapseItem title="认领资金详情" name="4">
            <BasicDescriptions
              :columns="CAPITAL_COLUMNS"
              :data="capitalDetail"
              :col-number="2"
            />
          </ElCollapseItem>

          <!-- 审核 -->
          <ElCollapseItem
            name="5"
            v-if="currentProcess"
            :title="currentProcess.title"
          >
            <component
              :is="currentProcess.component"
              @change-status="requesetCapitalStatus"
            />
          </ElCollapseItem>
        </ElCollapse>
      </ElTabPane>

      <ElTabPane label="审批记录" />
    </ElTabs>
  </div>
</template>
