import { useState, useEffect } from 'react'; import { View, Text, FlatList, Pressable, StyleSheet, ActivityIndicator, Platform, Modal, TextInput, Alert, } from 'react-native'; import { useRouter, useLocalSearchParams } from 'expo-router'; import { useStoresStore } from '@/store/stores'; import { api } from '@/lib/api'; import type { Store, Aisle } from '@shop/types'; /** * Aisle management screen * Allows managing aisles for a store */ export default function AislesScreen() { const router = useRouter(); const { id } = useLocalSearchParams<{ id: string }>(); const { stores, updateStore } = useStoresStore(); const store = stores.find((s) => s.id === id); const [isLoading, setIsLoading] = useState(false); const [showAddModal, setShowAddModal] = useState(false); const [aisleName, setAisleName] = useState(''); const [aisleColor, setAisleColor] = useState('#FF6B6B'); const [isAddingAisle, setIsAddingAisle] = useState(false); const colors = [ '#FF6B6B', '#4ECDC4', '#45B7D1', '#FFA07A', '#98D8C8', '#F7DC6F', '#BB8FCE', '#85C1E2', ]; const handleAddAisle = async () => { if (!aisleName.trim() || !store) { Alert.alert('Error', 'Please enter an aisle name'); return; } try { setIsAddingAisle(true); const newAisle = await api.post(`/api/stores/${store.id}/aisles`, { name: aisleName, colour: aisleColor, }); // Update local store const updatedStore: Store = { ...store, aisles: [...(store.aisles || []), newAisle], }; updateStore(updatedStore); setAisleName(''); setAisleColor('#FF6B6B'); setShowAddModal(false); } catch (err) { console.error('Failed to add aisle:', err); Alert.alert('Error', 'Failed to add aisle'); } finally { setIsAddingAisle(false); } }; const handleDeleteAisle = async (aisleId: string) => { if (!store) return; Alert.alert('Delete Aisle', 'Are you sure?', [ { text: 'Cancel' }, { text: 'Delete', onPress: async () => { try { await api.delete(`/api/stores/${store.id}/aisles/${aisleId}`); // Update local store const updatedStore: Store = { ...store, aisles: store.aisles?.filter((a) => a.id !== aisleId) || [], }; updateStore(updatedStore); } catch (err) { console.error('Failed to delete aisle:', err); Alert.alert('Error', 'Failed to delete aisle'); } }, }, ]); }; if (!store) { return ( Store not found ); } return ( {/* Header */} router.back()}> ← Back {store.name} Aisles setShowAddModal(true)}> + Add {/* Aisles list */} item.id} renderItem={({ item: aisle }) => ( {aisle.name} Order: {aisle.order} handleDeleteAisle(aisle.id)}> 🗑 )} ListEmptyComponent={ No aisles yet Add an aisle to organize items } /> {/* FAB to add aisle */} setShowAddModal(true)}> + {/* Add Aisle Modal */} setShowAddModal(false)}> Add Aisle Color {colors.map((color) => ( setAisleColor(color)} > {aisleColor === color && ✓} ))} setShowAddModal(false)}> Cancel {isAddingAisle ? ( ) : ( Add )} ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', }, header: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', paddingHorizontal: 16, paddingVertical: 12, marginTop: Platform.OS === 'ios' ? 44 : 0, borderBottomWidth: 1, borderBottomColor: '#e0e0e0', }, backButton: { fontSize: 16, color: '#007AFF', }, titleContainer: { flex: 1, alignItems: 'center', }, title: { fontSize: 16, fontWeight: '600', color: '#000', }, subtitle: { fontSize: 12, color: '#999', marginTop: 2, }, addButton: { fontSize: 16, color: '#007AFF', fontWeight: '600', }, aisleCard: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', paddingVertical: 12, paddingHorizontal: 16, marginVertical: 4, marginHorizontal: 8, backgroundColor: '#f9f9f9', borderRadius: 8, borderLeftWidth: 4, }, aisleContent: { flexDirection: 'row', alignItems: 'center', flex: 1, }, colorSwatch: { width: 32, height: 32, borderRadius: 8, marginRight: 12, }, aisleInfo: { flex: 1, }, aisleName: { fontSize: 16, fontWeight: '600', color: '#000', }, aisleOrder: { fontSize: 12, color: '#999', marginTop: 2, }, deleteButton: { paddingHorizontal: 8, }, deleteButtonText: { fontSize: 18, }, emptyContainer: { alignItems: 'center', justifyContent: 'center', paddingVertical: 48, }, emptyText: { fontSize: 16, fontWeight: '600', color: '#000', marginBottom: 8, }, emptySubtext: { fontSize: 14, color: '#999', }, fab: { position: 'absolute', bottom: 24, right: 24, width: 56, height: 56, borderRadius: 28, backgroundColor: '#007AFF', justifyContent: 'center', alignItems: 'center', shadowColor: '#000', shadowOffset: { width: 0, height: 2 }, shadowOpacity: 0.25, shadowRadius: 3.84, elevation: 5, }, fabText: { fontSize: 32, color: '#fff', fontWeight: '300', }, modalOverlay: { flex: 1, backgroundColor: 'rgba(0, 0, 0, 0.5)', justifyContent: 'flex-end', }, modal: { backgroundColor: '#fff', borderTopLeftRadius: 20, borderTopRightRadius: 20, paddingHorizontal: 16, paddingTop: 20, paddingBottom: 24, }, modalTitle: { fontSize: 18, fontWeight: '700', color: '#000', marginBottom: 16, }, input: { borderWidth: 1, borderColor: '#ddd', borderRadius: 8, paddingHorizontal: 12, paddingVertical: 10, fontSize: 16, marginBottom: 16, backgroundColor: '#f9f9f9', }, colorLabel: { fontSize: 14, fontWeight: '600', color: '#000', marginBottom: 8, }, colorGrid: { flexDirection: 'row', flexWrap: 'wrap', gap: 8, marginBottom: 24, }, colorOption: { width: '22.5%', aspectRatio: 1, borderRadius: 8, justifyContent: 'center', alignItems: 'center', borderWidth: 2, borderColor: 'transparent', }, colorOptionSelected: { borderColor: '#000', }, colorCheckmark: { color: '#fff', fontSize: 18, fontWeight: 'bold', }, modalButtons: { flexDirection: 'row', gap: 8, }, cancelButton: { flex: 1, paddingVertical: 12, borderRadius: 8, borderWidth: 1, borderColor: '#ddd', justifyContent: 'center', alignItems: 'center', }, cancelButtonText: { fontSize: 16, fontWeight: '600', color: '#666', }, addAisleButton: { flex: 1, paddingVertical: 12, backgroundColor: '#007AFF', borderRadius: 8, justifyContent: 'center', alignItems: 'center', }, addAisleButtonText: { color: '#fff', fontSize: 16, fontWeight: '600', }, buttonDisabled: { opacity: 0.6, }, });