import React, { useState } from 'react'; import { Calendar, DollarSign, Package, TrendingUp, TrendingDown, Eye, Edit3, MoreHorizontal, MapPin, ShoppingCart, Star, AlertTriangle, CheckCircle, Clock } from 'lucide-react'; import { SalesData } from '../../api/types'; import Card from '../ui/Card'; import Button from '../ui/Button'; interface SalesDataCardProps { salesData: SalesData; compact?: boolean; showActions?: boolean; inventoryProduct?: { id: string; name: string; category: string; }; onEdit?: (salesData: SalesData) => void; onDelete?: (salesData: SalesData) => void; onViewDetails?: (salesData: SalesData) => void; } const SalesDataCard: React.FC = ({ salesData, compact = false, showActions = true, inventoryProduct, onEdit, onDelete, onViewDetails }) => { const [showMenu, setShowMenu] = useState(false); // Format currency const formatCurrency = (amount: number) => { return new Intl.NumberFormat('es-ES', { style: 'currency', currency: 'EUR', minimumFractionDigits: 2, maximumFractionDigits: 2 }).format(amount); }; // Format date const formatDate = (dateString: string) => { return new Date(dateString).toLocaleDateString('es-ES', { day: 'numeric', month: 'short', year: 'numeric' }); }; // Format time const formatTime = (dateString: string) => { return new Date(dateString).toLocaleTimeString('es-ES', { hour: '2-digit', minute: '2-digit' }); }; // Get sales channel icon and label const getSalesChannelInfo = () => { switch (salesData.sales_channel) { case 'online': return { icon: ShoppingCart, label: 'Online', color: 'text-blue-600' }; case 'delivery': return { icon: MapPin, label: 'Delivery', color: 'text-green-600' }; case 'in_store': default: return { icon: Package, label: 'Tienda', color: 'text-purple-600' }; } }; // Get validation status const getValidationStatus = () => { if (salesData.is_validated) { return { icon: CheckCircle, label: 'Validado', color: 'text-green-600', bg: 'bg-green-50' }; } return { icon: Clock, label: 'Pendiente', color: 'text-yellow-600', bg: 'bg-yellow-50' }; }; // Calculate profit margin const profitMargin = salesData.cost_of_goods ? ((salesData.revenue - salesData.cost_of_goods) / salesData.revenue * 100) : null; const channelInfo = getSalesChannelInfo(); const validationStatus = getValidationStatus(); if (compact) { return (

{inventoryProduct?.name || `Producto ${salesData.inventory_product_id.slice(0, 8)}...`}

{salesData.quantity_sold} unidades {formatDate(salesData.date)}

{formatCurrency(salesData.revenue)}

{channelInfo.label}
); } return ( {/* Header */}

{inventoryProduct?.name || `Producto ${salesData.inventory_product_id.slice(0, 8)}...`}

{inventoryProduct?.category && ( <> {inventoryProduct.category} )} ID: {salesData.id.slice(0, 8)}...
{showActions && (
{showMenu && (
{onViewDetails && ( )} {onEdit && ( )}
)}
)}
{/* Sales Metrics */}
{salesData.quantity_sold}
Cantidad Vendida
{formatCurrency(salesData.revenue)}
Ingresos
{salesData.unit_price && (
{formatCurrency(salesData.unit_price)}
Precio Unitario
)} {profitMargin !== null && (
0 ? 'text-green-600' : 'text-red-600'}`}> {profitMargin.toFixed(1)}%
Margen
)}
{/* Details Row */}
{formatDate(salesData.date)} • {formatTime(salesData.date)}
{channelInfo.label}
{salesData.location_id && (
Local {salesData.location_id}
)}
{validationStatus.label}
{/* Additional Info */}
Origen: {salesData.source} {salesData.discount_applied && salesData.discount_applied > 0 && ( Descuento: {salesData.discount_applied}% )}
{salesData.weather_condition && (
{salesData.weather_condition.includes('rain') ? '🌧️' : salesData.weather_condition.includes('sun') ? '☀️' : salesData.weather_condition.includes('cloud') ? '☁️' : '🌤️'} {salesData.weather_condition}
)}
{/* Actions */} {showActions && (
{onViewDetails && ( )} {onEdit && ( )}
)}
); }; export default SalesDataCard;