<script lang="ts" setup>
/**
 * 平台今日总览（超管/系统管理员）：今日订单/今日营业额/今日挂号/新增门店录入
 * 数据走 workbench/platform-overview（轻量 count/sum，营业额口径与业务员看板一致）
 */
import type { PlatformOverviewResult } from '../api';

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

import { getPlatformOverviewApi } 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 | PlatformOverviewResult>(null);
const loading = ref(true);
const error = ref(false);

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

/** 跳转指定路由（失败静默，避免无权限菜单时报错） */
function go(path: string) {
  router.push(path).catch(() => {});
}

onMounted(fetchSummary);
</script>

<template>
  <KpiCard
    :title="title"
    :loading="loading"
    :error="error"
    subtitle="今日"
    @refresh="fetchSummary"
  >
    <template v-if="data">
      <StatChip
        :value="data.order_count"
        icon="lucide:shopping-cart"
        label="今日订单"
        @click="go('/order/product-order')"
      />
      <StatChip
        :value="`¥${data.revenue}`"
        icon="lucide:banknote"
        label="今日营业额"
        @click="go('/order/product-order')"
      />
      <StatChip
        :value="data.register_count"
        icon="lucide:stethoscope"
        label="今日挂号"
        @click="go('/business/register/list')"
      />
      <StatChip
        :value="data.store_input_count"
        icon="lucide:store"
        label="新增门店录入"
        @click="go('/system/store-input')"
      />
    </template>
  </KpiCard>
</template>
