import { Colors } from '@/constants/Colors'; import { useAppSelector } from '@/hooks/redux'; import { useColorScheme } from '@/hooks/useColorScheme'; import { selectUserAge } from '@/store/userSlice'; import { Ionicons } from '@expo/vector-icons'; import React, { useState } from 'react'; import { StyleSheet, Text, TouchableOpacity, View } from 'react-native'; interface DietPlanCardProps { onGeneratePlan: () => void; } const DietPlanCard: React.FC = ({ onGeneratePlan }) => { const colorScheme = useColorScheme(); const theme = Colors[colorScheme ?? 'light']; const [isExpanded, setIsExpanded] = useState(false); const userProfile = useAppSelector((s) => s.user?.profile); const userAge = useAppSelector(selectUserAge); // 计算BMI const calculateBMI = () => { if (!userProfile?.weight || !userProfile?.height) return null; const weight = Number(userProfile.weight); const height = Number(userProfile.height) / 100; // 转换为米 return weight / (height * height); }; const bmi = calculateBMI(); // 获取BMI状态 const getBMIStatus = (bmi: number) => { if (bmi < 18.5) return { text: '偏瘦', color: '#3B82F6' }; if (bmi < 24) return { text: '正常', color: '#10B981' }; if (bmi < 28) return { text: '超重', color: '#F59E0B' }; return { text: '肥胖', color: '#EF4444' }; }; const bmiStatus = bmi ? getBMIStatus(bmi) : null; // 估算基础代谢率 (BMR) const calculateBMR = () => { if (!userProfile?.weight || !userProfile?.height || userAge === null) return null; const weight = Number(userProfile.weight); const height = Number(userProfile.height); const age = userAge; const gender = userProfile.gender; // 使用 Mifflin-St Jeor 公式 if (gender === 'male') { return Math.round(10 * weight + 6.25 * height - 5 * age + 5); } else { return Math.round(10 * weight + 6.25 * height - 5 * age - 161); } }; const bmr = calculateBMR(); const dailyCalories = bmr ? Math.round(bmr * 1.4) : null; // 轻度活动系数 return ( {/* 头部 */} 个性化饮食方案 基于你的身体数据定制 {/* 用户资料概览 */} {userProfile && ( 个人资料 {userProfile.name?.charAt(0) || 'U'} {userProfile.weight && ( {userProfile.weight} kg )} {userProfile.height && ( {userProfile.height} cm )} {userAge !== null && ( {userAge} )} )} {/* BMI 部分 */} {bmi && bmiStatus && ( BMI 指数 {bmiStatus.text} {bmi.toFixed(1)} {/* BMI 刻度条 */} 偏瘦 正常 超重 肥胖 )} {/* 可折叠的详细信息 */} setIsExpanded(!isExpanded)} > 营养需求分析 {isExpanded && ( <> {/* 卡路里需求 */} {dailyCalories && ( 每日卡路里需求 {dailyCalories} kcal )} {/* 营养素分配 */} 55% 碳水 20% 蛋白质 25% 脂肪 * 营养素比例基于一般健康成人推荐标准,具体需求因人而异 )} {/* 生成方案按钮 */} 生成个性化饮食方案 {/* 使用次数提示 */} AI 将根据你的身体数据和健康目标制定专属方案 ); }; const styles = StyleSheet.create({ dietPlanContainer: { borderRadius: 16, padding: 16, gap: 16, borderWidth: 1, maxWidth: '85%', alignSelf: 'flex-start', minWidth: 300 }, dietPlanHeader: { gap: 4, }, dietPlanTitleContainer: { flexDirection: 'row', alignItems: 'center', gap: 8, }, dietPlanTitle: { fontSize: 18, fontWeight: '800', }, dietPlanSubtitle: { fontSize: 12, fontWeight: '600', letterSpacing: 1, }, profileSection: { gap: 12, }, sectionTitle: { fontSize: 14, fontWeight: '700', }, profileDataRow: { flexDirection: 'row', alignItems: 'center', gap: 16, }, avatarContainer: { alignItems: 'center', }, avatar: { width: 48, height: 48, borderRadius: 24, alignItems: 'center', justifyContent: 'center', }, avatarText: { color: '#FFFFFF', fontSize: 18, fontWeight: '800', }, profileStats: { flexDirection: 'row', flex: 1, justifyContent: 'space-around', }, statItem: { alignItems: 'center', gap: 4, }, statValue: { fontSize: 20, fontWeight: '800', }, statLabel: { fontSize: 12, }, bmiSection: { gap: 12, }, bmiHeader: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', }, bmiStatusBadge: { paddingHorizontal: 8, paddingVertical: 4, borderRadius: 12, }, bmiStatusText: { fontSize: 12, fontWeight: '700', color: '#FFFFFF', }, bmiValue: { fontSize: 32, fontWeight: '800', textAlign: 'center', }, bmiScale: { flexDirection: 'row', height: 8, borderRadius: 4, overflow: 'hidden', gap: 1, }, bmiBar: { flex: 1, height: '100%', }, bmiLabels: { flexDirection: 'row', justifyContent: 'space-between', paddingHorizontal: 4, }, bmiLabel: { fontSize: 11, }, collapsibleSection: { paddingVertical: 8, borderBottomWidth: 1, borderBottomColor: 'rgba(0,0,0,0.06)', }, collapsibleHeader: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', }, caloriesSection: { gap: 12, }, caloriesHeader: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', }, caloriesValue: { fontSize: 18, fontWeight: '800', }, nutritionGrid: { flexDirection: 'row', justifyContent: 'space-around', gap: 16, }, nutritionItem: { alignItems: 'center', gap: 8, }, nutritionValue: { fontSize: 24, fontWeight: '800', }, nutritionLabelRow: { flexDirection: 'row', alignItems: 'center', gap: 4, }, nutritionDot: { width: 8, height: 8, borderRadius: 4, }, nutritionLabel: { fontSize: 12, }, nutritionNote: { fontSize: 12, lineHeight: 16, textAlign: 'center', marginTop: 8, }, dietPlanButton: { flexDirection: 'row', alignItems: 'center', justifyContent: 'center', gap: 8, paddingVertical: 12, paddingHorizontal: 16, borderRadius: 12, marginTop: 8, }, dietPlanButtonText: { fontSize: 14, fontWeight: '700', color: '#FFFFFF', }, usageCountContainer: { flexDirection: 'row', alignItems: 'center', gap: 4, paddingHorizontal: 8, paddingVertical: 4, borderRadius: 12, backgroundColor: 'rgba(122,90,248,0.08)', }, usageText: { fontSize: 12, fontWeight: '600', flex: 1, }, }); export default DietPlanCard;