import React, { useState } from 'react'; import { motion } from 'framer-motion'; import { X } from 'lucide-react'; import { updatePlan } from '../../api/sysadmin'; import type { Plan } from '../../api/sysadmin'; type EditPlanModalProps = { plan: Plan; onClose: () => void; onSaved: () => void; }; const numberFields: { key: keyof Plan; label: string }[] = [ { key: 'price', label: 'Precio mensual' }, { key: 'annualPrice', label: 'Precio anual' }, { key: 'limitOrganizations', label: 'Límite de organizaciones' }, { key: 'limitEmployees', label: 'Límite de empleados' }, { key: 'limitServices', label: 'Límite de servicios' }, { key: 'limitAppointments', label: 'Límite de turnos' }, { key: 'limitClients', label: 'Límite de clientes' }, { key: 'limitRepeats', label: 'Límite de repeticiones' }, { key: 'discount3Months', label: 'Descuento 3 meses (%)' }, { key: 'discount6Months', label: 'Descuento 6 meses (%)' }, { key: 'discount12Months', label: 'Descuento 12 meses (%)' }, ]; const limitFields = new Set([ 'limitOrganizations', 'limitEmployees', 'limitServices', 'limitAppointments', 'limitClients', 'limitRepeats', ]); const booleanFields: { key: keyof Plan; label: string }[] = [ { key: 'active', label: 'Activo para contratación' }, { key: 'featured', label: 'Destacado' }, { key: 'mailNotifications', label: 'Notificaciones por email' }, { key: 'smsNotifications', label: 'Notificaciones SMS' }, { key: 'wapNotifications', label: 'Notificaciones WhatsApp' }, { key: 'payments', label: 'Pagos habilitados' }, { key: 'bot', label: 'Bot habilitado' }, { key: 'dateLimit', label: 'Tiene límite de fecha' }, ]; export const EditPlanModal = ({ plan, onClose, onSaved }: EditPlanModalProps) => { const [formData, setFormData] = useState({ ...plan, features: Array.isArray(plan.features) ? plan.features : [], }); const [featuresText, setFeaturesText] = useState(formData.features.join('\n')); const [loading, setLoading] = useState(false); const planId = plan._id || plan.id || ''; const setField = (key: keyof Plan, value: string | number | boolean | string[]) => { setFormData({ ...formData, [key]: value }); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); try { await updatePlan({ planId, name: formData.name, description: formData.description, code: formData.code, price: Number(formData.price) || 0, annualPrice: Number(formData.annualPrice) || 0, limitOrganizations: Number(formData.limitOrganizations) || 0, limitEmployees: Number(formData.limitEmployees) || 0, limitServices: Number(formData.limitServices) || 0, limitAppointments: Number(formData.limitAppointments) || 0, limitClients: Number(formData.limitClients) || 0, limitRepeats: Number(formData.limitRepeats) || 0, discount3Months: Number(formData.discount3Months) || 0, discount6Months: Number(formData.discount6Months) || 0, discount12Months: Number(formData.discount12Months) || 0, active: Boolean(formData.active), featured: Boolean(formData.featured), mailNotifications: Boolean(formData.mailNotifications), smsNotifications: Boolean(formData.smsNotifications), wapNotifications: Boolean(formData.wapNotifications), payments: Boolean(formData.payments), bot: Boolean(formData.bot), dateLimit: Boolean(formData.dateLimit), features: featuresText.split('\n').map(feature => feature.trim()).filter(Boolean), }); onSaved(); onClose(); } catch (err: any) { alert(err.response?.data?.message || err.message || 'Error guardando el plan'); } setLoading(false); }; return (

Editar plan: {plan.name}

setField('name', e.target.value)} required />
setField('code', e.target.value)} required />