import React from "react"; import { View, Text, TextInput, Image, TouchableOpacity, StyleSheet, KeyboardAvoidingView, Platform, Switch, } from "react-native"; import { useState } from "react"; import DropDownPicker from "react-native-dropdown-picker"; import { Country, countries } from "../../constants/countries"; import AsyncStorage from "@react-native-async-storage/async-storage"; import { addressApi } from "../../services/api/addressApi"; import BackIcon from "../../components/BackIcon"; import { useNavigation, useRoute, RouteProp } from "@react-navigation/native"; import { eventBus } from "../../utils/eventBus"; import { AddressItem } from "../../services/api/addressApi"; type RootStackParamList = { AddRess: { address?: AddressItem }; }; type AddRessRouteProp = RouteProp; export const AddRess = () => { const navigation = useNavigation(); const route = useRoute(); const [open, setOpen] = useState(false); const [value, setValue] = useState(null); const [items, setItems] = useState( countries.map((country) => ({ label: `${country.flag} ${country.name} (${country.phoneCode})`, value: country.name, })) ); const [formData, setFormData] = useState({ receiver_first_name: "", receiver_last_name: "", country: "", receiver_phone: "", receiver_phone_again: "", whatsapp_phone: "", is_default: false, }); const [selectedCountry, setSelectedCountry] = useState(null); const [isDefault, setIsDefault] = useState(false); const [copyPhoneToWhatsApp, setCopyPhoneToWhatsApp] = useState(false); const [phoneNumbersMatch, setPhoneNumbersMatch] = useState(true); const [errors, setErrors] = useState>({}); // 获取选中的国家数据 const fetchSelectedCountry = async () => { try { const countryData = await AsyncStorage.getItem("@selected_country"); if (countryData) { const parsedData = JSON.parse(countryData); setSelectedCountry(parsedData); setValue(parsedData.name); } } catch (error) { console.error("Error fetching selected country:", error); } }; // 在组件加载时获取数据 React.useEffect(() => { fetchSelectedCountry(); if (route.params?.address) { setFormData({ ...route.params.address, receiver_phone_again: route.params.address.receiver_phone, is_default: Boolean(route.params.address.is_default), }); } }, []); // 监听手机号码变化,如果勾选了复制选项则自动更新 WhatsApp 号码 React.useEffect(() => { if (copyPhoneToWhatsApp) { setFormData((prev) => ({ ...prev, whatsapp_phone: prev.receiver_phone, })); } }, [formData.receiver_phone, copyPhoneToWhatsApp]); // 监听手机号码变化,验证两次输入是否一致 React.useEffect(() => { if (formData.receiver_phone && formData.receiver_phone_again) { setPhoneNumbersMatch( formData.receiver_phone === formData.receiver_phone_again ); } else { setPhoneNumbersMatch(true); } }, [formData.receiver_phone, formData.receiver_phone_again]); const validateForm = () => { const newErrors: Record = {}; if (!formData.receiver_first_name) { newErrors.receiver_first_name = "请输入名"; } if (!formData.receiver_last_name) { newErrors.receiver_last_name = "请输入姓"; } if (!formData.receiver_phone) { newErrors.receiver_phone = "请输入手机号码"; } if (!formData.receiver_phone_again) { newErrors.receiver_phone_again = "请再次输入手机号码"; } if (formData.receiver_phone !== formData.receiver_phone_again) { newErrors.receiver_phone_again = "两次输入的手机号码不一致"; } if (!formData.whatsapp_phone) { newErrors.whatsapp_phone = "请输入WhatsApp号码"; } setErrors(newErrors); return Object.keys(newErrors).length === 0; }; const handleSubmit = async () => { if (validateForm()) { formData.country = selectedCountry.name; const data = { ...formData, country: value || '', } if (route.params?.address) { eventBus.emit("address-added", { ...formData, country: value || '', type: "edit", }); navigation.goBack(); await addressApi.updateAddress(data); } else { eventBus.emit("address-added", { ...formData, country: value || '', type: "add", }); navigation.goBack(); await addressApi.postAddress(data); } } }; return ( navigation.goBack()}> 新增收件人 请输入收件人信息 {/* First Name Field */} First Name * setFormData({ ...formData, receiver_first_name: text }) } /> {errors.receiver_first_name && ( {errors.receiver_first_name} )} {/* Last Name Field */} Last Name * setFormData({ ...formData, receiver_last_name: text }) } /> {errors.receiver_last_name && ( {errors.receiver_last_name} )} {/* 国家 */} country {/* Phone Number Section */} Mobile phone number * setFormData({ ...formData, receiver_phone: text, }) } /> {errors.receiver_phone && ( {errors.receiver_phone} )} Mobile phone number again * setFormData({ ...formData, receiver_phone_again: text, }) } /> {errors.receiver_phone_again && ( {errors.receiver_phone_again} )} {!phoneNumbersMatch && ( 两次输入的手机号码不一致 )} {/* WhatsApp Field */} WhatsApp * setFormData({ ...formData, whatsapp_phone: text }) } /> {errors.whatsapp_phone && ( {errors.whatsapp_phone} )} setCopyPhoneToWhatsApp(!copyPhoneToWhatsApp) } > {copyPhoneToWhatsApp && ( )} 复制手机号码到WhatsApp号码 {/* Default Setting Section */} Set as default setFormData({ ...formData, is_default: !formData.is_default, }) } trackColor={{ false: "#767577", true: "#81b0ff" }} thumbColor={formData.is_default ? "#002fa7" : "#f4f3f4"} ios_backgroundColor="#3e3e3e" /> {/* Submit Button */} Submit ); }; const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: "white", }, scrollContainer: { width: "100%", }, recipientFormContainer3: { flex: 1, flexDirection: "column", alignItems: "stretch", justifyContent: "flex-start", }, recipientFormContainer1: { width: "100%", padding: 15, paddingBottom: 32, }, recipientFormContainer2: { width: "100%", flexDirection: "column", alignItems: "stretch", justifyContent: "flex-start", }, titleContainer: { width: "100%", flexDirection: "row", alignItems: "center", justifyContent: "center", position: "relative", }, backIconContainer: { position: "absolute", left: 0, }, titleHeading: { fontWeight: "600", fontSize: 20, lineHeight: 22, fontFamily: "PingFang SC", color: "black", }, recipientInfoForm: { marginTop: 35, }, recipientInfoHeading: { padding: 0, margin: 0, fontWeight: "500", fontSize: 18, lineHeight: 22, fontFamily: "PingFang SC", color: "black", }, contactFormContainer: { width: "100%", marginTop: 9, }, formFieldContainer: { width: "100%", padding: 6, paddingLeft: 8, paddingBottom: 10, backgroundColor: "white", borderWidth: 1, borderColor: "#dbdce0", borderRadius: 5, }, flexRowCentered: { flexDirection: "row", alignItems: "center", justifyContent: "flex-start", }, elegantTextSnippet: { padding: 0, margin: 0, fontWeight: "500", fontSize: 12, fontFamily: "PingFang SC", color: "#646472", }, redTextHeading: { padding: 0, margin: 0, marginLeft: 1, fontWeight: "500", fontSize: 18, lineHeight: 22, fontFamily: "PingFang SC", color: "#fe1e00", }, pingFangText: { padding: 0, margin: 0, marginTop: -2, fontWeight: "400", fontSize: 16, lineHeight: 22, fontFamily: "PingFang SC", color: "#807e7e", }, pingFangText1: { padding: 0, margin: 0, marginTop: -2, fontWeight: "400", fontSize: 16, lineHeight: 22, fontFamily: "PingFang SC", color: "#807e7e", }, copyContainer: { marginTop: 10, flexDirection: "row", alignItems: "center", }, checkboxContainer: { flexDirection: "row", alignItems: "center", }, checkbox: { width: 15, height: 15, borderWidth: 1, borderColor: "#dbdce0", borderRadius: 4, marginRight: 8, justifyContent: "center", alignItems: "center", }, checked: { backgroundColor: "#002fa7", borderColor: "#002fa7", }, checkmark: { color: "white", fontSize: 12, }, checkboxLabel: { fontSize: 14, color: "#646472", fontFamily: "PingFang SC", }, lastNameInputContainer: { width: "100%", padding: 6, paddingLeft: 8, paddingBottom: 10, marginTop: 12, backgroundColor: "white", borderWidth: 1, borderColor: "#dbdce0", borderRadius: 5, }, lastNameInputContainer1: { width: "100%", padding: 6, paddingLeft: 8, paddingBottom: 10, backgroundColor: "white", borderTopWidth: 1, borderColor: "#dbdce0", }, redAsteriskTextStyle: { padding: 0, margin: 0, marginLeft: 1, fontWeight: "500", fontSize: 18, lineHeight: 22, fontFamily: "PingFang SC", color: "#fe1e00", }, formContainer: { flexDirection: "column", alignItems: "stretch", justifyContent: "center", width: "100%", marginTop: 12, backgroundColor: "white", borderWidth: 1, borderColor: "#dbdce0", }, optionListDropdown: {}, verticalCenteredColumn: { flexDirection: "column", alignItems: "stretch", justifyContent: "center", marginTop: -1, }, phoneNumberContainer: { flexDirection: "row", alignItems: "center", justifyContent: "flex-start", height: 60, paddingRight: 8, paddingLeft: 8, backgroundColor: "white", borderWidth: 1, borderColor: "#dbdce0", }, mobilePhoneNumberLabel: { padding: 0, margin: 0, fontWeight: "500", fontSize: 14, fontFamily: "PingFang SC", color: "#807e7e", }, mobilePhoneNumberLabel1: { padding: 0, margin: 0, fontWeight: "500", fontSize: 14, fontFamily: "PingFang SC", color: "#807e7e", }, phoneNumberPromptContainer: { flexDirection: "row", alignItems: "center", justifyContent: "flex-start", height: 60, paddingRight: 8, paddingLeft: 8, marginTop: -1, backgroundColor: "white", borderWidth: 1, borderColor: "#dbdce0", borderBottomLeftRadius: 5, borderBottomRightRadius: 5, }, defaultSettingSection: { flexDirection: "row", alignItems: "center", justifyContent: "space-between", width: "100%", marginTop: 15.5, gap: 8, }, defaultTextDisplayStyle: { padding: 0, margin: 0, fontWeight: "500", fontSize: 16, lineHeight: 22, fontFamily: "PingFang SC", color: "black", }, submitButtonContainer: { paddingRight: 11, paddingLeft: 11, marginTop: 60, }, primaryButtonStyle: { width: "100%", height: 50, justifyContent: "center", alignItems: "center", fontWeight: "600", fontSize: 16, lineHeight: 22, fontFamily: "PingFang SC", color: "white", backgroundColor: "#002fa7", borderWidth: 0, borderRadius: 25, }, buttonText: { color: "white", fontWeight: "600", fontSize: 16, lineHeight: 22, fontFamily: "PingFang SC", }, dropdown: { borderColor: "#dbdce0", borderWidth: 1, borderRadius: 5, marginTop: 8, backgroundColor: "white", minHeight: 40, paddingHorizontal: 10, }, dropdownContainer: { borderColor: "#dbdce0", borderWidth: 1, borderRadius: 5, backgroundColor: "white", marginTop: 2, }, dropdownText: { fontSize: 16, color: "#333", }, dropdownLabel: { fontSize: 16, color: "#333", }, arrowIcon: { width: 20, height: 20, }, placeholderStyle: { color: "#999", }, searchContainer: { borderBottomWidth: 1, borderBottomColor: "#dbdce0", padding: 10, }, searchInput: { borderWidth: 1, borderColor: "#dbdce0", borderRadius: 5, padding: 8, fontSize: 16, }, phoneNumberError: { borderColor: "#fe1e00", }, errorText: { color: "#fe1e00", fontSize: 12, marginTop: 4, fontFamily: "PingFang SC", }, });