import React, { useState } from 'react'; import { Card, Button, Badge, Input, Select } from '../../ui'; import type { ExportOptionsProps, ExportOptions, ExportFormat, ExportTemplate, ReportSchedule } from './types'; const FORMAT_LABELS: Record = { pdf: 'PDF', excel: 'Excel', csv: 'CSV', png: 'PNG', svg: 'SVG', json: 'JSON', }; const FORMAT_DESCRIPTIONS: Record = { pdf: 'Documento PDF con formato profesional', excel: 'Hoja de cálculo de Microsoft Excel', csv: 'Archivo de valores separados por comas', png: 'Imagen PNG de alta calidad', svg: 'Imagen vectorial escalable', json: 'Datos estructurados en formato JSON', }; const FORMAT_ICONS: Record = { pdf: '📄', excel: '📊', csv: '📋', png: '🖼️', svg: '🎨', json: '⚙️', }; const FREQUENCY_OPTIONS = [ { value: 'daily', label: 'Diario' }, { value: 'weekly', label: 'Semanal' }, { value: 'monthly', label: 'Mensual' }, { value: 'quarterly', label: 'Trimestral' }, { value: 'yearly', label: 'Anual' }, ]; const TIME_OPTIONS = [ { value: '06:00', label: '6:00 AM' }, { value: '09:00', label: '9:00 AM' }, { value: '12:00', label: '12:00 PM' }, { value: '15:00', label: '3:00 PM' }, { value: '18:00', label: '6:00 PM' }, { value: '21:00', label: '9:00 PM' }, ]; const DAYS_OF_WEEK = [ { value: 1, label: 'Lunes' }, { value: 2, label: 'Martes' }, { value: 3, label: 'Miércoles' }, { value: 4, label: 'Jueves' }, { value: 5, label: 'Viernes' }, { value: 6, label: 'Sábado' }, { value: 0, label: 'Domingo' }, ]; interface ExportOptionsComponentProps extends ExportOptionsProps { onClose?: () => void; } export const ExportOptions: React.FC = ({ type, title, description, availableFormats = ['pdf', 'excel', 'csv', 'png'], templates = [], onExport, onSchedule, loading = false, disabled = false, showScheduling = true, showTemplates = true, showAdvanced = true, defaultOptions, className = '', onClose, }) => { const [selectedFormat, setSelectedFormat] = useState( defaultOptions?.format || availableFormats[0] ); const [selectedTemplate, setSelectedTemplate] = useState( templates.find(t => t.is_default)?.id || '' ); const [exportOptions, setExportOptions] = useState>({ include_headers: true, include_filters: true, include_summary: true, date_format: 'DD/MM/YYYY', number_format: '#,##0.00', currency_format: '€#,##0.00', locale: 'es-ES', timezone: 'Europe/Madrid', page_size: 'A4', orientation: 'portrait', password_protected: false, ...defaultOptions, }); const [scheduleOptions, setScheduleOptions] = useState>({ enabled: false, frequency: 'weekly', time: '09:00', days_of_week: [1], // Monday recipients: [], format: selectedFormat, include_attachments: true, }); const [recipientsInput, setRecipientsInput] = useState(''); const [activeTab, setActiveTab] = useState<'export' | 'schedule'>('export'); const [showAdvancedOptions, setShowAdvancedOptions] = useState(false); const handleExportOptionChange = (key: keyof ExportOptions, value: any) => { setExportOptions(prev => ({ ...prev, [key]: value })); }; const handleScheduleOptionChange = (key: keyof ReportSchedule, value: any) => { setScheduleOptions(prev => ({ ...prev, [key]: value })); }; const handleExport = () => { if (onExport) { const fullOptions: ExportOptions = { format: selectedFormat, template: selectedTemplate || undefined, include_headers: true, include_filters: true, include_summary: true, date_format: 'DD/MM/YYYY', number_format: '#,##0.00', currency_format: '€#,##0.00', locale: 'es-ES', timezone: 'Europe/Madrid', page_size: 'A4', orientation: 'portrait', ...exportOptions, // format: selectedFormat, // Ensure format matches selection - handled by exportOptions }; onExport(fullOptions); } }; const handleSchedule = () => { if (onSchedule) { const recipients = recipientsInput .split(',') .map(email => email.trim()) .filter(email => email.length > 0); const fullSchedule: ReportSchedule = { enabled: true, frequency: 'weekly', time: '09:00', recipients: recipients, format: selectedFormat, include_attachments: true, ...scheduleOptions, }; onSchedule(fullSchedule); } }; const renderFormatSelector = () => (

Formato de exportación

{availableFormats.map(format => ( ))}
); const renderTemplateSelector = () => { if (!showTemplates || templates.length === 0) return null; return (

Plantilla

{selectedTemplate && (
{templates.find(t => t.id === selectedTemplate)?.description}
)}
); }; const renderBasicOptions = () => (

Opciones básicas

); const renderAdvancedOptions = () => { if (!showAdvanced || !showAdvancedOptions) return null; return (

Opciones avanzadas

{(['pdf'] as ExportFormat[]).includes(selectedFormat) && ( <>
)}
{exportOptions.password_protected && (
handleExportOptionChange('password', e.target.value)} disabled={disabled || loading} />
)}
); }; const renderScheduleTab = () => (
{scheduleOptions.enabled && (
{scheduleOptions.frequency === 'weekly' && (
{DAYS_OF_WEEK.map(day => ( ))}
)}
setRecipientsInput(e.target.value)} placeholder="ejemplo@empresa.com, otro@empresa.com" disabled={disabled || loading} />
)}
); return (
{/* Header */}

{title}

{description && (

{description}

)}
{onClose && ( )}
{/* Tabs */}
{/* Content */}
{activeTab === 'export' ? ( <> {renderFormatSelector()} {renderTemplateSelector()} {renderBasicOptions()} {showAdvanced && (
)} {renderAdvancedOptions()} ) : ( renderScheduleTab() )}
{/* Footer */}
{onClose && ( )} {activeTab === 'export' ? ( ) : ( )}
); }; export default ExportOptions;