import React from 'react'; import { View, TouchableOpacity, Linking, Alert, TextInput, ScrollView, Dimensions } from 'react-native'; import * as Location from 'expo-location'; const { height: SCREEN_HEIGHT } = Dimensions.get('window'); import { MyText, tw, BottomDialog, } from 'common-ui'; import { trpc } from '@/src/trpc-client'; import MaterialIcons from '@expo/vector-icons/MaterialIcons'; import Ionicons from '@expo/vector-icons/Ionicons'; interface OrderOptionsMenuProps { open: boolean; onClose: () => void; order: { id: number; readableId: number; isPackaged: boolean; isDelivered: boolean; isFlashDelivery?: boolean; address: string; addressId: number; adminNotes?: string | null; userNotes?: string | null; latitude?: number | null; longitude?: number | null; status?: string; }; onViewDetails: () => void; onTogglePackaged: () => void; onToggleDelivered: () => void; onOpenAdminNotes: () => void; onCancelOrder: () => void; onAttachLocation: () => void; onWhatsApp: () => void; onDial: () => void; } export function OrderOptionsMenu({ open, onClose, order, onViewDetails, onTogglePackaged, onToggleDelivered, onOpenAdminNotes, onCancelOrder, onAttachLocation, onWhatsApp, onDial, }: OrderOptionsMenuProps) { const updateAddressCoordsMutation = trpc.admin.order.updateAddressCoords.useMutation(); const handleAttachLocation = async () => { try { const { status } = await Location.requestForegroundPermissionsAsync(); if (status !== 'granted') { Alert.alert( 'Permission Denied', 'Location permission is required to attach coordinates.' ); return; } const location = await Location.getCurrentPositionAsync({ accuracy: Location.Accuracy.High, }); const { latitude, longitude } = location.coords; await updateAddressCoordsMutation.mutateAsync({ addressId: order.addressId, latitude, longitude, }); Alert.alert('Success', 'Location attached to address successfully.'); onAttachLocation(); } catch (error) { Alert.alert('Error', 'Failed to attach location. Please try again.'); } }; const extractPhone = (address: string) => { const phoneMatch = address.match(/Phone: (\d+)/); return phoneMatch ? phoneMatch[1] : null; }; const handleWhatsApp = () => { const phone = extractPhone(order.address); if (phone) { Linking.openURL(`whatsapp://send?phone=+91${phone}`); } else { Alert.alert('No phone number found'); } }; const handleDial = () => { const phone = extractPhone(order.address); if (phone) { Linking.openURL(`tel:${phone}`); } else { Alert.alert('No phone number found'); } }; const handleOpenInMaps = () => { if (order.latitude && order.longitude) { const url = `https://www.google.com/maps/search/?api=1&query=${order.latitude},${order.longitude}`; Linking.openURL(url); } else { Alert.alert('No location coordinates available'); } }; const hasCoordinates = order.latitude !== null && order.latitude !== undefined && order.longitude !== null && order.longitude !== undefined; return ( Order #{order.readableId} Select an action to perform { onViewDetails(); onClose(); }} > View Details See full order information true} onResponderEnd={(e) => { e.stopPropagation(); onTogglePackaged(); }} > Packaged {order.isPackaged ? 'Mark as not packaged' : 'Mark as packaged'} true} onResponderEnd={(e) => { e.stopPropagation(); onToggleDelivered(); }} > Delivered {order.isDelivered ? 'Mark as not delivered' : 'Mark as delivered'} { onOpenAdminNotes(); onClose(); }} > Admin Notes {order.adminNotes ? 'Edit existing notes' : 'Add admin notes'} {order.status !== 'cancelled' && ( { onCancelOrder(); onClose(); }} > Cancel Order Cancel and provide reason )} { handleAttachLocation(); onClose(); }} disabled={updateAddressCoordsMutation.isPending} > Attach Location Save GPS coordinates to address {hasCoordinates && ( { handleOpenInMaps(); onClose(); }} > Open in Maps View delivery location on Google Maps )} { handleWhatsApp(); onClose(); }} > Message On WhatsApp Send message via WhatsApp { handleDial(); onClose(); }} > Dial Mobile Number Call customer directly ); }