<script lang="ts" setup>
/**
 * 订单管理员摘要：平台待发货 / 退款处理中，整卡可点跳商品订单列表
 * 数据走 workbench/order-summary（轻量 count）
 */
import type { OrderSummaryResult } from '../api';

import { onMounted, ref } from 'vue';
import { useRouter } from 'vue-router';

import { getOrderSummaryApi } from '../api';
import KpiCard from './KpiCard.vue';
import StatChip from './StatChip.vue';

interface Props {
  title: string;
}
defineProps<Props>();

const router = useRouter();
const data = ref<null | OrderSummaryResult>(null);
const loading = ref(true);
const error = ref(false);

/**
 * 拉取订单摘要；失败进入错误态可重试（不再静默消失）
 */
async function fetchSummary() {
  loading.value = true;
  error.value = false;
  try {
    data.value = await getOrderSummaryApi();
  } catch {
    error.value = true;
  } finally {
    loading.value = false;
  }
}

/**
 * 跳转商品订单列表
 */
function goProductOrder() {
  router.push('/order/product-order').catch(() => {});
}

onMounted(fetchSummary);
</script>

<template>
  <KpiCard
    :title="title"
    :loading="loading"
    :error="error"
    @refresh="fetchSummary"
  >
    <template v-if="data">
      <StatChip
        :highlight="data.wait_delivery > 0"
        :value="data.wait_delivery"
        icon="lucide:package"
        label="待发货"
        @click="goProductOrder"
      />
      <StatChip
        :highlight="data.refunding > 0"
        :value="data.refunding"
        icon="lucide:rotate-ccw"
        label="退款处理中"
        @click="goProductOrder"
      />
    </template>
  </KpiCard>
</template>
