import React, { useState, useEffect } from "react"; import { X, Percent } from "lucide-react"; interface DiscountOrderModalProps { isOpen: boolean; onClose: () => void; onConfirm: (discountValue: number, discountPercent: number) => void; orderValue: number; profit?: number; netProfit?: number; isManager?: boolean; } const DiscountOrderModal: React.FC = ({ isOpen, onClose, onConfirm, orderValue, profit = 0, netProfit = 0, isManager = false, }) => { const [discountPercent, setDiscountPercent] = useState("0"); const [discountValue, setDiscountValue] = useState("0"); useEffect(() => { if (!isOpen) { setDiscountPercent("0"); setDiscountValue("0"); } }, [isOpen]); const formatCurrency = (value: number) => { return new Intl.NumberFormat("pt-BR", { style: "currency", currency: "BRL", }).format(value); }; const formatPercent = (value: number) => { return new Intl.NumberFormat("pt-BR", { minimumFractionDigits: 2, maximumFractionDigits: 2, }).format(value); }; const handlePercentChange = (value: string) => { const numValue = parseFloat(value.replace(/[^\d,.-]/g, "").replace(",", ".")) || 0; setDiscountPercent(formatPercent(numValue)); if (orderValue > 0) { const discount = (orderValue * numValue) / 100; setDiscountValue(formatCurrency(discount)); } }; const handleValueChange = (value: string) => { const numValue = parseFloat(value.replace(/[^\d,.-]/g, "").replace(",", ".")) || 0; setDiscountValue(formatCurrency(numValue)); if (orderValue > 0) { const percent = (numValue / orderValue) * 100; setDiscountPercent(formatPercent(percent)); } }; const handleConfirm = () => { const discountValueNum = parseFloat( discountValue.replace(/[^\d,.-]/g, "").replace(",", ".") ) || 0; const discountPercentNum = parseFloat( discountPercent.replace(/[^\d,.-]/g, "").replace(",", ".") ) || 0; onConfirm(discountValueNum, discountPercentNum); onClose(); }; const netValue = orderValue - (parseFloat(discountValue.replace(/[^\d,.-]/g, "").replace(",", ".")) || 0); const calculatedNetProfit = orderValue > 0 && netValue > 0 ? (((netValue - profit) / netValue) * 100).toFixed(2) : "0.00"; if (!isOpen) return null; return (
{/* Header */}

Pedido de venda

Conceder desconto sobre o pedido

{/* Content */}
{/* Valor do pedido */}
{/* % Margem (apenas para gerente) */} {isManager && (
)} {/* Percentual de desconto */}
handlePercentChange(e.target.value)} className="w-full bg-white border-2 border-slate-200 rounded-xl px-4 py-3 font-black text-slate-700 outline-none focus:border-orange-500 transition-all" placeholder="0,00" />
{/* Valor de desconto */}
handleValueChange(e.target.value)} className="w-full bg-white border-2 border-slate-200 rounded-xl px-4 py-3 font-black text-slate-700 outline-none focus:border-orange-500 transition-all" placeholder="R$ 0,00" />
{/* Valor líquido */}
{/* % Margem Líquida (apenas para gerente) */} {isManager && (
)}
{/* Footer */}
); }; export default DiscountOrderModal;