All files / app/(tabs)/profile/components AddHealthRecordModal.tsx

0% Statements 0/41
0% Branches 0/26
0% Functions 0/7
0% Lines 0/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
/**
 * 添加健康记录模态框组件
 */
 
import { useState } from 'react';
import { Alert, ScrollView } from 'react-native';
import { Dialog, YStack, XStack, Text, Input, TextArea, Separator } from 'tamagui';
import { Calendar, Syringe, Bug, X } from '@tamagui/lucide-icons';
import { Button } from '@/src/design-system/components';
import { supabasePetHealthService } from '@/src/lib/supabase';
import type { HealthRecordType } from '@/src/types/petHealth';
 
interface Props {
  petId: number;
  petName: string;
  open: boolean;
  onOpenChange: (open: boolean) => void;
  onSuccess: () => void;
}
 
export function AddHealthRecordModal({ petId, petName, open, onOpenChange, onSuccess }: Props) {
  const [recordType, setRecordType] = useState<HealthRecordType>('vaccine');
  const [name, setName] = useState('');
  const [date, setDate] = useState(new Date().toISOString().split('T')[0]);
  const [nextDate, setNextDate] = useState('');
  const [brand, setBrand] = useState('');
  const [dosage, setDosage] = useState('');
  const [clinic, setClinic] = useState('');
  const [notes, setNotes] = useState('');
  const [loading, setLoading] = useState(false);
 
  const handleSubmit = async () => {
    if (!name.trim()) {
      Alert.alert('提示', '请输入记录名称');
      return;
    }
 
    if (!date) {
      Alert.alert('提示', '请选择日期');
      return;
    }
 
    setLoading(true);
    try {
      const { error } = await supabasePetHealthService.createHealthRecord({
        pet_id: petId,
        record_type: recordType,
        name: name.trim(),
        date,
        next_date: nextDate || undefined,
        brand: brand.trim() || undefined,
        dosage: dosage.trim() || undefined,
        clinic: clinic.trim() || undefined,
        notes: notes.trim() || undefined,
      });
 
      if (error) {
        Alert.alert('错误', (error as any)?.message || '添加记录失败');
        return;
      }
 
      Alert.alert('成功', '健康记录已添加');
      resetForm();
      onOpenChange(false);
      onSuccess();
    } catch (err: any) {
      Alert.alert('错误', err.message || '添加记录失败');
    } finally {
      setLoading(false);
    }
  };
 
  const resetForm = () => {
    setName('');
    setDate(new Date().toISOString().split('T')[0]);
    setNextDate('');
    setBrand('');
    setDosage('');
    setClinic('');
    setNotes('');
  };
 
  return (
    <Dialog modal open={open} onOpenChange={onOpenChange}>
      <Dialog.Portal>
        <Dialog.Overlay
          key="overlay"
          animation="quick"
          opacity={0.5}
          enterStyle={{ opacity: 0 }}
          exitStyle={{ opacity: 0 }}
        />
 
        <Dialog.Content
          bordered
          key="content"
          animateOnly={['transform', 'opacity']}
          animation="quick"
          enterStyle={{ x: 0, y: -20, opacity: 0, scale: 0.9 }}
          exitStyle={{ x: 0, y: 10, opacity: 0, scale: 0.95 }}
          gap="$4"
          padding="$4"
          backgroundColor="$background"
          width="90%"
          maxWidth={400}
          maxHeight="80%"
        >
          {/* 标题栏 */}
          <XStack justifyContent="space-between" alignItems="center">
            <Dialog.Title fontSize={20} fontWeight="700">
              添加健康记录
            </Dialog.Title>
            <YStack
              padding="$2"
              borderRadius="$2"
              pressStyle={{ opacity: 0.7, backgroundColor: '$gray3' }}
              cursor="pointer"
              onPress={() => onOpenChange(false)}
            >
              <X size={20} color="$gray11" />
            </YStack>
          </XStack>
 
          <Text fontSize={14} color="$gray11">
            为 {petName} 添加健康记录
          </Text>
 
          <Separator />
 
          {/* 表单内容 - 可滚动 */}
          <ScrollView
            style={{ maxHeight: 400 }}
            showsVerticalScrollIndicator={false}
            contentContainerStyle={{ paddingBottom: 16 }}
          >
            <YStack gap="$4">
              {/* 记录类型选择 */}
              <YStack gap="$2">
                <Text fontSize={14} fontWeight="600">
                  记录类型
                </Text>
                <XStack gap="$2">
                  <Button
                    size="sm"
                    variant={recordType === 'vaccine' ? 'primary' : 'outline'}
                    leftIcon={<Syringe size={16} />}
                    onPress={() => setRecordType('vaccine')}
                    flex={1}
                  >
                    疫苗接种
                  </Button>
                  <Button
                    size="sm"
                    variant={recordType === 'deworming' ? 'primary' : 'outline'}
                    leftIcon={<Bug size={16} />}
                    onPress={() => setRecordType('deworming')}
                    flex={1}
                  >
                    驱虫记录
                  </Button>
                </XStack>
              </YStack>
 
              {/* 记录名称 */}
              <YStack gap="$2">
                <Text fontSize={14} fontWeight="600">
                  记录名称 *
                </Text>
                <Input
                  placeholder={recordType === 'vaccine' ? '如:狂犬疫苗' : '如:体内驱虫'}
                  value={name}
                  onChangeText={setName}
                  backgroundColor="$gray2"
                  borderColor="$gray5"
                />
              </YStack>
 
              {/* 日期 */}
              <YStack gap="$2">
                <Text fontSize={14} fontWeight="600">
                  日期 *
                </Text>
                <Input
                  placeholder="YYYY-MM-DD"
                  value={date}
                  onChangeText={setDate}
                  backgroundColor="$gray2"
                  borderColor="$gray5"
                />
              </YStack>
 
              {/* 下次日期 */}
              <YStack gap="$2">
                <Text fontSize={14} fontWeight="600">
                  下次日期(可选)
                </Text>
                <Input
                  placeholder="YYYY-MM-DD"
                  value={nextDate}
                  onChangeText={setNextDate}
                  backgroundColor="$gray2"
                  borderColor="$gray5"
                />
              </YStack>
 
              {/* 品牌 */}
              <YStack gap="$2">
                <Text fontSize={14} fontWeight="600">
                  品牌(可选)
                </Text>
                <Input
                  placeholder="疫苗或药品品牌"
                  value={brand}
                  onChangeText={setBrand}
                  backgroundColor="$gray2"
                  borderColor="$gray5"
                />
              </YStack>
 
              {/* 剂量 */}
              <YStack gap="$2">
                <Text fontSize={14} fontWeight="600">
                  剂量(可选)
                </Text>
                <Input
                  placeholder="如:0.5ml"
                  value={dosage}
                  onChangeText={setDosage}
                  backgroundColor="$gray2"
                  borderColor="$gray5"
                />
              </YStack>
 
              {/* 诊所 */}
              <YStack gap="$2">
                <Text fontSize={14} fontWeight="600">
                  诊所/医院(可选)
                </Text>
                <Input
                  placeholder="接种或购买地点"
                  value={clinic}
                  onChangeText={setClinic}
                  backgroundColor="$gray2"
                  borderColor="$gray5"
                />
              </YStack>
 
              {/* 备注 */}
              <YStack gap="$2">
                <Text fontSize={14} fontWeight="600">
                  备注(可选)
                </Text>
                <TextArea
                  placeholder="添加备注信息"
                  value={notes}
                  onChangeText={setNotes}
                  backgroundColor="$gray2"
                  borderColor="$gray5"
                  minHeight={80}
                />
              </YStack>
            </YStack>
          </ScrollView>
 
          {/* 按钮 */}
          <XStack gap="$2" marginTop="$2">
            <Button
              size="md"
              variant="outline"
              onPress={() => onOpenChange(false)}
              flex={1}
              disabled={loading}
            >
              取消
            </Button>
            <Button
              size="md"
              variant="primary"
              onPress={handleSubmit}
              flex={1}
              loading={loading}
              disabled={loading}
            >
              添加
            </Button>
          </XStack>
        </Dialog.Content>
      </Dialog.Portal>
    </Dialog>
  );
}