import React, { useState } from 'react'; import { Trash2, Edit3, Check, X, Percent, EuroIcon, Tag } 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 DiscountsSection = ({ discounts, setDiscounts, handleCreate, handleEdit, handleDelete, type, subscriptionMode = false, selectedDiscounts, handleDiscountSelection, }) => { const [editingDiscount, setEditingDiscount] = useState(null); const [newDiscount, setNewDiscount] = 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 { selectedEstablishmentId } = useEstablishment(); const handleAddDiscount = () => { setNewDiscount({ id: Date.now(), name: '', amount: '', description: '', discount_type: 0, type: type, establishment: selectedEstablishmentId, }); }; const handleRemoveDiscount = (id) => { return handleDelete(id) .then(() => { setDiscounts((prevDiscounts) => prevDiscounts.filter((discount) => discount.id !== id) ); }) .catch((error) => { logger.error(error); }); }; const handleSaveNewDiscount = () => { if (newDiscount.name && newDiscount.amount) { const discountData = { ...newDiscount, establishment: selectedEstablishmentId, }; handleCreate(discountData) .then((createdDiscount) => { setDiscounts([createdDiscount, ...discounts]); setNewDiscount(null); setLocalErrors({}); }) .catch((error) => { if (error && typeof error === 'object') { setLocalErrors(error); } else { logger.error(error); } }); } else { setPopupMessage('Tous les champs doivent être remplis'); setPopupVisible(true); } }; const handleUpdateDiscount = (id, updatedDiscount) => { if (updatedDiscount.name && updatedDiscount.amount) { handleEdit(id, updatedDiscount) .then(() => { setEditingDiscount(null); setLocalErrors({}); }) .catch((error) => { if (error && typeof error === 'object') { setLocalErrors(error); } else { logger.error(error); } }); } else { setPopupMessage('Tous les champs doivent être remplis'); setPopupVisible(true); } }; const handleToggleDiscountType = (id) => { const discount = discounts.find((discount) => discount.id === id); if (!discount) return; const updatedData = { ...discount, discount_type: discount.discount_type === 0 ? 1 : 0, }; handleEdit(id, updatedData) .then(() => { setDiscounts((prevDiscounts) => prevDiscounts.map((discount) => discount.id === id ? { ...discount, discount_type: updatedData.discount_type } : discount ) ); }) .catch((error) => { logger.error(error); }); }; const handleToggleDiscountTypeEdition = (id) => { if (editingDiscount) { setFormData((prevData) => ({ ...prevData, discount_type: prevData.discount_type === 0 ? 1 : 0, })); } else if (newDiscount) { setNewDiscount((prevData) => ({ ...prevData, discount_type: prevData.discount_type === 0 ? 1 : 0, })); } }; const handleChange = (e) => { const { name, value } = e.target; if (editingDiscount) { setFormData((prevData) => ({ ...prevData, [name]: value, })); } else if (newDiscount) { setNewDiscount((prevData) => ({ ...prevData, [name]: value, })); } }; const renderInputField = (field, value, onChange, placeholder) => (