<script lang="ts" setup>
/**
 * 药师待审摘要：待审处方总数，整卡可点跳审方列表
 * 数据走 workbench/pharmacist-summary（与审方列表 status=0 同口径）
 */
import type { PharmacistSummaryResult } from '../api';

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

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

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

/**
 * 跳转审方列表
 */
function goAudit() {
  router.push('/pharmacist/review-prescription').catch(() => {});
}

onMounted(fetchSummary);
</script>

<template>
  <KpiCard
    :title="title"
    :loading="loading"
    :error="error"
    @refresh="fetchSummary"
  >
    <template v-if="data">
      <StatChip
        :highlight="data.pending_total > 0"
        :value="data.pending_total"
        icon="lucide:shield-check"
        label="待审处方"
        @click="goAudit"
      />
    </template>
  </KpiCard>
</template>
