import React, { useState } from 'react'; import { View, Text, StyleSheet, ScrollView, TouchableOpacity, Alert, Platform } from 'react-native'; import Animated, { FadeInUp, FadeInDown, SlideInDown, SlideOutDown } from 'react-native-reanimated'; import { useBarbearia } from '../../stores/BarbeariaContext'; import { useLanguage } from '../../stores/LanguageContext'; import { COLORS, SPACING, TYPOGRAPHY, BORDER_RADIUS, SHADOWS } from '../../constants/theme'; import { Button } from '../../components/ui/Button'; import { format, startOfToday, addDays } from 'date-fns'; import { ptBR, es } from 'date-fns/locale'; import { ChevronLeft, ChevronRight, Calendar as CalendarIcon, Lock, CheckCircle2, X } from 'lucide-react-native'; const ALL_TIMES = [ '09:00', '09:30', '10:00', '10:30', '11:00', '11:30', '13:30', '14:00', '14:30', '15:00', '15:30', '16:00', '16:30', '17:00', '17:30', '18:00' ]; interface SelectedSlot { barberId: string; time: string; } export default function AdminAgenda() { const { barbearia, updateBlockedSlots, activeBarberId } = useBarbearia(); const { t, language } = useLanguage(); const [selectedDate, setSelectedDate] = useState(startOfToday()); const [selectedSlots, setSelectedSlots] = useState([]); const isOwner = !activeBarberId; const appointments = barbearia?.appointments || []; const barbers = barbearia?.barbers || []; const blockedSlots = barbearia?.blockedSlots || []; const themeColors = barbearia?.colors || COLORS; const primaryColor = themeColors.primary; const dateFormatted = format(selectedDate, 'dd/MM/yyyy'); const dateLocale = language === 'pt' ? ptBR : es; const changeDate = (amount: number) => { setSelectedDate(prev => addDays(prev, amount)); setSelectedSlots([]); // Clear selection when changing days }; const handleSlotPress = (barberId: string, time: string, hasAppointment: boolean) => { if (!isOwner && barberId !== activeBarberId) { if (Platform.OS === 'web') window.alert('Você só pode alterar a sua própria agenda.'); else Alert.alert('Acesso Negado', 'Você só pode alterar a sua própria agenda.'); return; } if (hasAppointment) { if (Platform.OS === 'web') window.alert('Horário ocupado por agendamento.'); else Alert.alert('Aviso', 'Este horário já possui um agendamento.'); return; } setSelectedSlots(prev => { const exists = prev.find(s => s.barberId === barberId && s.time === time); if (exists) { return prev.filter(s => !(s.barberId === barberId && s.time === time)); } else { return [...prev, { barberId, time }]; } }); }; const applyAction = async (action: 'block' | 'unblock') => { if (selectedSlots.length === 0) return; const slotsToUpdate = selectedSlots.map(s => ({ barberId: s.barberId, time: s.time, date: dateFormatted })); await updateBlockedSlots(slotsToUpdate, action); setSelectedSlots([]); // Limpa a seleção após aplicar }; const isSlotSelected = (barberId: string, time: string) => { return selectedSlots.some(s => s.barberId === barberId && s.time === time); }; return ( {t('admin.agenda.title')} changeDate(-1)} style={styles.navBtn}> {format(selectedDate, "EEEE, dd 'de' MMMM", { locale: dateLocale })} changeDate(1)} style={styles.navBtn}> {t('admin.agenda.time')} {barbers.map(barber => ( {barber.nome} ))} {ALL_TIMES.map(time => ( {time} {barbers.map(barber => { const appointment = appointments.find(a => a.date === dateFormatted && a.time === time && a.barberId === barber.id && a.status !== 'rejected' ); const isBlocked = blockedSlots.some(s => s.barberId === barber.id && s.date === dateFormatted && s.time === time); const isSelected = isSlotSelected(barber.id, time); return ( handleSlotPress(barber.id, time, !!appointment)} activeOpacity={0.7} style={[ styles.cell, styles.barberColumn, { borderColor: themeColors.divider }, appointment ? (appointment.status === 'accepted' ? styles.busyCell : styles.pendingCell) : (isBlocked ? styles.blockedCell : styles.freeCell), isSelected && { borderColor: primaryColor, borderWidth: 2, backgroundColor: `${primaryColor}20` } ]} > {isSelected && !appointment && ( )} {appointment ? ( {appointment.clientName} ) : isBlocked ? ( Bloqueado ) : ( {t('admin.agenda.free')} )} ); })} ))} {/* Action Bar (Multi-Select) */} {selectedSlots.length > 0 && ( {`${selectedSlots.length} horário${selectedSlots.length > 1 ? 's' : ''} selecionado${selectedSlots.length > 1 ? 's' : ''}`} setSelectedSlots([])} style={styles.clearBtn}>