/** * Checklist Screen * ERP Transportistas * Sprint S8 - TASK-007 * * Pre-trip checklist screen. */ import React, { useEffect, useState } from 'react'; import { View, Text, StyleSheet, ScrollView, TouchableOpacity, ActivityIndicator, Alert, } from 'react-native'; import { useNavigation, useRoute } from '@react-navigation/native'; import type { NativeStackNavigationProp } from '@react-navigation/native-stack'; import type { RouteProp } from '@react-navigation/native'; import { Ionicons } from '@expo/vector-icons'; import { useViajeStore } from '../store'; import type { RootStackParamList, ChecklistItem } from '../types'; type NavigationProp = NativeStackNavigationProp; type ChecklistRouteProp = RouteProp; export function ChecklistScreen(): JSX.Element { const navigation = useNavigation(); const route = useRoute(); const { checklist, cargarChecklist, completarItemChecklist, guardarChecklist, isLoading, } = useViajeStore(); const [isSaving, setIsSaving] = useState(false); useEffect(() => { cargarChecklist(route.params.viajeId); }, [route.params.viajeId]); const toggleItem = (itemId: string, currentState: boolean): void => { completarItemChecklist(itemId, !currentState); }; const allRequiredCompleted = (): boolean => { return checklist .filter((item) => item.requerido) .every((item) => item.completado); }; const handleSave = async (): Promise => { if (!allRequiredCompleted()) { Alert.alert( 'Items Pendientes', 'Debes completar todos los items requeridos antes de continuar' ); return; } setIsSaving(true); try { await guardarChecklist(); Alert.alert('Éxito', 'Checklist guardado correctamente', [ { text: 'OK', onPress: () => navigation.goBack() }, ]); } catch (error) { Alert.alert('Error', 'No se pudo guardar el checklist'); } finally { setIsSaving(false); } }; const renderItem = (item: ChecklistItem): JSX.Element => ( toggleItem(item.id, item.completado)} > {item.completado ? ( ) : ( )} {item.descripcion} {item.requerido && ( Requerido )} {item.foto && ( )} ); if (isLoading) { return ( Cargando checklist... ); } const completedCount = checklist.filter((item) => item.completado).length; const totalCount = checklist.length; const progress = totalCount > 0 ? (completedCount / totalCount) * 100 : 0; return ( {/* Progress Header */} Checklist Pre-viaje {completedCount} de {totalCount} completados {/* Checklist Items */} {checklist.length === 0 ? ( No hay items en el checklist ) : ( checklist.map(renderItem) )} {/* Save Button */} {isSaving ? ( ) : ( <> Guardar y Continuar )} ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#f1f5f9', }, loadingContainer: { flex: 1, justifyContent: 'center', alignItems: 'center', }, loadingText: { marginTop: 12, fontSize: 16, color: '#64748b', }, header: { backgroundColor: '#ffffff', padding: 16, borderBottomWidth: 1, borderBottomColor: '#e2e8f0', }, headerTitle: { fontSize: 20, fontWeight: 'bold', color: '#1e293b', }, headerProgress: { fontSize: 14, color: '#64748b', marginTop: 4, }, progressBar: { height: 6, backgroundColor: '#e2e8f0', borderRadius: 3, marginTop: 12, overflow: 'hidden', }, progressFill: { height: '100%', backgroundColor: '#22c55e', borderRadius: 3, }, content: { flex: 1, padding: 16, }, emptyContainer: { alignItems: 'center', paddingVertical: 48, }, emptyText: { fontSize: 16, color: '#64748b', marginTop: 12, }, itemContainer: { backgroundColor: '#ffffff', borderRadius: 12, padding: 16, marginBottom: 8, flexDirection: 'row', alignItems: 'center', shadowColor: '#000', shadowOffset: { width: 0, height: 1 }, shadowOpacity: 0.05, shadowRadius: 2, elevation: 1, }, itemCompleted: { backgroundColor: '#f0fdf4', borderWidth: 1, borderColor: '#bbf7d0', }, itemCheckbox: { marginRight: 12, }, itemContent: { flex: 1, }, itemDescription: { fontSize: 16, color: '#1e293b', }, itemTextCompleted: { color: '#16a34a', }, itemRequired: { fontSize: 12, color: '#f59e0b', marginTop: 4, }, footer: { padding: 16, backgroundColor: '#ffffff', borderTopWidth: 1, borderTopColor: '#e2e8f0', }, saveButton: { backgroundColor: '#22c55e', borderRadius: 12, padding: 16, flexDirection: 'row', alignItems: 'center', justifyContent: 'center', }, saveButtonDisabled: { backgroundColor: '#86efac', }, saveButtonText: { color: '#ffffff', fontSize: 16, fontWeight: '600', marginLeft: 8, }, });