<template>
  <scroll-view scroll-y class="page-scroll">
    <view :class="['family-page', elderClass]">
      <text class="family-page-title">就诊人管理</text>

      <view class="family-list">
        <view
          v-for="p in patients"
          :key="p.id"
          :class="['family-item', { active: authStore.currentPatient?.id === p.id }]"
          @tap="handleSelect(p)"
        >
          <view class="family-avatar">
            <text class="family-avatar-text">{{ relationInitial(p.relation || '本人') }}</text>
          </view>
          <view class="family-info">
            <view class="family-name-row">
              <text class="family-name">{{ p.name }}</text>
              <text v-if="authStore.currentPatient?.id === p.id" class="family-current-tag">当前</text>
            </view>
            <view class="family-meta">
              <text class="family-relation-tag">{{ p.relation || '本人' }}</text>
              <text class="family-gender">{{ genderText(p.gender) }}</text>
            </view>
          </view>
          <view class="family-edit" @tap.stop="goToEdit(p)">
            <text class="family-edit-text">编辑</text>
          </view>
        </view>
      </view>

      <EmptyState v-if="patients.length === 0 && !loading" text="暂无就诊人" />

      <view class="family-add-btn" @tap="goToAdd">
        <text class="family-add-text">添加就诊人</text>
      </view>
    </view>
  </scroll-view>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { onShow } from '@dcloudio/uni-app'
import { listPatients, Patient } from '@/services/patient'
import { useAuthStore } from '@/stores/auth'
import { useElderClass } from '@/composables/useElderClass'
import EmptyState from '@/components/EmptyState.vue'

const authStore = useAuthStore()
const { elderClass } = useElderClass()

const patients = ref<Patient[]>([])
const loading = ref(false)

async function fetchPatients() {
  loading.value = true
  try {
    const res = await listPatients()
    patients.value = res.data || []
  } catch {
    uni.showToast({ title: '加载失败', icon: 'none' })
  } finally {
    loading.value = false
  }
}

function handleSelect(patient: Patient) {
  authStore.setCurrentPatient({
    id: patient.id,
    name: patient.name,
    gender: patient.gender,
    birth_date: patient.birth_date,
    relation: patient.relation || '本人',
  })
  uni.showToast({ title: `已切换为 ${patient.name}`, icon: 'success' })
}

function goToAdd() {
  uni.navigateTo({ url: '/pages-sub/pkg-profile/family-add/index' })
}

function goToEdit(patient: Patient) {
  uni.setStorageSync('edit_patient', patient)
  uni.navigateTo({ url: `/pages-sub/pkg-profile/family-add/index?id=${patient.id}` })
}

function genderText(g?: string) {
  if (g === 'male') return '男'
  if (g === 'female') return '女'
  return '未知'
}

function relationInitial(relation: string) {
  return relation ? relation.charAt(0) : '本'
}

onShow(() => {
  fetchPatients()
})
</script>

<style lang="scss" scoped>
.page-scroll {
  height: 100vh;
  background: $bg;
}

.family-page {
  padding: 32px 24px;
  padding-bottom: 160px;
}

.family-page-title {
  @include section-title;
  padding-left: 4px;
}

.family-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.family-item {
  display: flex;
  align-items: center;
  background: $card;
  border-radius: $r;
  padding: 24px;
  box-shadow: $shadow-sm;
  transition: box-shadow 0.2s;

  &:active {
    box-shadow: $shadow-md;
  }

  &.active {
    box-shadow: $shadow-md;
  }
}

.family-avatar {
  @include flex-center;
  width: 80px;
  height: 80px;
  border-radius: $r;
  background: $pri-l;
  flex-shrink: 0;
  margin-right: 20px;
}

.family-avatar-text {
  font-family: 'Georgia', 'Times New Roman', serif;
  font-size: var(--tk-font-num-lg);
  font-weight: bold;
  color: $pri-d;
}

.family-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.family-name-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
}

.family-name {
  font-size: var(--tk-font-num);
  font-weight: bold;
  color: $tx;
}

.family-current-tag {
  @include tag($pri, $white);
  font-size: var(--tk-font-body-sm);
  padding: 2px 10px;
}

.family-meta {
  display: flex;
  align-items: center;
  gap: 12px;
}

.family-relation-tag {
  @include tag($pri-l, $pri-d);
  font-size: var(--tk-font-body);
  padding: 2px 12px;
}

.family-gender {
  font-size: var(--tk-font-h2);
  color: $tx2;
}

.family-edit {
  flex-shrink: 0;
  margin-left: 16px;
  padding: 14px 24px;
  border: 1px solid $bd;
  border-radius: $r-pill;
  min-height: 48px;
  @include flex-center;

  &:active {
    background: $bd-l;
  }
}

.family-edit-text {
  font-size: var(--tk-font-h2);
  color: $tx2;
}

.family-add-btn {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: $pri;
  padding: 28px;
  text-align: center;
  box-shadow: 0 -2px 12px rgba(196, 98, 58, 0.15);
}

.family-add-text {
  font-family: 'Georgia', 'Times New Roman', serif;
  font-size: var(--tk-font-num);
  color: $white;
  font-weight: bold;
  letter-spacing: 2px;
}
</style>
