import { router, useLocalSearchParams } from 'expo-router'; import { View, Text, ScrollView, Pressable, Image, Dimensions, Alert, ActivityIndicator } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; import { PreviousPageBlackSvg, StarSvg } from '../global/SVG'; import NormalButton from '../global/normal_button'; import { chargeStationService } from '../../service/chargeStationService'; import { useState } from 'react'; import { formatDate } from '../../util/lib'; const ManageVehiclePageComponent = () => { const { height: deviceHeight, width: deviceWidth } = Dimensions.get('window'); const [loading, setLoading] = useState(false); const [loading2, setLoading2] = useState(false); const params = useLocalSearchParams(); const carID = params?.carID; const carCapacitance = params?.capacitance; const carCapacitanceUnit = params?.capacitance_unit; const carCreatedAt = params?.createdAt; const carModel = params?.carModel; const carImage = params.carImage; const licensePlate = params?.licensePlate; console.log(carID, carCapacitance, carCapacitanceUnit, carCreatedAt); const handleDeleteCar = async (carID) => { const confirmDelete = () => { return new Promise((resolve) => { Alert.alert( 'Confirm Delete', 'Are you sure you want to delete this car?', [ { text: 'No', onPress: () => resolve(false), style: 'cancel' }, { text: 'Yes', onPress: () => resolve(true) } ], { cancelable: false } ); }); }; const userConfirmed = await confirmDelete(); if (userConfirmed) { try { setLoading2(true); const isDeleted = await chargeStationService.deleteCar(carID); if (isDeleted) { Alert.alert('Deletion Successful', 'Car has been deleted'); router.push('/mainPage'); } else { // Show an error message if deletion was not successful Alert.alert('Deletion Failed', 'Unable to delete the car. Please try again.'); } } catch (error) { console.error('Error deleting car:', error); Alert.alert('Error', 'An error occurred while deleting the car.'); } finally { setLoading2(false); } } }; const handleSetDefaultCar = async (carID) => { setLoading(true); try { const isSetDefault = await chargeStationService.setDefaultCar(carID); if (isSetDefault) { Alert.alert('設置成功', '已成功設置預設車輛'); router.push('/mainPage'); } else { console.log('Unable to set default car'); } } catch (error) { console.log(error); } finally { setLoading(false); } }; return ( { if (router.canGoBack()) { router.back(); } else { router.replace('/accountMainPage'); } }} > 管理車輛 {carModel} {licensePlate} 設置中... ) : ( 設置為預設車輛 ) } onPress={() => { if (!loading) { handleSetDefaultCar(carID); } }} extendedStyle={{ marginTop: 24 }} disabled={loading} /> 車輛資訊 加入日期 {formatDate(carCreatedAt)} 總充電量 {carCapacitance + carCapacitanceUnit} 上一次充電 待DATA {loading2 ? '删除車輛中...' : '删除車輛'} } onPress={() => { handleDeleteCar(carID); }} extendedStyle={{ backgroundColor: '#D40000' }} /> ); }; export default ManageVehiclePageComponent;