'use client'; import { useState, useEffect } from 'react'; import { Product, CreateProductData, UpdateProductData, PRODUCT_TYPES } from '@/types/product'; import { PlusIcon, XMarkIcon } from '@heroicons/react/24/outline'; interface ProductFormProps { product?: Product | null; packageName: string; onSubmit: (data: CreateProductData | UpdateProductData) => Promise; onCancel: () => void; isLoading: boolean; } export default function ProductForm({ product, packageName, onSubmit, onCancel, isLoading }: ProductFormProps) { const [formData, setFormData] = useState({ title: '', price: 0, type: 4, // Default to monthly }); const [descriptions, setDescriptions] = useState([]); const [newDescription, setNewDescription] = useState(''); useEffect(() => { if (product) { setFormData({ title: product.title || '', price: product.price || 0, type: product.type || 4, }); setDescriptions(product.descriptions || []); } }, [product]); const handleInputChange = (e: React.ChangeEvent) => { const { name, value } = e.target; setFormData(prev => ({ ...prev, [name]: name === 'price' ? parseInt(value) || 0 : value, })); }; const handleAddDescription = () => { if (newDescription.trim()) { setDescriptions([...descriptions, newDescription.trim()]); setNewDescription(''); } }; const handleRemoveDescription = (index: number) => { setDescriptions(descriptions.filter((_, i) => i !== index)); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); const submitData = { ...formData, ...(descriptions.length > 0 && { descriptions }), }; await onSubmit(submitData); }; return (

{product ? 'ویرایش محصول' : 'ایجاد محصول جدید'} برای پکیج {packageName}

{/* Title */}
{/* Price */}
{/* Type */}
{/* Descriptions */}
setNewDescription(e.target.value)} className="flex-1 px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" placeholder="ویژگی جدید را وارد کنید" onKeyPress={(e) => { if (e.key === 'Enter') { e.preventDefault(); handleAddDescription(); } }} />
{/* Descriptions List */} {descriptions.length > 0 && (
    {descriptions.map((desc, index) => (
  • {desc}
  • ))}
)}
{/* Form Actions */}
); }