<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 ReturnTciketForm from "./components/return-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 { httpDetail as httpReturnDetail } from "/@/api/purchase/ticketReturn";

import {
  ticketReturnReliveColumns,
  ticketReturnColumns
} from "/@/utils/details/_purchase";

import {
  httpDetail,
  httpAdd,
  httpStatus
} from "/@/api/purchase/ticketReturnRelive";

const { push } = useRouter();

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

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

const { isSuperUser } = useUserInfo();

const { id, isDetail, title, collapses } = useDetail({
  baseName: "通用采购回票解除",
  collapseLen: 5
});

const { run: detail, data } = useAsync<Record<string, string>>({
  initalData: {},
  success: ({ orderCode }) =>
    returnDetailRun(httpReturnDetail({ hpNo: orderCode }))
});

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

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

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

const handleApproval = data =>
  approval(httpStatus({ returnCode: id.value, ...data }));

const requesetDetail = () => detail(httpDetail({ returnCode: id.value }));
const handleCreate = data => create(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
              v-if="isDetail"
              :columns="ticketReturnReliveColumns"
              :data="data"
            />
            <!-- 创建回票退票 -->
            <ReturnTciketForm v-else @create="handleCreate" />
          </ElCollapseItem>

          <ElCollapseItem v-if="isDetail" name="4" title="采购回票详情">
            <BasicDescriptions
              :columns="ticketReturnColumns"
              :data="returnDetailData"
            />
          </ElCollapseItem>

          <div v-if="isDetail">
            <ElCollapseItem name="2" title="对账详情">
              <ReconciliationForm ref="formRef" readonly :id="data.payNo" />
            </ElCollapseItem>
            <ElCollapseItem
              name="3"
              title="买方公司财务审核"
              v-if="
                !isSuperUser &&
                String(data.status) === '1' &&
                hasPermissionWithCode('020')
              "
            >
              <ReceiptApprovalNode
                no-time
                has-remark
                approve-value="2"
                reject-value="3"
                img-prop="returnImg"
                @approval="handleApproval"
              />
            </ElCollapseItem>
          </div>
        </ElCollapse>
      </ElTabPane>
    </ElTabs>
  </div>
</template>
