"use client"; import { useState } from "react"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { Calendar, Clock, User, DollarSign, Eye } from "lucide-react"; import { toast } from "sonner"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; type Appointment = { id: string; date: Date; timeSlot: string; status: string; notes: string | null; dentist: { name: string; image: string | null; }; service: { name: string; price: number | string; }; payment: { status: string; amount: number; } | null; }; type AppointmentsListProps = { appointments: Appointment[]; }; export function AppointmentsList({ appointments }: AppointmentsListProps) { const [isLoading, setIsLoading] = useState(null); const [selectedAppointment, setSelectedAppointment] = useState(null); const upcomingAppointments = appointments.filter( (apt) => new Date(apt.date) >= new Date() && apt.status !== "cancelled" ); const pastAppointments = appointments.filter( (apt) => new Date(apt.date) < new Date() || apt.status === "cancelled" ); const handleCancelAppointment = async (appointmentId: string) => { if (!confirm("Are you sure you want to cancel this appointment?")) { return; } setIsLoading(appointmentId); try { const response = await fetch(`/api/appointments/${appointmentId}`, { method: "PATCH", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ status: "cancelled", cancelReason: "Cancelled by patient", }), }); if (!response.ok) { throw new Error("Failed to cancel appointment"); } toast.success("Appointment cancelled successfully"); window.location.reload(); } catch (error) { console.error(error); toast.error("Failed to cancel appointment"); } finally { setIsLoading(null); } }; const getStatusBadge = (status: string) => { const variants: Record< string, "default" | "secondary" | "destructive" | "outline" > = { pending: "secondary", confirmed: "default", cancelled: "destructive", completed: "outline", rescheduled: "secondary", }; return ( {status.toUpperCase()} ); }; const getPaymentBadge = (status: string) => { const variants: Record< string, "default" | "secondary" | "destructive" | "outline" > = { paid: "default", pending: "secondary", failed: "destructive", refunded: "outline", }; return ( {status.toUpperCase()} ); }; const formatPrice = (price: number | string): string => { if (typeof price === "string") { return price; } if (isNaN(price)) { return "Contact for pricing"; } return `₱${price.toLocaleString()}`; }; const renderAppointmentCard = (appointment: Appointment) => (
{appointment.service.name} Dr. {appointment.dentist.name}
{getStatusBadge(appointment.status)}
{new Date(appointment.date).toLocaleDateString()}
{appointment.timeSlot}
{formatPrice(appointment.service.price)}
{appointment.payment && (
Payment: {getPaymentBadge(appointment.payment.status)}
)}
{appointment.notes && (

Notes:

{appointment.notes}

)} {appointment.status === "pending" || appointment.status === "confirmed" ? (
) : ( )} {appointment.status === "completed" && appointment.payment?.status === "pending" && ( )}
); return ( <> Upcoming ({upcomingAppointments.length}) Past ({pastAppointments.length}) {upcomingAppointments.length === 0 ? (

No upcoming appointments

) : ( upcomingAppointments.map(renderAppointmentCard) )}
{pastAppointments.length === 0 ? (

No past appointments

) : ( pastAppointments.map(renderAppointmentCard) )}
{/* Appointment Details Dialog */} setSelectedAppointment(null)} >
Appointment Details Booking ID: {selectedAppointment?.id}
{selectedAppointment && getStatusBadge(selectedAppointment.status)}
{selectedAppointment && (
{/* Service Information */}

Service Information

Service

{selectedAppointment.service.name}

Price

{formatPrice(selectedAppointment.service.price)}

{/* Appointment Details */}

Appointment Schedule

Date

{new Date(selectedAppointment.date).toLocaleDateString( "en-US", { weekday: "long", year: "numeric", month: "long", day: "numeric", } )}

Time

{selectedAppointment.timeSlot}

{/* Dentist Information */}

Dentist Information

Your Dentist

Dr. {selectedAppointment.dentist.name}

{/* Payment Information */} {selectedAppointment.payment && (

Payment Information

Payment Status

{getPaymentBadge(selectedAppointment.payment.status)}

Amount

₱{selectedAppointment.payment.amount.toLocaleString()}

)} {/* Notes */} {selectedAppointment.notes && (

Special Requests / Notes

{selectedAppointment.notes}

)} {/* Action Buttons */}
{selectedAppointment.status === "pending" || selectedAppointment.status === "confirmed" ? ( <> ) : selectedAppointment.status === "completed" && selectedAppointment.payment?.status === "pending" ? ( ) : null}
)}
); }