import { useState, useEffect } from 'react'; import { View, Text, Input, ScrollView } from '@tarojs/components'; import Taro from '@tarojs/taro'; import * as doctorApi from '@/services/doctor'; import Loading from '@/components/Loading'; import EmptyState from '@/components/EmptyState'; import './index.scss'; export default function PatientList() { const [patients, setPatients] = useState([]); const [tags, setTags] = useState([]); const [activeTag, setActiveTag] = useState(''); const [search, setSearch] = useState(''); const [loading, setLoading] = useState(true); const [total, setTotal] = useState(0); const [page, setPage] = useState(1); useEffect(() => { loadTags(); }, []); useEffect(() => { loadPatients(); }, [page, activeTag]); const loadTags = async () => { try { const res = await doctorApi.listPatientTags(); setTags(res.data || []); } catch { /* ignore */ } }; const loadPatients = async () => { setLoading(true); try { const res = await doctorApi.listPatients({ page, page_size: 20, search: search || undefined, tag_id: activeTag || undefined, }); setPatients(res.data || []); setTotal(res.total || 0); } catch { Taro.showToast({ title: '加载失败', icon: 'none' }); } finally { setLoading(false); } }; const handleSearch = () => { setPage(1); loadPatients(); }; const handleTagFilter = (tagId: string) => { setActiveTag(tagId === activeTag ? '' : tagId); setPage(1); }; const getGenderLabel = (gender?: string) => { if (!gender) return ''; return gender === 'male' ? '男' : gender === 'female' ? '女' : gender; }; const calcAge = (birthDate?: string) => { if (!birthDate) return ''; const birth = new Date(birthDate); const now = new Date(); let age = now.getFullYear() - birth.getFullYear(); if (now.getMonth() < birth.getMonth() || (now.getMonth() === birth.getMonth() && now.getDate() < birth.getDate())) { age--; } return `${age}岁`; }; if (loading && patients.length === 0) return ; return ( setSearch(e.detail.value)} confirmType='search' onConfirm={handleSearch} /> {tags.length > 0 && ( handleTagFilter('')} > 全部 {tags.map((tag) => ( handleTagFilter(tag.id)} > {tag.name} ))} )} 共 {total} 位患者 {patients.length === 0 ? ( ) : ( {patients.map((p) => ( Taro.navigateTo({ url: `/pages/doctor/patients/detail/index?id=${p.id}` })} > {p.name} {getGenderLabel(p.gender)} {calcAge(p.birth_date)} {p.tags && p.tags.length > 0 && ( {p.tags.map((t) => ( {t.name} ))} )} {p.status && ( {p.status === 'active' ? '活跃' : p.status === 'inactive' ? '非活跃' : p.status} )} ))} )} {total > 20 && ( page > 1 && setPage(page - 1)} > 上一页 {page} / {Math.ceil(total / 20)} = Math.ceil(total / 20) ? 'disabled' : ''}`} onClick={() => page < Math.ceil(total / 20) && setPage(page + 1)} > 下一页 )} ); }