<script lang="ts" setup>
/**
 * 医生今日摘要：待接诊/接诊中/已完成/今日开方/有效处方金额
 * 数据走 workbench/doctor-summary（与医生小程序工作台同口径），整卡可点跳医生接诊页
 */
import type { DoctorSummaryResult } from '../api';

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

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

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

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

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

/**
 * 跳转医生接诊页
 */
function goReception() {
  router.push('/doctor/reception').catch(() => {});
}

onMounted(fetchSummary);
</script>

<template>
  <KpiCard
    :title="title"
    :loading="loading"
    :error="error"
    @refresh="fetchSummary"
  >
    <template v-if="data">
      <StatChip
        :highlight="data.wait_accept > 0"
        :value="data.wait_accept"
        icon="lucide:clock"
        label="待接诊"
        @click="goReception"
      />
      <StatChip
        :value="data.accepting"
        icon="lucide:stethoscope"
        label="接诊中"
        @click="goReception"
      />
      <StatChip
        :value="data.completed"
        icon="lucide:check-circle-2"
        label="已完成"
        @click="goReception"
      />
      <StatChip
        :value="data.prescription_count"
        icon="lucide:file-text"
        label="今日开方"
        @click="goReception"
      />
      <StatChip
        :value="data.prescription_amount"
        icon="lucide:wallet"
        label="有效处方金额"
        @click="goReception"
      />
    </template>
  </KpiCard>
</template>
