'use client' import { zodResolver } from "@hookform/resolvers/zod" import { useForm } from "react-hook-form" import * as z from "zod" import { Button } from "@/components/ui/button" import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form" import { Input } from "@/components/ui/input" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select" import { Textarea } from "@/components/ui/textarea" import { createReservation } from "./actions" import { useState } from "react" import { toast } from "sonner" import { Check, ChevronsUpDown } from "lucide-react" import { cn } from "@/lib/utils" import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "@/components/ui/command" import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover" const formSchema = z.object({ hall_id: z.string().min(1, "Salon seçmelisiniz."), customer_id: z.string().min(1, "Müşteri seçmelisiniz."), package_id: z.string().optional(), date: z.string().min(1, "Tarih seçmelisiniz."), start_time: z.string().min(1, "Başlangıç saati seçmelisiniz."), end_time: z.string().min(1, "Bitiş saati seçmelisiniz."), notes: z.string().optional(), }) interface ReservationFormProps { halls: { id: string, name: string }[] customers: { id: string, full_name: string, phone?: string | null }[] packages: { id: string, name: string, price: number }[] } export function ReservationForm({ halls, customers, packages }: ReservationFormProps) { const [loading, setLoading] = useState(false) const [error, setError] = useState(null) const [openCustomer, setOpenCustomer] = useState(false) const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { hall_id: "", customer_id: "", package_id: "", date: "", start_time: "", end_time: "", notes: "", }, }) async function onSubmit(values: z.infer) { setLoading(true) setError(null) // Combine date and time const startDateTime = new Date(`${values.date}T${values.start_time}`) const endDateTime = new Date(`${values.date}T${values.end_time}`) if (endDateTime <= startDateTime) { setError("Bitiş saati başlangıç saatinden sonra olmalıdır.") setLoading(false) return } try { const result = await createReservation({ hall_id: values.hall_id, customer_id: values.customer_id, package_id: values.package_id === "none" ? undefined : values.package_id, start_time: startDateTime.toISOString(), end_time: endDateTime.toISOString(), notes: values.notes, }) if (result && result.error) { setError(result.error) } } catch (e) { setError("Beklenmedik bir hata oluştu.") } finally { setLoading(false) } } return (
{error && (
{error}
)}
( Salon )} /> ( Müşteri Müşteri bulunamadı. {customers.map((customer) => ( { form.setValue("customer_id", customer.id) setOpenCustomer(false) }} >
{customer.full_name} {customer.phone && ( {customer.phone} )}
))}
)} />
( Tarih )} /> ( Başlangıç Saati )} /> ( Bitiş Saati )} />
( Paket (Opsiyonel) )} /> ( Notlar