import { View, Text, ScrollView, StyleSheet, Pressable } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; import { Alert } from 'react-native'; import NormalButton from '../global/normal_button'; import { router, useLocalSearchParams } from 'expo-router'; import { GrayRightArrowIconSvg, RightArrowIconSvg, TickLogoSvg } from '../global/SVG'; import { useEffect, useRef, useState } from 'react'; import useBookingStore from '../../providers/booking_store'; import useCouponStore from '../../providers/coupon_store'; import { walletService } from '../../service/walletService'; const PaymentSummaryPageComponent = () => { const selectedCouponName = useCouponStore((state) => state.selectedCouponName); const selectedCouponRedeemCode = useCouponStore((state) => state.selectedCouponRedeemCode); const selectedCouponPrice = useCouponStore((state) => state.selectedCouponPrice); const params = useLocalSearchParams(); const setBookingInfo = useBookingStore((state) => state.setBookingInfo); const initialSetupDone = useRef(false); const [formatOrderId, setFormatOrderId] = useState(''); useEffect(() => { if (!initialSetupDone.current && Object.keys(params).length > 0) { const bookingInfo = { bookTime: params.bookTime, date: params.date, carID: params.carID as string, chargingWatt: params.chargingWatt as string, connectorID: params.connectorID as string, price: params.price as string, stationID: params.stationID as string, user: params.userID as string, paymentFee: params.paymentFee as string, carCapacitance: params.carCapacitance as string }; setBookingInfo(bookingInfo); console.log('Booking info stored:', bookingInfo); initialSetupDone.current = true; } }, [params, setBookingInfo]); const { bookTime, date, carID, chargingWatt, connectorID, price, stationID, user, paymentFee, carCapacitance } = useBookingStore(); const calculateFinalFee = (): string => { console.log('selected coupon price:', selectedCouponPrice); if (selectedCouponRedeemCode && selectedCouponPrice) { const difference = parseFloat(paymentFee) - parseFloat(selectedCouponPrice); return Math.max(0, difference).toFixed(2); } else { return paymentFee; } }; const finalFee = calculateFinalFee(); const total_power = chargingWatt === '' ? carCapacitance : parseFloat(chargingWatt.split('~')[0]); const promotion_code = selectedCouponRedeemCode || ''; const convertEndTime = (num1: number) => { switch (num1) { case 20: return 25; case 25: return 30; case 30: return 40; case 40: return 45; } }; function convertToUTC(date, bookTime) { const currentYear = new Date().getFullYear(); const [month, day] = date.split('/'); const fullDate = new Date(`${currentYear}-${month}-${day}T${bookTime}:00`); fullDate.setHours(fullDate.getHours()); return fullDate; } const book_time = convertToUTC(date, bookTime); const timeRequiredInMinute = convertEndTime(Number(chargingWatt.split(' ')[0])); function findEndTime(num1, book_time) { const date = new Date(book_time); date.setMinutes(date.getMinutes() + num1); return date; } let end_time; if (chargingWatt === '') { end_time = book_time; } else { const endingTime = findEndTime(timeRequiredInMinute, book_time); end_time = endingTime; } const handleSubmitPayment = async () => { let type = 'reservation'; let is_ic_call = false; console.log('i am handleSubmitPayment, connectorID:', connectorID); try { const response = await walletService.submitPayment( stationID, connectorID, user, book_time, end_time, total_power, total_fee, promotion_code, carID, type, is_ic_call ); if (response.status === 200 || response.status === 201) { console.log('submit payment successful'); // router.push('/paymentFinishPage'); router.push({ pathname: '/paymentFinishPage', params: { formatOrderId: response.data.format_order_id } }); } else if (response.status === 400) { console.log('400 error in paymentSummaryPageComponent'); Alert.alert('餘額不足', '您的餘額不足,請充值後再試。'); } else { console.log('submit payment failed:', response.status); } } catch (error) { console.log('submit payment error:', error); } }; const total_fee = parseInt(finalFee, 10); // console.log(' i am only console.log all variables for clarity:', { // stationID, // connectorID, // user, // book_time, // end_time, // total_power, // total_fee, // promotion_code, // carID // }); return ( 付款概要 router.push('selectCouponPage')}> 優惠券 {selectedCouponName === '' ? ( 選擇優惠券 ) : ( {selectedCouponName} )} 收費概要 充電費用 HK ${finalFee} {chargingWatt === '' ? ( 充滿停機預估費用 ) : ( 按每度電結算: {chargingWatt?.split('~')[0]} )} 總計 HK$ {finalFee} 前往付款 } onPress={ // () => router.push('/paymentFinishPage') handleSubmitPayment } extendedStyle={{ padding: 24 }} /> ); }; export default PaymentSummaryPageComponent; const styles = StyleSheet.create({ grayColor: { color: '#888888' }, greenColor: { color: '#02677D' } });