import { useAlert } from '@/components/AlertComponent'; import CalendarWidget from '@/components/CalendarWidget'; import LoadingScreen from '@/components/LoadingScreen'; import RequestPermitModal from '@/components/RequestPermitModal'; import { TimeOffRequest, TimeOffRequestType } from '@/types/types'; import api from '@/utils/api'; import { formatDate, formatTime } from '@/utils/dateTime'; import { Calendar as CalendarIcon, CalendarX, Clock, Plus, Thermometer } from 'lucide-react-native'; import React, { JSX, useEffect, useMemo, useState } from 'react'; import { RefreshControl, ScrollView, Text, TouchableOpacity, View } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; // Icon Mapping const typeIcons: Record JSX.Element> = { Ferie: (color) => , Permesso: (color) => , Malattia: (color) => , Assenza: (color) => , }; export default function PermitsScreen() { const [showModal, setShowModal] = useState(false); const alert = useAlert(); const [permits, setPermits] = useState([]); const [types, setTypes] = useState([]); const [currentMonthDate, setCurrentMonthDate] = useState(new Date()); const [isLoading, setIsLoading] = useState(true); const [refreshing, setRefreshing] = useState(false); const fetchPermits = async () => { try { if (!refreshing) setIsLoading(true); // Fetch Permits const response = await api.get('/time-off-request/list'); setPermits(response.data); // Fetch Types const typesResponse = await api.get('/time-off-request/get-types'); setTypes(typesResponse.data); } catch (error) { console.error('Errore nel recupero dei permessi:', error); alert.showAlert('error', 'Errore', 'Impossibile recuperare i permessi. Riprova più tardi.'); } finally { setIsLoading(false); setRefreshing(false); } }; const filteredPermits = useMemo(() => { if (!permits.length) return []; // Calculate start and end of the current month const year = currentMonthDate.getFullYear(); const month = currentMonthDate.getMonth(); const startOfMonth = new Date(year, month, 1); // Day 0 of the next month = last day of the current month const endOfMonth = new Date(year, month + 1, 0, 23, 59, 59); return permits.filter(item => { const itemStart = new Date(item.start_date?.toString() ?? ''); // If there's no end_date, assume it's a single day (so end = start) const itemEnd = item.end_date ? new Date(item.end_date?.toString() ?? '') : new Date(item.start_date?.toString() ?? ''); // The permit is visible if it starts before the end of the month // And ends after the start of the month. return itemStart <= endOfMonth && itemEnd >= startOfMonth; }); }, [permits, currentMonthDate]); useEffect(() => { fetchPermits(); }, []); const onRefresh = () => { setRefreshing(true); fetchPermits(); }; if (isLoading && !refreshing) { return ; } return ( setShowModal(false)} onSubmit={(data) => { console.log('Richiesta:', data); fetchPermits(); }} /> {/* Header */} Ferie e Permessi Gestisci le tue assenze } > {/* Calendar Widget */} setCurrentMonthDate(date)} /> {/* Recent Requests List */} {filteredPermits.length === 0 ? ( Nessuna richiesta di permesso questo mese ) : ( Le tue richieste {filteredPermits.map((item) => ( {typeIcons[item.timeOffRequestType.name]?.(item.timeOffRequestType.color)} {item.timeOffRequestType.name} {formatDate(item.start_date?.toLocaleString())} {item.end_date ? `- ${formatDate(item.end_date.toLocaleString())}` : ''} {item.timeOffRequestType.name === 'Permesso' && ( {formatTime(item.start_time)} - {formatTime(item.end_time)} )} {/* TODO: Add functionality to edit/remove the request */} {item.status ? 'Approvato' : 'In Attesa'} ))} )} {/* FAB */} setShowModal(true)} className="absolute bottom-8 right-6 w-16 h-16 bg-[#099499] rounded-full shadow-lg items-center justify-center active:scale-90" > ); }