import React, { useEffect, useState } from "react"; import { View, Text, StyleSheet, TouchableOpacity, ActivityIndicator, ScrollView, Image, Alert, Linking, SafeAreaView, StatusBar, Platform, Modal, } from "react-native"; import { useTranslation } from "react-i18next"; import BackIcon from "../../components/BackIcon"; import MassageIcon from "../../components/MassageIcon"; import fontSize from "../../utils/fontsizeUtils"; import { useNavigation } from "@react-navigation/native"; import { NativeStackNavigationProp } from "@react-navigation/native-stack"; import { useRoute, RouteProp } from "@react-navigation/native"; import { ordersApi, OrderDetailsType } from "../../services/api/orders"; import { payApi } from "../../services/api/payApi"; import useUserStore from "../../store/user"; import OrderIcon from "../../components/OrderIcon"; import InfoIcon from "../../components/InfoIcon"; import Progress from "./Progress"; import AddressIcon from "../../components/AddressIcon"; import EditIcon from "../../components/ColorfulEditIcon"; import BrightnessIcon from "../../components/BrightnessIcon"; import PhoneIcon from "../../components/PhoneIcon"; import ShoppingBagIcon from "../../components/ShoppingBagIcon"; import PowerIcon from "../../components/PowerIcon"; import CardIcon from "../../components/ShoppingCartIcon"; import WhatsAppIcon from "../../components/WatchAppIcon"; import { useOrderListStore } from "../../store/orderList"; export const OrderDetails = () => { const navigation = useNavigation>(); const { t } = useTranslation(); const route = useRoute< RouteProp< { OrderDetails: { orderId: string; }; }, "OrderDetails" > >(); const [orderDetails, setOrderDetails] = useState(); const [isLoading, setIsLoading] = useState(true); const { deleteOrder, changeOrder,updateOrderShippingInfo } = useOrderListStore(); const [showPaymentModal, setShowPaymentModal] = useState(false); const [selectedPayment, setSelectedPayment] = useState(null); const [currentTab, setCurrentTab] = useState("online"); const [tabs, setTabs] = useState([ { id: "online", label: t("order.payment.online"), options: [ { id: "Brainnel Pay", label: "Brainnel Pay", icon: "💳" }, { id: "Wave", label: "Wave", icon: "💸" }, { id: "Paypal", label: "Paypal", icon: "🅿️" }, { id: "Bank Card", label: t("order.payment.bank_card"), icon: "💳" }, ], }, { id: "offline", label: t("order.payment.offline"), options: [], }, ]); const [selectedCurrency, setSelectedCurrency] = useState("USD"); const [convertedAmount, setConvertedAmount] = useState<{ converted_amount: number; item_key: string; original_amount: number; }[]>([]); const [isConverting, setIsConverting] = useState(false); const { user } = useUserStore(); const [isPaymentLoading, setIsPaymentLoading] = useState(false); const getOrderDetails = async () => { try { setIsLoading(true); const response = await ordersApi.getOrderDetails(route.params.orderId); setOrderDetails(response); } catch (error) { console.error("Error fetching order details:", error); } finally { setIsLoading(false); } }; useEffect(() => { getOrderDetails(); }, []); //拨打电话 const callPhone = async (phoneNumber: string) => { const url = `tel:${phoneNumber}`; try { await Linking.openURL(url); // 直接尝试打开拨号界面 } catch (error) { Alert.alert(t("error"), t("order.error.phone_open")); console.error("拨号失败:", error); } }; const onSelectPayment = (paymentId: string) => { if (paymentId === "Paypal") { setIsConverting(true); const data = { from_currency: orderDetails?.currency || "", to_currency: selectedCurrency, amounts: { total_amount: orderDetails?.total_amount || 0, domestic_shipping_fee: orderDetails?.domestic_shipping_fee || 0, shipping_fee: orderDetails?.shipping_fee || 0, }, }; payApi .convertCurrency(data) .then((res) => { setConvertedAmount(res.converted_amounts_list); setIsConverting(false); }) .catch((error) => { console.error("Currency conversion failed:", error); setIsConverting(false); }); } setSelectedPayment(paymentId); }; const onSelectCurrency = (currency: string) => { setSelectedCurrency(currency); setIsConverting(true); const data = { from_currency: orderDetails?.currency || "", to_currency: currency, amounts: { total_amount: orderDetails?.total_amount || 0, domestic_shipping_fee: orderDetails?.domestic_shipping_fee || 0, shipping_fee: orderDetails?.shipping_fee || 0, }, }; payApi .convertCurrency(data) .then((res) => { setConvertedAmount(res.converted_amounts_list); setIsConverting(false); }) .catch((error) => { console.error("Currency conversion failed:", error); setIsConverting(false); }); }; const handlePaymentConfirm = async () => { if (!selectedPayment || !orderDetails?.order_id) return; setIsPaymentLoading(true); const paymentData = { order_id: orderDetails.order_id, payment_method: selectedPayment, currency: selectedPayment === "Paypal" ? selectedCurrency : user.currency, total_amount: selectedPayment === "Paypal" ? convertedAmount.reduce((acc, item) => acc + item.converted_amount, 0) : orderDetails?.total_amount || 0, actual_amount: selectedPayment === "Paypal" ? convertedAmount.reduce((acc, item) => acc + item.converted_amount, 0) : orderDetails?.actual_amount || 0, shipping_fee: selectedPayment === "Paypal" ? convertedAmount.find(item => item.item_key === "shipping_fee")?.converted_amount || 0 : orderDetails?.shipping_fee || 0, domestic_shipping_fee: selectedPayment === "Paypal" ? convertedAmount.find(item => item.item_key === "domestic_shipping_fee")?.converted_amount || 0 : orderDetails?.domestic_shipping_fee || 0 }; try { await ordersApi.updateOrderPaymentMethod(paymentData) const payData = { order_id: orderDetails.order_id, method: selectedPayment, currency: selectedPayment === "Paypal" ? selectedCurrency : user.currency, amount: selectedPayment === "Paypal" ? convertedAmount.reduce((acc, item) => acc + item.converted_amount, 0) : orderDetails?.total_amount || 0 }; payApi .getPayInfo(payData) .then((res) => { if (res.success) { setIsPaymentLoading(false); setShowPaymentModal(false); navigation.navigate("Pay", { payUrl: res.payment_url, }); } }) .catch((err) => { Alert.alert(t("error"), t("order.error.payment_update")); setIsPaymentLoading(false); }) .finally(() => { setIsPaymentLoading(false); }); } catch (error) { Alert.alert(t("error"), t("order.error.payment_update")); setIsPaymentLoading(false); } }; return ( navigation.goBack()}> {t("order.details")} {isLoading ? ( ) : orderDetails ? ( {t("order.status")} {/* 订单信息 */} {t("order.information")} {t("order.id")} {orderDetails.order_id} {t("order.create_time")} {orderDetails.create_time} {t("order.shipping_type")} {orderDetails.shipping_type === 0 ? t("order.shipping.sea") : t("order.shipping.air")} {/* 配送信息 */} {t("order.delivery_info")} {t("order.warehouse")} {orderDetails.receiver_address} {t("order.contact_after_payment")} {t("order.recipient")} {/* 姓名 */} {orderDetails.receiver_name} {/* 电话 */} {orderDetails.receiver_phone} {/* watchApp */} WhatsApp: {orderDetails.receiver_phone} {/* 商品信息 */} {t("order.product_info")} ({orderDetails.items.length}) {orderDetails.items.map((item, index) => ( {item.product_name} {item.sku_attributes.map((sku, index) => ( {sku.attribute_name}:{sku.attribute_value} ))} {/* {item.product_name} */} {item.total_price} x{item.quantity} ))} {t("order.add_to_cart")} {/* 价格信息 */} {t("order.price_details")} {t("order.subtotal")} {orderDetails.total_amount} {t("order.platform_shipping")} {orderDetails.domestic_shipping_fee} {t("order.international_shipping")} {orderDetails.shipping_fee} {t("order.total")} {orderDetails.total_amount} + ${orderDetails.shipping_fee} {t("order.estimated_shipping")} (COD) {/* 代付款 */} {orderDetails.order_status === 0 && ( { deleteOrder(route.params.orderId); }} > {t("order.cancel")} { setShowPaymentModal(true); }} > {t("order.pay")} )} {/* 待发货 */} {orderDetails.order_status === 1 && ( { callPhone("15903995548"); }} > {t("order.contact_shipping")} { changeOrder(route.params.orderId, 2); navigation.goBack(); }} > {t("order.cancel")} )} {/* 代收货 */} {orderDetails.order_status === 2 && ( {}}> {t("order.check_logistics")} { updateOrderShippingInfo(route.params.orderId,{ shipping_status: 0, shipping_info: { shipping_company: "string", shipping_no: "string", shipping_info: {} } }); navigation.goBack(); }} > {t("order.confirm_receipt")} )} {/* 已完成 */} {orderDetails.order_status === 3 && ( { deleteOrder(route.params.orderId); navigation.goBack(); }} > {t("order.cancel")} {t("order.pay_now")} )} {/* 已取消 */} {orderDetails.order_status === 4 && ( {}}> {t("order.add_to_cart")} {t("order.reorder")} )} ) : ( {t("order.unable_to_load")} )} setShowPaymentModal(false)} > {t("order.select_payment")} setShowPaymentModal(false)}> × {tabs.map((tab) => ( setCurrentTab(tab.id)} > {tab.label} ))} {tabs .find((tab) => tab.id === currentTab) ?.options.map((option) => ( onSelectPayment(option.id)} > {option.icon} {option.label} {/* PayPal Currency Selection */} {selectedPayment === "Paypal" && option.id === "Paypal" && ( {t("order.select_currency")} onSelectCurrency("USD")} > USD onSelectCurrency("EUR")} > EUR {!isConverting && ( {selectedCurrency === "USD" ? "$" : "€"} {convertedAmount .reduce((acc, item) => acc + item.converted_amount, 0) .toFixed(2)} )} {isConverting && ( )} )} ))} setShowPaymentModal(false)} > {t("cancel")} {isPaymentLoading ? ( ) : ( {t("order.confirm_payment")} )} ); }; const styles = StyleSheet.create({ safeArea: { flex: 1, backgroundColor: '#fff', }, safeAreaContent: { flex: 1, paddingTop: 0, }, container: { flex: 1, backgroundColor: "#f8f9fa", }, header: { flexDirection: "row", justifyContent: "space-between", alignItems: "center", padding: 16, backgroundColor: "#fff", shadowColor: "#000", shadowOffset: { width: 0, height: 2, }, shadowOpacity: 0.1, shadowRadius: 3, elevation: 3, }, title: { fontSize: fontSize(16), fontWeight: "600", }, orderStatus: { paddingInline: 16, marginTop: 10, }, orderStatusContent: { backgroundColor: "#fff", borderRadius: 16, }, orderStatusTitle: { flexDirection: "row", alignItems: "center", borderBottomWidth: 1, borderColor: "#f5f5f5", padding: 10, }, orderStatusTitleText: { fontSize: fontSize(16), fontWeight: "600", marginLeft: 10, }, orderStatusContentPreview: { padding: 10, justifyContent: "center", }, productItem: { flexDirection: "row", width: "100%", borderBottomWidth: 1, borderColor: "#f5f5f5", padding: 10, }, productItemImage: { width: "15%", height: 50, borderRadius: 10, }, productItemInfo: { width: "75%", justifyContent: "space-between", paddingLeft: 10, }, productItemNum: { width: "10%", alignItems: "center", justifyContent: "flex-end", }, productItemNumText: { fontSize: fontSize(16), color: "#999", }, productItemInfoName: { width: "100%", paddingVertical: 5, }, productItemInfoNameText: { fontSize: fontSize(13), fontWeight: "600", }, productItemInfoSkuText: { fontSize: fontSize(13), color: "#999", }, productItemInfoSku: { width: "100%", paddingVertical: 5, }, productItemInfoPrice: { width: "100%", paddingVertical: 5, }, orderStatusContentPreviewInformation: { flexDirection: "row", justifyContent: "space-between", width: "100%", }, loadingContainer: { flex: 1, justifyContent: "center", alignItems: "center", }, orderId: { flexDirection: "row", justifyContent: "space-between", paddingVertical: 10, width: "100%", }, orderIdText: { color: "#999", width: "50%", fontSize: fontSize(14), }, orderIdText1: { width: "50%", textAlign: "right", fontSize: fontSize(14), }, TotalText: { color: "#f77f3a", fontSize: fontSize(18), fontWeight: "600", width: "50%", }, TotalPrice: { color: "#f77f3a", fontSize: fontSize(18), fontWeight: "600", width: "50%", textAlign: "right", }, warehouse: { width: "50%", }, recipientTitle: { paddingVertical: 5, }, recipientPhoneContainer: { flexDirection: "row", alignItems: "center", }, recipient: { width: "50%", }, orderStatusContentPreviewInformationText: { fontSize: fontSize(16), fontWeight: "600", }, warehousePhone: { padding: 5, backgroundColor: "#f9f9f9", borderRadius: 10, width: "90%", flexDirection: "row", alignItems: "center", }, warehousePhoneText: { fontSize: fontSize(14), fontWeight: "400", color: "#3D3D3D", }, warehousePhoneTextContainer: { paddingRight: 5, }, recipientName: { fontSize: fontSize(16), fontWeight: "600", }, recipientPhone: { fontSize: fontSize(14), fontWeight: "400", color: "#3D3D3D", }, dottedLine: { width: "100%", borderBottomWidth: 2, borderColor: "#f5f5f5", borderStyle: "dashed", }, orderRemakeText: { fontSize: fontSize(14), fontWeight: "400", color: "#999", }, addCard: { width: "100%", justifyContent: "center", alignItems: "center", }, addCardBox: { padding: 10, borderWidth: 1, borderColor: "#0098ef", borderRadius: 10, backgroundColor: "#e2f2fd", opacity: 0.5, flexDirection: "row", alignItems: "center", }, addCardText: { fontSize: fontSize(16), fontWeight: "600", color: "#0098ef", marginLeft: 5, }, bottomButtons: { position: "absolute", bottom: 0, width: "100%", flexDirection: "row", justifyContent: "center", alignItems: "center", padding: 10, backgroundColor: "#fff", shadowColor: "#000", shadowOffset: { width: 0, height: -4, }, shadowOpacity: 0.1, shadowRadius: 3, elevation: 3, }, bottomButton1: { padding: 10, marginHorizontal: 10, alignItems: "center", backgroundColor: "#f0f0f0", borderRadius: 5, flex: 1, }, bottomButton: { padding: 10, marginHorizontal: 10, alignItems: "center", backgroundColor: "#fe7f42", borderRadius: 5, flex: 1, }, bottomButtonText: { fontSize: fontSize(18), fontWeight: "600", color: "#fff", }, bottomButtonText1: { fontSize: fontSize(18), fontWeight: "600", color: "#999", }, modalOverlay: { flex: 1, backgroundColor: 'rgba(0, 0, 0, 0.5)', justifyContent: 'flex-end', }, modalContent: { backgroundColor: '#fff', borderTopLeftRadius: 20, borderTopRightRadius: 20, padding: 20, maxHeight: '80%', }, modalHeader: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', marginBottom: 20, }, modalTitle: { fontSize: fontSize(18), fontWeight: '600', }, closeButton: { fontSize: fontSize(24), color: '#999', }, tabs: { flexDirection: 'row', marginBottom: 15, borderBottomWidth: 1, borderBottomColor: '#f5f5f5', }, tab: { paddingVertical: 10, paddingHorizontal: 15, marginRight: 10, }, tabActive: { borderBottomWidth: 2, borderBottomColor: '#FF5100', }, tabText: { fontSize: fontSize(16), color: '#666', }, tabTextActive: { color: '#FF5100', fontWeight: '500', }, paymentOptions: { maxHeight: 300, }, paymentOption: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', padding: 15, backgroundColor: '#F8F8F8', borderRadius: 8, marginBottom: 10, }, paymentSelected: { backgroundColor: '#FFF0E8', borderWidth: 1, borderColor: '#FF5100', }, paymentContent: { flex: 1, }, defaultPaymentContainer: { flexDirection: 'row', alignItems: 'center', }, paymentIcon: { fontSize: fontSize(24), marginRight: 8, }, paymentLabel: { fontSize: fontSize(16), fontWeight: '500', }, radioButton: { width: 20, height: 20, borderRadius: 10, borderWidth: 2, borderColor: '#FF5100', alignItems: 'center', justifyContent: 'center', }, radioInner: { width: 10, height: 10, borderRadius: 5, backgroundColor: 'transparent', }, radioInnerSelected: { backgroundColor: '#FF5100', }, modalFooter: { flexDirection: 'row', justifyContent: 'space-between', marginTop: 20, paddingTop: 20, borderTopWidth: 1, borderTopColor: '#f5f5f5', }, cancelButton: { flex: 1, padding: 15, marginRight: 10, borderRadius: 25, borderWidth: 1, borderColor: '#999', alignItems: 'center', }, confirmButton: { flex: 1, padding: 15, marginLeft: 10, borderRadius: 25, backgroundColor: '#FF5100', alignItems: 'center', }, disabledButton: { backgroundColor: '#ccc', }, cancelButtonText: { fontSize: fontSize(16), color: '#666', }, confirmButtonText: { fontSize: fontSize(16), color: '#fff', fontWeight: '600', }, currencySelectorContainer: { padding: 15, backgroundColor: '#f8f8f8', borderRadius: 8, marginTop: 10, }, currencySelectorTitle: { fontSize: fontSize(14), color: '#666', marginBottom: 10, }, currencyOptions: { flexDirection: 'row', marginBottom: 10, }, currencyOption: { paddingVertical: 8, paddingHorizontal: 15, borderRadius: 20, borderWidth: 1, borderColor: '#ddd', marginRight: 10, }, selectedCurrencyOption: { backgroundColor: '#FFF0E8', borderColor: '#FF5100', }, currencyText: { fontSize: fontSize(14), color: '#333', }, totalContainer: { flexDirection: 'row', alignItems: 'center', justifyContent: 'flex-end', }, totalText: { fontSize: fontSize(16), fontWeight: '600', color: '#FF5100', }, loadingIndicator: { marginLeft: 10, }, });