import React, { useState } from 'react'; import { Trash2, Edit3, Check, X, EyeOff, Eye, CreditCard } from 'lucide-react'; import Table from '@/components/Table'; import Popup from '@/components/Popup'; import CheckBox from '@/components/CheckBox'; import InputText from '@/components/InputText'; import logger from '@/utils/logger'; import SectionHeader from '@/components/SectionHeader'; import { useEstablishment } from '@/context/EstablishmentContext'; const FeesSection = ({ fees, setFees, discounts, handleCreate, handleEdit, handleDelete, type, subscriptionMode = false, selectedFees, handleFeeSelection, }) => { const [editingFee, setEditingFee] = useState(null); const [newFee, setNewFee] = useState(null); const [formData, setFormData] = useState({}); const [localErrors, setLocalErrors] = useState({}); const [popupVisible, setPopupVisible] = useState(false); const [popupMessage, setPopupMessage] = useState(''); const [removePopupVisible, setRemovePopupVisible] = useState(false); const [removePopupMessage, setRemovePopupMessage] = useState(''); const [removePopupOnConfirm, setRemovePopupOnConfirm] = useState(() => {}); const labelTypeFrais = type === 0 ? "Frais d'inscription" : 'Frais de scolarité'; const { selectedEstablishmentId } = useEstablishment(); // Récupération des messages d'erreur const getError = (field) => { return localErrors?.[field]?.[0]; }; const handleAddFee = () => { setNewFee({ id: Date.now(), name: '', base_amount: '', description: '', validity_start_date: '', validity_end_date: '', discounts: [], type: type, establishment: selectedEstablishmentId, }); }; const handleRemoveFee = (id) => { return handleDelete(id) .then(() => { setFees((prevFees) => prevFees.filter((fee) => fee.id !== id)); }) .catch((error) => { logger.error(error); }); }; const handleSaveNewFee = () => { if (newFee.name && newFee.base_amount) { const feeData = { ...newFee, establishment: selectedEstablishmentId, }; handleCreate(feeData) .then((createdFee) => { setFees([createdFee, ...fees]); setNewFee(null); setLocalErrors({}); }) .catch((error) => { logger.error('Error:', error.message); if (error.details) { logger.error('Form errors:', error.details); setLocalErrors(error.details); } }); } else { setPopupMessage('Tous les champs doivent être remplis et valides'); setPopupVisible(true); } }; const handleUpdateFee = (id, updatedFee) => { if (updatedFee.name && updatedFee.base_amount) { handleEdit(id, updatedFee) .then((updatedFee) => { setFees(fees.map((fee) => (fee.id === id ? updatedFee : fee))); setEditingFee(null); setLocalErrors({}); }) .catch((error) => { logger.error('Error:', error.message); if (error.details) { logger.error('Form errors:', error.details); setLocalErrors(error.details); } }); } else { setPopupMessage('Tous les champs doivent être remplis et valides'); setPopupVisible(true); } }; const handleToggleActive = (id, isActive) => { const fee = fees.find((fee) => fee.id === id); if (!fee) return; const updatedData = { is_active: !isActive, discounts: fee.discounts, }; handleEdit(id, updatedData) .then(() => { setFees((prevFees) => prevFees.map((fee) => fee.id === id ? { ...fee, is_active: !isActive } : fee ) ); }) .catch((error) => { logger.error(error); }); }; const handleChange = (e) => { const { name, value } = e.target; let parsedValue = value; if (name === 'discounts') { parsedValue = value.split(',').map((v) => parseInt(v, 10)); } if (editingFee) { setFormData((prevData) => ({ ...prevData, [name]: parsedValue, })); } else if (newFee) { setNewFee((prevData) => ({ ...prevData, [name]: parsedValue, })); } }; const renderInputField = (field, value, onChange, placeholder) => (
); const renderFeeCell = (fee, column) => { const isEditing = editingFee === fee.id; const isCreating = newFee && newFee.id === fee.id; const currentData = isEditing ? formData : newFee; if (isEditing || isCreating) { switch (column) { case 'NOM': return renderInputField( 'name', currentData.name, handleChange, 'Nom des frais' ); case 'MONTANT': return renderInputField( 'base_amount', currentData.base_amount, handleChange, 'Montant de base' ); case 'DESCRIPTION': return renderInputField( 'description', currentData.description, handleChange, 'Description' ); case 'ACTIONS': return (
); default: return null; } } else { switch (column) { case 'NOM': return fee.name; case 'MONTANT': return fee.base_amount + ' €'; case 'MISE A JOUR': return fee.updated_at_formatted; case 'DESCRIPTION': return fee.description; case 'ACTIONS': return (
); case '': return (
handleFeeSelection(fee.id)} fieldName="selectedFees" />
); default: return null; } } }; const columns = subscriptionMode ? [ { name: 'NOM', label: 'Nom' }, { name: 'DESCRIPTION', label: 'Description' }, { name: 'MONTANT', label: 'Montant de base' }, { name: '', label: 'Sélection' }, ] : [ { name: 'NOM', label: 'Nom' }, { name: 'MONTANT', label: 'Montant de base' }, { name: 'DESCRIPTION', label: 'Description' }, { name: 'MISE A JOUR', label: 'Date mise à jour' }, { name: 'ACTIONS', label: 'Actions' }, ]; return (
{!subscriptionMode && ( )} setPopupVisible(false)} onCancel={() => setPopupVisible(false)} uniqueConfirmButton={true} /> setRemovePopupVisible(false)} /> ); }; export default FeesSection;