<template>
  <view :class="['diagnoses-page', elderClass]">
    <text class="page-title">诊断记录</text>

    <scroll-view scroll-y class="diagnosis-scroll" @scrolltolower="loadMore">
      <view class="diagnosis-list">
        <view v-for="d in records" :key="d.id" class="diagnosis-card">
          <view class="diagnosis-card__header">
            <text class="diagnosis-card__name">{{ d.diagnosis_name }}</text>
            <text :class="['diagnosis-card__status', (STATUS_MAP[d.status] || { cls: '' }).cls]">
              {{ (STATUS_MAP[d.status] || { label: d.status }).label }}
            </text>
          </view>
          <view class="diagnosis-card__meta">
            <text :class="['diagnosis-card__type', (TYPE_MAP[d.diagnosis_type] || { cls: '' }).cls]">
              {{ (TYPE_MAP[d.diagnosis_type] || { label: d.diagnosis_type }).label }}
            </text>
            <text class="diagnosis-card__code">{{ d.icd_code }}</text>
          </view>
          <text class="diagnosis-card__date">诊断日期：{{ d.diagnosed_date }}</text>
          <text v-if="d.notes" class="diagnosis-card__notes">{{ d.notes }}</text>
        </view>
      </view>

      <EmptyState
        v-if="records.length === 0 && !loading"
        :text="authStore.currentPatient ? '暂无诊断记录' : '请先在就诊人管理中选择就诊人'"
      />

      <Loading v-if="loading" text="加载中..." />
    </scroll-view>
  </view>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { onShow, onPullDownRefresh } from '@dcloudio/uni-app'
import { listDiagnoses, type Diagnosis } from '@/services/health-record'
import { useAuthStore } from '@/stores/auth'
import { useElderClass } from '@/composables/useElderClass'
import EmptyState from '@/components/EmptyState.vue'
import Loading from '@/components/Loading.vue'

const TYPE_MAP: Record<string, { label: string; cls: string }> = {
  primary: { label: '主要', cls: 'primary' },
  secondary: { label: '次要', cls: 'secondary' },
  comorbid: { label: '合并症', cls: 'comorbid' },
}

const STATUS_MAP: Record<string, { label: string; cls: string }> = {
  active: { label: '活动', cls: 'active' },
  resolved: { label: '已解决', cls: 'resolved' },
  chronic: { label: '慢性', cls: 'chronic' },
}

const { elderClass } = useElderClass()
const authStore = useAuthStore()
const records = ref<Diagnosis[]>([])
const page = ref(1)
const total = ref(0)
const loading = ref(false)

const fetchData = async (p: number, append = false) => {
  if (!authStore.currentPatient) {
    records.value = []
    return
  }
  loading.value = true
  try {
    const res = await listDiagnoses(authStore.currentPatient.id, { page: p, page_size: 20 })
    const list = res.data || []
    records.value = append ? [...records.value, ...list] : list
    total.value = res.total
    page.value = p
  } catch {
    uni.showToast({ title: '加载失败', icon: 'none' })
  } finally {
    loading.value = false
  }
}

const loadMore = () => {
  if (!loading.value && records.value.length < total.value) {
    fetchData(page.value + 1, true)
  }
}

onShow(() => { fetchData(1) })
onPullDownRefresh(() => { fetchData(1).finally(() => uni.stopPullDownRefresh()) })
</script>

<style lang="scss" scoped>
.diagnoses-page { min-height: 100vh; background: $bg; padding: 32px 24px 0; }
.page-title {
  font-family: 'Georgia', 'Times New Roman', serif;
  font-size: var(--tk-font-num);
  font-weight: bold;
  color: $tx;
  margin-bottom: 20px;
  display: block;
  padding-left: 4px;
}
.diagnosis-scroll { height: calc(100vh - 80px); }
.diagnosis-list { display: flex; flex-direction: column; gap: 16px; }
.diagnosis-card { background: $card; border-radius: $r; padding: 28px; box-shadow: $shadow-sm; }
.diagnosis-card__header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.diagnosis-card__name { font-size: var(--tk-font-body-lg); font-weight: bold; color: $tx; flex: 1; margin-right: 12px; }
.diagnosis-card__status {
  @include tag($bd-l, $tx3);
  &.active { @include tag($acc-l, $acc); }
  &.resolved { @include tag($acc-l, $acc); }
  &.chronic { @include tag($wrn-l, $wrn); }
}
.diagnosis-card__meta { display: flex; align-items: center; gap: 12px; margin-bottom: 8px; }
.diagnosis-card__type {
  @include tag($pri-l, $pri-d);
  &.secondary { @include tag($bd-l, $tx2); }
  &.comorbid { @include tag($wrn-l, $wrn); }
}
.diagnosis-card__code { font-size: var(--tk-font-body); color: $tx3; font-variant-numeric: tabular-nums; }
.diagnosis-card__date { font-size: var(--tk-font-body); color: $tx2; display: block; }
.diagnosis-card__notes { font-size: var(--tk-font-body); color: $tx2; display: block; margin-top: 8px; }
</style>
