import React, { useState } from "react"; import FullCalendar from "@fullcalendar/react"; import dayGridPlugin from "@fullcalendar/daygrid"; import timeGridPlugin from "@fullcalendar/timegrid"; import interactionPlugin from "@fullcalendar/interaction"; import ptBrLocale from "@fullcalendar/core/locales/pt-br"; import "../../assets/css/index.css"; function Calendar1() { const [events, setEvents] = useState([]); const [editingEvent, setEditingEvent] = useState(null); const [showPopup, setShowPopup] = useState(false); const [showActionModal, setShowActionModal] = useState(false); const [step, setStep] = useState(1); const [newEvent, setNewEvent] = useState({ title: "", time: "" }); const [selectedDate, setSelectedDate] = useState(null); const [selectedEvent, setSelectedEvent] = useState(null); const colorsByType = { Rotina: "#4dabf7", Cardiologia: "#f76c6c", Otorrino: "#f7b84d", Pediatria: "#6cf78b" }; // Clicar em um dia -> abrir popup 3 etapas const handleDateClick = (arg) => { setSelectedDate(arg.dateStr); setNewEvent({ title: "", time: "" }); setStep(1); setEditingEvent(null); setShowPopup(true); }; // Adicionar nova consulta const handleAddEvent = () => { const eventToAdd = { id: Date.now(), // number title: newEvent.title, time: newEvent.time, date: selectedDate, color: colorsByType[newEvent.type] || "#4dabf7" }; setEvents((prev) => [...prev, eventToAdd]); setShowPopup(false); }; // Editar consulta existente const handleEditEvent = () => { setEvents((prevEvents) => prevEvents.map((ev) => ev.id.toString() === editingEvent.id.toString() ? { ...ev, title: newEvent.title, time: newEvent.time, color: colorsByType[newEvent.type] || "#4dabf7" } : ev ) ); setEditingEvent(null); setShowPopup(false); setShowActionModal(false); }; // Próxima etapa no popup const handleNextStep = () => { if (step < 2) setStep(step + 1); else editingEvent ? handleEditEvent() : handleAddEvent(); }; // Clicar em uma consulta -> abre modal de ação (Editar/Apagar) const handleEventClick = (clickInfo) => { setSelectedEvent(clickInfo.event); setShowActionModal(true); }; // Apagar consulta const handleDeleteEvent = () => { if (!selectedEvent) return; setEvents((prevEvents) => prevEvents.filter( (ev) => ev.id.toString() !== selectedEvent.id.toString() ) ); setShowActionModal(false); }; // Começar a editar const handleStartEdit = () => { if (!selectedEvent) return; setEditingEvent(selectedEvent); setNewEvent({ title: selectedEvent.title, time: selectedEvent.extendedProps.time }); setStep(1); setShowActionModal(false); setShowPopup(true); }; // Aparência da consulta dentro do calendário const renderEventContent = (eventInfo) => { const bg = eventInfo.event.backgroundColor || eventInfo.event.extendedProps?.color || "#4dabf7"; return (
{selectedEvent.extendedProps.type} às {selectedEvent.extendedProps.time}