"use client"; import { useState, useEffect } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Card, CardContent, CardHeader, CardTitle, CardFooter } from "@/components/ui/card"; interface PlanFormData { name: string; description: string; price: number; durationMonths: number; freeHours: number; bookingDiscount: number; storeDiscount: number; extraBenefits: string; } interface MembershipPlan { id: string; name: string; description: string | null; price: number | string; durationMonths: number; courtHours: number | null; discountPercent: number | string | null; benefits: string[] | null; benefitsSummary?: { freeHours: number; bookingDiscount: number; extraBenefits: string[]; }; } interface PlanFormProps { initialData?: MembershipPlan; onSubmit: (data: PlanFormData) => Promise; onCancel: () => void; isLoading?: boolean; mode?: "create" | "edit"; } const durationOptions = [ { value: 1, label: "1 month" }, { value: 3, label: "3 months" }, { value: 6, label: "6 months" }, { value: 12, label: "12 months" }, ]; export function PlanForm({ initialData, onSubmit, onCancel, isLoading = false, mode = "create", }: PlanFormProps) { // Extract store discount from benefits if present const extractStoreDiscount = (benefits: string[] | null): number => { if (!benefits) return 0; const storeDiscountBenefit = benefits.find(b => b.includes("store discount")); if (storeDiscountBenefit) { const match = storeDiscountBenefit.match(/(\d+)%/); return match ? parseInt(match[1], 10) : 0; } return 0; }; const getOtherBenefits = (benefits: string[] | null): string => { if (!benefits) return ""; return benefits .filter(b => !b.includes("store discount")) .join("\n"); }; const [formData, setFormData] = useState({ name: initialData?.name || "", description: initialData?.description || "", price: initialData?.price ? Number(initialData.price) : 0, durationMonths: initialData?.durationMonths || 1, freeHours: initialData?.benefitsSummary?.freeHours ?? initialData?.courtHours ?? 0, bookingDiscount: initialData?.benefitsSummary?.bookingDiscount ?? (initialData?.discountPercent ? Number(initialData.discountPercent) : 0), storeDiscount: extractStoreDiscount(initialData?.benefits || null), extraBenefits: getOtherBenefits(initialData?.benefitsSummary?.extraBenefits || initialData?.benefits || null), }); const [errors, setErrors] = useState>({}); const handleChange = ( e: React.ChangeEvent ) => { const { name, value, type } = e.target; setFormData((prev) => ({ ...prev, [name]: type === "number" ? parseFloat(value) || 0 : value, })); // Clear error when field is modified if (errors[name]) { setErrors((prev) => { const newErrors = { ...prev }; delete newErrors[name]; return newErrors; }); } }; const validate = (): boolean => { const newErrors: Record = {}; if (!formData.name.trim()) { newErrors.name = "Name is required"; } if (formData.price <= 0) { newErrors.price = "Price must be greater than 0"; } if (formData.bookingDiscount < 0 || formData.bookingDiscount > 100) { newErrors.bookingDiscount = "Discount must be between 0 and 100"; } if (formData.storeDiscount < 0 || formData.storeDiscount > 100) { newErrors.storeDiscount = "Discount must be between 0 and 100"; } if (formData.freeHours < 0) { newErrors.freeHours = "Free hours cannot be negative"; } setErrors(newErrors); return Object.keys(newErrors).length === 0; }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!validate()) return; await onSubmit(formData); }; return (
{mode === "create" ? "New Membership Plan" : "Edit Plan"} {/* Name */}
{errors.name && (

{errors.name}

)}
{/* Description */}