import React from 'react'; import { View, Text, TouchableOpacity, StyleSheet, TextInput, ScrollView, Alert, ActivityIndicator, Platform, StatusBar, SafeAreaView, BackHandler } from "react-native"; import useCreateOrderStore from "../../store/createOrder"; import BackIcon from "../../components/BackIcon"; import { useNavigation, useRoute, RouteProp, useFocusEffect } from "@react-navigation/native"; import { NativeStackNavigationProp } from "@react-navigation/native-stack"; import { useState, useEffect } from "react"; import useUserStore from "../../store/user"; import { Order } from "../../services/api/orders"; import { payApi } from "../../services/api/payApi"; import { useTranslation } from "react-i18next"; import fontSize from "../../utils/fontsizeUtils"; // Define the param list for navigation type RootStackParamList = { PreviewOrder: { data: Order; payMethod: string; currency: string; amount: number; }; Pay: { payUrl: string }; OrderDetails: { orderId?: number }; }; export const PreviewOrder = () => { const { orderData, setOrderData } = useCreateOrderStore(); const navigation = useNavigation>(); const [phoneNumber, setPhoneNumber] = useState(""); const [showPhoneInput, setShowPhoneInput] = useState(false); const route = useRoute>(); const [loading, setLoading] = useState(false); const { user } = useUserStore(); const { t } = useTranslation(); useEffect(() => { if (!user.user_id) { return Alert.alert(t("order.preview.login_required")); } if (route.params.payMethod === "Brainnel Pay(Mobile Money)") { setShowPhoneInput(true); } else { setShowPhoneInput(false); } }, [route.params.payMethod, user.user_id, t]); // 处理系统返回键 useFocusEffect( React.useCallback(() => { const onBackPress = () => { // 拦截系统返回键事件,导航到OrderDetails页面 navigation.navigate("OrderDetails", { orderId: 1 }); return true; // 返回true表示已处理返回事件 }; // 添加返回键监听(Android) BackHandler.addEventListener('hardwareBackPress', onBackPress); return () => BackHandler.removeEventListener('hardwareBackPress', onBackPress); }, [navigation]) ); const handleSubmit = () => { if (showPhoneInput && !phoneNumber) { // Show error or alert if needed console.log("Phone number is required for Mobile Money"); return; } console.log(route.params.currency); const data = { order_id: route.params.data.order_id, method: route.params.payMethod, currency: route.params.currency ? route.params.currency : route.params.data.currency, amount: route.params.data.actual_amount, }; setLoading(true); payApi .getPayInfo(data) .then((res) => { if (res.success) { navigation.navigate("Pay", { payUrl: res.payment_url, }); } }) .catch((err) => { setLoading(false); Alert.alert(t("order.preview.payment_failed")); }) .finally(() => { setLoading(false); }); // navigation.navigate('Pay', { // orderId: user.user_id, // payMethod: route.params.payMethod, // }) }; return ( navigation.navigate("OrderDetails", { orderId :1})}> {t("order.preview.pay_now")} {/* Payment Method Section */} {t("order.preview.payment_method")} {route.params.payMethod} {showPhoneInput && ( {t("order.preview.enter_phone")} )} {/* Order Info Section */} {t("order.preview.payment_info")} {t("order.preview.name")} {route.params?.data.receiver_name || "N/A"} {t("order.preview.phone")} {route.params?.data.receiver_phone || "N/A"} {route.params.data?.whatsapp_number && ( WhatsApp {route.params.data?.whatsapp_number} )} {t("order.preview.country")} {route.params?.data.receiver_country || "N/A"} {t("order.preview.shipping_address")} {route.params?.data.receiver_address || "N/A"} {t("order.preview.order_amount")} {route.params.amount || "N/A"} {route.params.data.currency} {t("order.preview.shipping_fee")} {( route.params.data.domestic_shipping_fee + route.params.data.shipping_fee ).toFixed(2) || "N/A"}{" "} {route.params.data.currency} {/* Order Summary Section */} {t("order.preview.order_total")} {t("order.preview.total_amount")} {route.params.data.actual_amount} {route.params.data.currency} {loading ? ( ) : ( {t("order.preview.confirm_payment")} )} ); }; const styles = StyleSheet.create({ safeArea: { flex: 1, backgroundColor: '#fff', }, safeAreaContent: { flex: 1, paddingTop: Platform.OS === 'android' ? 0 : 0, }, container: { flex: 1, backgroundColor: '#fff', }, scrollContainer: { flex: 1, }, mainContent: { padding: 15, }, submitButtonContainer: { paddingHorizontal: 16, paddingVertical: 20, backgroundColor: "white", borderTopWidth: 1, borderTopColor: "#eaeaea", }, primaryButtonStyle: { width: "100%", height: 50, justifyContent: "center", alignItems: "center", backgroundColor: "#002fa7", borderRadius: 25, shadowColor: "#002fa7", shadowOffset: { width: 0, height: 3 }, shadowOpacity: 0.2, shadowRadius: 5, elevation: 5, }, buttonText: { color: "white", fontWeight: "600", fontSize: fontSize(16), fontFamily: "PingFang SC", }, disabledButtonStyle: { backgroundColor: "#c0c0c0", shadowOpacity: 0, }, titleContainer: { width: "100%", padding: 15, flexDirection: "row", alignItems: "center", justifyContent: "center", position: "relative", backgroundColor: "#fff", borderBottomWidth: 1, borderBottomColor: "#eaeaea", }, backIconContainer: { position: "absolute", left: 15, backgroundColor: "#fff", }, titleHeading: { fontWeight: "600", fontSize: fontSize(16), lineHeight: 22, fontFamily: "PingFang SC", color: "black", }, section: { marginBottom: 20, backgroundColor: "#fff", borderRadius: 8, padding: 15, shadowColor: "#000", shadowOffset: { width: 0, height: 1, }, shadowOpacity: 0.22, shadowRadius: 2.22, elevation: 3, }, sectionTitle: { fontSize: fontSize(18), fontWeight: "600", marginBottom: 15, color: "#000", }, paymentMethodContainer: { marginTop: 5, }, paymentMethodText: { fontSize: fontSize(17), color: "#FF6000", fontWeight: "500", }, phoneInputContainer: { marginTop: 15, }, phoneInputLabel: { fontSize: fontSize(15), marginBottom: 5, color: "#333", }, phoneInput: { borderWidth: 1, borderColor: "#ddd", borderRadius: 5, padding: 10, fontSize: fontSize(15), }, infoRow: { flexDirection: "row", justifyContent: "space-between", marginBottom: 10, paddingBottom: 10, borderBottomWidth: 1, borderBottomColor: "#f0f0f0", }, infoLabel: { fontSize: fontSize(15), color: "#666", flex: 1, }, infoValue: { fontSize: fontSize(15), color: "#333", flex: 2, textAlign: "right", }, totalRow: { flexDirection: "row", justifyContent: "space-between", alignItems: "center", paddingVertical: 12, }, totalLabel: { fontSize: fontSize(16), fontWeight: "600", color: "#333", fontFamily: "PingFang SC", }, totalValue: { fontSize: fontSize(20), fontWeight: "700", color: "#002fa7", fontFamily: "PingFang SC", }, });