import { View, Text, Pressable, Dimensions,Image, StyleSheet } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; import { router, useLocalSearchParams } from 'expo-router'; import { CrossLogoSvg } from '../global/SVG'; import { useEffect, useMemo, useState, useRef } from 'react'; import { chargeStationService } from '../../service/chargeStationService'; import { ChargingDetails, Remark, ElectricityPrice, Special } from '../../service/type/chargeStationType'; import { format, parseISO } from 'date-fns'; const ChargingDetailsPageComponent = () => { const screenHeight = Dimensions.get('window').height; const params = useLocalSearchParams(); const [list, setList] = useState({} as ChargingDetails); const [remark, setRemark] = useState({} as Remark); const [time, setTime] = useState(''); useEffect(() => { const fetchData = async () => { const res = await chargeStationService.fetchChargingDetails(params.id.toString()) if (res) { // If res is an array, take the first item; otherwise, set as is setList(Array.isArray(res) ? res[0] : {} as ChargingDetails); setRemark(JSON.parse(res[0]?.remark || '{}') as Remark) // 解析并格式化日期 const startTime = parseISO(res[0].actual_start_time); const endTime = parseISO(res[0].actual_end_time); // 格式化为指定格式 const formattedDate = `${format(startTime, 'yyyy/MM/dd HH:mm:ss')}-${format(endTime, 'HH:mm:ss')}`; setTime(formattedDate) } }; fetchData(); }, []) const totalPrice = useMemo(() => { if (list.promotion_name) { const price = list?.connector?.EquipmentID?.StationID?.price if (price && remark.TotalPower) { return `${(price * remark.TotalPower).toFixed(1)}` } return '0' } else { if (list.total_fee && list.withdraw_fee) { let actual_fee = list.total_fee - list.withdraw_fee const value = actual_fee <= 0? '0': actual_fee % 1 === 0? `${actual_fee}`: `${actual_fee.toFixed(1)}` return value } return '0'; } }, [list]); return ( { if (router.canGoBack()) { router.back(); } else { router.replace('/accountMainPage'); } }} > {/* */} 750 ? 200 : 110, height: screenHeight > 750 ? 200 : 110 }} /> {totalPrice} 訂單编號: {list.format_order_id} 充電時間: {time} 充電站位置: {params.chargeStationName} 實付: {list.promotion_name ? 優惠券支付: null} ${totalPrice} ); }; interface ChargingDataComponentProps { list: ChargingDetails; totalPrice: string; remark: Remark; } const ChargingDataComponent: React.FC = ({ list, totalPrice, remark }) => { const [price, setPrice] = useState(0); const hasFetchedPrice = useRef(false); // 添加 ref 跟踪是否已获取过价格 useEffect(() => { if (!list.promotion_name && !hasFetchedPrice.current && list.actual_start_time) { chargeStationService.fetchElectricityPrice(list.pricemodel_id || 'a').then(res => { hasFetchedPrice.current = true; // 标记为已调用 const date = new Date(list.actual_start_time); const str = (date.toLocaleString('en-US', { weekday: 'short' })).toLowerCase(); if (res && res.length > 0) { if (remark.RushKwh) { const obj = (res[1][str as keyof ElectricityPrice]) as Special setPrice(obj.price) } if (remark.ElseKwh) { const obj = (res[2][str as keyof ElectricityPrice]) as Special setPrice(obj.price) } if (remark.OffKwh) { const obj = (res[0][str as keyof ElectricityPrice]) as Special setPrice(obj.price) } } }) } }, [list.actual_start_time]) if (!list.promotion_name) { return ( {(remark.RushKwh) ? 峰時總電量: {remark.RushKwh?.toFixed(1)} 峰時電價(09:00-20:59): ${price} 峰時總電費: ${totalPrice} : null } {(remark.ElseKwh) ? 平時總電量: {remark.ElseKwh?.toFixed(1)} 平時電價(09:00-20:59): ${price} 平時總電費: ${totalPrice} : null } {(remark.OffKwh) ? 穀時總電量: {remark.OffKwh?.toFixed(1)} 穀時電價(09:00-20:59): ${price} 穀時總電費: ${totalPrice} : null } ) } else { return ( 總電量: {remark.TotalPower?.toFixed(1)} 電價: ${list?.connector?.EquipmentID?.StationID?.price} 總電費: ${totalPrice} ) } } const styles = StyleSheet.create({ viewLine: { width: '100%', height: 1, backgroundColor: '#E5E5E5', }, totalPrice: { fontSize: 26, fontWeight: 'bold', marginBottom: 25, }, leftLable: { fontSize: 18, color:'#888888', }, rightLable: { fontSize: 17 }, }) export default ChargingDetailsPageComponent;