@extends('admin.layouts.app')

@section('page-title', 'Shipping Report List')

@section('content')
<style>
    /* ===== GLOBAL RESPONSIVE STYLES ===== */
    html, body {
    overflow-x: hidden !important;
    max-width: 100%;
    }
    
    @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');
    
    .report-container {
        background: #ffffff;
        padding: 25px;
        border-radius: 16px;
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.08);
        margin: 20px auto;
        max-width: 100%;
        font-family: 'Inter', sans-serif;
        border: 1px solid #f0f0f0;
    }

    .report-header {
        display: flex;
        align-items: center;
        gap: 18px;
        margin-bottom: 8px;
        padding-bottom: 20px;
        border-bottom: 2px solid #f8f9fa;
    }

    .header-icon {
        width: 55px;
        height: 55px;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        border-radius: 14px;
        display: flex;
        align-items: center;
        justify-content: center;
        box-shadow: 0 6px 20px rgba(102, 126, 234, 0.3);
    }

    .header-icon i {
        font-size: 24px;
        color: white;
    }

    .header-text {
        flex: 1;
    }

    .report-title {
        font-size: 24px;
        font-weight: 700;
        color: #1a202c;
        letter-spacing: -0.6px;
        margin-bottom: 4px;
    }

    .report-subtitle {
        font-size: 14px;
        color: #718096;
        font-weight: 500;
    }

    .stats-container {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
        gap: 15px;
        margin: 20px 0;
    }

    .stat-card {
        background: linear-gradient(135deg, #f8f9ff 0%, #f0f4ff 100%);
        padding: 18px 15px;
        border-radius: 12px;
        border-left: 4px solid #4f46e5;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
        transition: transform 0.3s ease;
        text-align: center;
    }

    .stat-card:hover {
        transform: translateY(-2px);
    }

    .stat-card.warning {
        border-left-color: #f59e0b;
        background: linear-gradient(135deg, #fffbeb 0%, #fef3c7 100%);
    }

    .stat-card.success {
        border-left-color: #10b981;
        background: linear-gradient(135deg, #ecfdf5 0%, #d1fae5 100%);
    }

    .stat-card.danger {
        border-left-color: #ef4444;
        background: linear-gradient(135deg, #fef2f2 0%, #fee2e2 100%);
    }

    .stat-card.info {
        border-left-color: #3b82f6;
        background: linear-gradient(135deg, #eff6ff 0%, #dbeafe 100%);
    }

    .stat-card.secondary {
        border-left-color: #8b5cf6;
        background: linear-gradient(135deg, #faf5ff 0%, #f3e8ff 100%);
    }

    

    .stat-icon i {
        font-size: 18px;
        color: #4f46e5;
    }

    .stat-card.warning .stat-icon {
        background: rgba(245, 158, 11, 0.1);
    }

    .stat-card.warning .stat-icon i {
        color: #f59e0b;
    }

    .stat-card.success .stat-icon {
        background: rgba(16, 185, 129, 0.1);
    }

    .stat-card.success .stat-icon i {
        color: #10b981;
    }

    .stat-card.danger .stat-icon {
        background: rgba(239, 68, 68, 0.1);
    }

    .stat-card.danger .stat-icon i {
        color: #ef4444;
    }

    /* .stat-card.info .stat-icon {
        background: rgba(59, 130, 246, 0.1);
    } */

    .stat-card.info .stat-icon i {
        color: #3b82f6;
    }

    .stat-card.secondary .stat-icon {
        background: rgba(139, 92, 246, 0.1);
    }

    .stat-card.secondary .stat-icon i {
        color: #8b5cf6;
    }

    .stat-value {
        font-size: 22px;
        font-weight: 700;
        color: #1a202c;
        margin-bottom: 4px;
        line-height: 1.2;
    }

    .stat-label {
        font-size: 13px;
        color: #718096;
        font-weight: 500;
        line-height: 1.3;
    }

    .filter-section {
        background: #f8fafc;
        border-radius: 12px;
        padding: 20px;
        margin-bottom: 20px;
        border: 1px solid #e2e8f0;
    }

    .filter-row {
        display: flex;
        gap: 15px;
        flex-wrap: wrap;
        align-items: end;
    }

    .filter-group {
        display: flex;
        flex-direction: column;
        gap: 6px;
        min-width: 180px;
        flex: 1;
    }

    .filter-label {
        font-size: 13px;
        font-weight: 600;
        color: #374151;
        display: flex;
        align-items: center;
        gap: 6px;
    }

    .filter-label i {
        color: #6b7280;
        font-size: 13px;
    }

    .filter-control {
        padding: 10px 12px;
        border-radius: 8px;
        border: 1.5px solid #e5e7eb;
        background: #ffffff;
        font-size: 13px;
        color: #1f2937;
        transition: all 0.3s ease;
        font-family: 'Inter', sans-serif;
    }

    .filter-control:focus {
        outline: none;
        border-color: #4f46e5;
        box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.15);
    }

    /* ===== IMPROVED TABLE CONTAINER FOR HORIZONTAL SCROLL ===== */
    .table-container {
        overflow-x: auto;
        border-radius: 12px;
        border: 1px solid #e5e7eb;
        background: white;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
        margin-top: 10px;
        -webkit-overflow-scrolling: touch; /* Smooth scrolling on iOS */
    }

    /* Ensure table takes full width and doesn't wrap */
    .report-table {
        width: 100%;
        border-collapse: collapse;
        min-width: 1200px; /* Minimum width to prevent squeezing */
        table-layout: auto; /* Allow columns to adjust based on content */
    }

    /* Table header styling - CENTER ALIGNED */
    .report-table thead {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        position: sticky;
        top: 0;
        z-index: 10;
    }

    .report-table th {
        padding: 15px 12px;
        text-align: center; /* Center align header text */
        font-size: 13px;
        font-weight: 600;
        color: white;
        border-bottom: none;
        white-space: nowrap;
        position: relative;
        min-width: 120px; /* Minimum column width */
    }

    .report-table th:not(:last-child)::after {
        content: '';
        position: absolute;
        right: 0;
        top: 50%;
        transform: translateY(-50%);
        width: 1px;
        height: 16px;
        background: rgba(255, 255, 255, 0.3);
    }

    /* Table cell styling with proper text handling - CENTER ALIGNED */
    .report-table td {
        padding: 12px;
        font-size: 13px;
        color: #374151;
        border-bottom: 1px solid #f3f4f6;
        vertical-align: middle;
        white-space: nowrap; /* Prevent text wrapping */
        overflow: hidden;
        text-overflow: ellipsis; /* Show ellipsis for overflow */
        max-width: 200px; /* Maximum width before ellipsis */
        min-width: 120px; /* Minimum column width */
        text-align: center; /* Center align all table data */
    }

    /* Specific column width adjustments */
    .report-table th:nth-child(1), /* Order ID */
    .report-table td:nth-child(1) {
        min-width: 100px;
        max-width: 150px;
    }

    .report-table th:nth-child(2), /* Shipment ID */
    .report-table td:nth-child(2) {
        min-width: 120px;
        max-width: 180px;
    }

    .report-table th:nth-child(3), /* Company Name */
    .report-table td:nth-child(3) {
        min-width: 150px;
        max-width: 200px;
    }

    .report-table th:nth-child(4), /* User Name */
    .report-table td:nth-child(4) {
        min-width: 120px;
        max-width: 160px;
    }

    .report-table th:nth-child(5), /* Origin */
    .report-table td:nth-child(5),
    .report-table th:nth-child(6), /* Destination */
    .report-table td:nth-child(6) {
        min-width: 120px;
        max-width: 180px;
    }

    .report-table th:nth-child(7), /* Date */
    .report-table td:nth-child(7),
    .report-table th:nth-child(9), /* Invoice Date */
    .report-table td:nth-child(9) {
        min-width: 100px;
        max-width: 120px;
    }

    .report-table th:nth-child(8), /* Invoice ID */
    .report-table td:nth-child(8) {
        min-width: 120px;
        max-width: 160px;
    }

    .report-table th:nth-child(10), /* Invoice Amount */
    .report-table td:nth-child(10) {
        min-width: 120px;
        max-width: 150px;
    }

    .report-table th:nth-child(11), /* Invoice Status */
    .report-table td:nth-child(11),
    .report-table th:nth-child(12), /* Shipment Status */
    .report-table td:nth-child(12) {
        min-width: 120px;
        max-width: 140px;
    }

    /* Row hover effects */
    .report-table tbody tr {
        transition: all 0.3s ease;
    }

    .report-table tbody tr:hover {
        background-color: #f8faff;
        transform: translateY(-1px);
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
    }

    .report-table tbody tr:nth-child(even) {
        background-color: #fafafa;
    }

    .report-table tbody tr:nth-child(even):hover {
        background-color: #f3f4ff;
    }

    /* ===== BADGE STYLES - CENTER ALIGNED ===== */
    .status-badge {
        font-size: 11px !important;
        font-weight: 600 !important;
        padding: 6px 12px 6px 8px !important;
        border-radius: 20px !important;
        text-transform: uppercase;
        letter-spacing: 0.3px;
        display: inline-flex !important;
        align-items: center;
        justify-content: center;
        background-size: cover !important;
        background-position: center !important;
        color: #fff !important;
        text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
        border: 2px solid transparent !important;
        min-width: 40px;
        box-sizing: border-box;
        line-height: 1.2;
        gap: 6px;
        position: relative;
        overflow: hidden;
        white-space: nowrap;
        margin: 0 auto; /* Center badges */
    }

    .status-badge::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: inherit;
        opacity: 0.9;
        z-index: -1;
    }

    .status-badge i {
        font-size: 8px;
        position: relative;
        z-index: 1;
    }

    /* Invoice Status Badges */
    .status-paid {
        background: linear-gradient(135deg, #10b981, #34d399) !important;
        color: white !important;
        border-color: #059669 !important;
        width: 99px;
    }

    .status-pending {
        background: linear-gradient(135deg, #fef3c7, #fde68a) !important;
        color: #d97706 !important;
        border-color: #f59e0b !important;
        width: 99px;
    }

    .status-overdue {
        background: linear-gradient(135deg, #fef2f2, #fecaca) !important;
        color: #dc2626 !important;
        border-color: #ef4444 !important;
        width: 99px;
    }

    /* Shipment Status Badges */
    .ship-pending {
        background: linear-gradient(135deg, #fef3c7, #fde68a) !important;
        color: #d97706 !important;
        border-color: #f59e0b !important;
        width: 99px;
    }

    .ship-in_transit {
        background: linear-gradient(135deg, #dbeafe, #bfdbfe) !important;
        color: #1e40af !important;
        border-color: #3b82f6 !important;
        width: 99px;
    }

    .ship-dispatched {
        background: linear-gradient(135deg, #e9d5ff, #d8b4fe) !important;
        color: #7e22ce !important;
        border-color: #8b5cf6 !important;
        width: 99px;
    }

    .ship-delivered {
        background: linear-gradient(135deg, #d1fae5, #a7f3d0) !important;
        color: #065f46 !important;
        border-color: #10b981 !important;
        width: 99px;
    }

    .data-highlight {
        font-weight: 600;
        color: #1a202c;
        display: flex;
        align-items: center;
        justify-content: center; /* Center content */
        gap: 4px;
        font-size: 13px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        margin: 0 auto; /* Center the highlight container */
    }

    .data-highlight i {
        color: #4f46e5;
        font-size: 11px;
        flex-shrink: 0; /* Prevent icon from shrinking */
    }

    .empty-state {
        text-align: center;
        padding: 50px 20px;
        color: #6b7280;
    }

    .empty-icon {
        width: 60px;
        height: 60px;
        background: #f8f9fa;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        margin: 0 auto 15px;
        border: 2px dashed #e5e7eb;
    }

    .empty-icon i {
        font-size: 24px;
        color: #9ca3af;
    }

    .empty-state h3 {
        font-size: 16px;
        margin-bottom: 6px;
        color: #4b5563;
    }

    .empty-state p {
        font-size: 13px;
        color: #6b7280;
    }

    /* Scrollbar styling for better appearance */
    .table-container::-webkit-scrollbar {
        height: 8px;
    }

    .table-container::-webkit-scrollbar-track {
        background: #f1f1f1;
        border-radius: 4px;
    }

    .table-container::-webkit-scrollbar-thumb {
        background: #c1c1c1;
        border-radius: 4px;
    }

    .table-container::-webkit-scrollbar-thumb:hover {
        background: #a8a8a8;
    }

    @media (max-width: 1024px) {
        .stats-container {
            grid-template-columns: repeat(3, 1fr);
        }
        
        .report-table {
            min-width: 1100px;
        }
    }

    @media (max-width: 768px) {
        .report-container {
            padding: 15px;
        }
        
        .report-header {
            flex-direction: column;
            text-align: center;
            gap: 12px;
        }
        
        .header-icon {
            width: 50px;
            height: 50px;
        }
        
        .report-title {
            font-size: 20px;
        }
        
        .stats-container {
            grid-template-columns: repeat(2, 1fr);
            gap: 12px;
        }
        
        .stat-card {
            padding: 15px 12px;
        }
        
        .stat-value {
            font-size: 20px;
        }
        
        .filter-row {
            flex-direction: column;
            align-items: stretch;
            gap: 12px;
        }
        
        .filter-group {
            min-width: 100%;
        }
        
        .report-table {
            min-width: 1000px;
        }
        
        /* Adjust column widths for mobile */
        .report-table th,
        .report-table td {
            min-width: 100px;
            padding: 10px 8px;
        }
    }

    @media (max-width: 480px) {
        .stats-container {
            grid-template-columns: 1fr;
        }
        
        .stat-card {
            padding: 15px;
        }
        
        .report-table {
            min-width: 900px;
        }
        
        .report-table th,
        .report-table td {
            min-width: 90px;
            padding: 8px 6px;
            font-size: 12px;
        }
        
        .status-badge {
            font-size: 10px !important;
            padding: 4px 8px 4px 6px !important;
        }
    }
</style>

<div class="report-container">
    <!-- HEADER SECTION -->
    <div class="report-header">
        <div class="header-icon">
            <i class="fas fa-shipping-fast"></i>
        </div>
        <div class="header-text">
            <h1 class="report-title">Shipping Report List</h1>
            <p class="report-subtitle">Comprehensive overview of all shipping activities and invoices</p>
        </div>
    </div>

    <!-- DYNAMIC STATS CARDS -->
    <div class="stats-container">
        <div class="stat-card">
            <div class="stat-icon">
                <i class="fas fa-box-open"></i>
            </div>
            <div class="stat-value" id="totalShipments">{{ count($reports) }}</div>
            <div class="stat-label">Total Shipments</div>
        </div>
        <div class="stat-card warning">
            <div class="stat-icon">
                <i class="fas fa-clock"></i>
            </div>
            <div class="stat-value" id="pendingShipments">0</div>
            <div class="stat-label">Pending Shipments</div>
        </div>
        <div class="stat-card info">
            <div class="stat-icon">
                <i class="fas fa-truck-moving"></i>
            </div>
            <div class="stat-value" id="inTransitShipments">0</div>
            <div class="stat-label">In Transit</div>
        </div>
        <div class="stat-card secondary">
            <div class="stat-icon">
                <i class="fas fa-shipping-fast"></i>
            </div>
            <div class="stat-value" id="dispatchedShipments">0</div>
            <div class="stat-label">Dispatched</div>
        </div>
        <div class="stat-card success">
            <div class="stat-icon">
                <i class="fas fa-check-circle"></i>
            </div>
            <div class="stat-value" id="deliveredShipments">0</div>
            <div class="stat-label">Delivered</div>
        </div>
        <div class="stat-card danger">
            <div class="stat-icon">
                <i class="fas fa-exclamation-triangle"></i>
            </div>
            <div class="stat-value" id="overdueInvoices">0</div>
            <div class="stat-label">Overdue Invoices</div>
        </div>
    </div>

    <!-- FILTER SECTION -->
    <div class="filter-section">
        <div class="filter-row">
            <div class="filter-group">
                <label class="filter-label">
                    <i class="fas fa-calendar-alt"></i>
                    From Date
                </label>
                <input type="date" class="filter-control" id="fromDate">
            </div>
            <div class="filter-group">
                <label class="filter-label">
                    <i class="fas fa-calendar-check"></i>
                    To Date
                </label>
                <input type="date" class="filter-control" id="toDate">
            </div>
            <div class="filter-group">
                <label class="filter-label">
                    <i class="fas fa-building"></i>
                    Company
                </label>
                <select class="filter-control" id="companyFilter">
                    <option value="" selected>All Companies</option>
                    <option value="ABC Corporation">ABC Corporation</option>
                    <option value="XYZ Enterprises">XYZ Enterprises</option>
                    <option value="Global Traders">Global Traders</option>
                    <option value="Tech Solutions Ltd">Tech Solutions Ltd</option>
                </select>
            </div>
            <div class="filter-group">
                <label class="filter-label">
                    <i class="fas fa-truck-loading"></i>
                    Shipping Status
                </label>
                <select class="filter-control" id="statusFilter">
                    <option value="" selected>All Status</option>
                    <option value="pending">Pending</option>
                    <option value="in_transit">In Transit</option>
                    <option value="dispatched">Dispatched</option>
                    <option value="delivered">Delivered</option>
                </select>
            </div>
        </div>
    </div>

    <!-- TABLE SECTION -->
    <div class="table-container">
        <table class="report-table">
            <thead>
                <tr>
                    <th><i class="fas fa-hashtag"></i> Order ID</th>
                    <th><i class="fas fa-barcode"></i> Shipment ID</th>
                    <th><i class="fas fa-building"></i> Company Name</th>
                    <th><i class="fas fa-user"></i> User Name</th>
                    <th><i class="fas fa-map-marker-alt"></i> Origin</th>
                    <th><i class="fas fa-location-arrow"></i> Destination</th>
                    <th><i class="fas fa-calendar"></i> Date</th>
                    <th><i class="fas fa-file-invoice"></i> Invoice ID</th>
                    <th><i class="fas fa-calendar-day"></i> Invoice Date</th>
                    <th><i class="fas fa-money-bill-wave"></i> Invoice Amount</th>
                    <th><i class="fas fa-receipt"></i> Invoice Status</th>
                    <th><i class="fas fa-shipping-fast"></i> Shipment Status</th>
                </tr>
            </thead>
            <tbody id="reportTableBody">
                @forelse ($reports as $r)
                    <tr data-status="{{ $r->shipment_status }}" data-invoice-status="{{ strtolower($r->invoice_status) }}" data-company="{{ $r->company_name ?? '' }}" data-date="{{ $r->shipment_date }}">
                        <td>
                            <span class="data-highlight" title="{{ $r->order_id }}">
                                <i class="fas fa-hashtag"></i>
                                {{ $r->order_id }}
                            </span>
                        </td>
                        <td>
                            <span class="data-highlight" title="{{ $r->shipment_id }}">
                                <i class="fas fa-barcode"></i>
                                {{ $r->shipment_id }}
                            </span>
                        </td>
                        <td title="{{ $r->company_name ?? '-' }}">{{ $r->company_name ?? '-' }}</td>
                        <td title="{{ $r->customer_name ?? '-' }}">{{ $r->customer_name ?? '-' }}</td>
                        <td>
                            <span class="data-highlight" title="{{ $r->origin }}">
                                <i class="fas fa-map-marker-alt"></i>
                                {{ $r->origin }}
                            </span>
                        </td>
                        <td>
                            <span class="data-highlight" title="{{ $r->destination }}">
                                <i class="fas fa-location-arrow"></i>
                                {{ $r->destination }}
                            </span>
                        </td>
                        <td>{{ \Carbon\Carbon::parse($r->shipment_date)->format('d/m/Y') }}</td>
                        <td>
                            <span class="data-highlight" title="{{ $r->invoice_number }}">
                                <i class="fas fa-file-invoice"></i>
                                {{ $r->invoice_number }}
                            </span>
                        </td>
                        <td>{{ \Carbon\Carbon::parse($r->invoice_date)->format('d/m/Y') }}</td>
                        <td>
                            <span class="data-highlight" title="₹{{ number_format($r->final_amount) }}">
                                <i class="fas fa-rupee-sign"></i>
                                {{ number_format($r->final_amount) }}
                            </span>
                        </td>
                        <td>
                            @php $ist = strtolower($r->invoice_status); @endphp
                            <span class="status-badge 
                                @if($ist === 'paid') status-paid
                                @elseif($ist === 'pending') status-pending
                                @elseif($ist === 'overdue') status-overdue
                                @endif">
                                <i class="fas fa-circle"></i>
                                {{ ucfirst($ist) }}
                            </span>
                        </td>
                        <td>
                            <span class="status-badge ship-{{ $r->shipment_status }}">
                                <i class="fas fa-circle"></i>
                                {{ ucfirst(str_replace('_',' ', $r->shipment_status)) }}
                            </span>
                        </td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="12">
                            <div class="empty-state">
                                <div class="empty-icon">
                                    <i class="fas fa-inbox"></i>
                                </div>
                                <h3>No Shipping Reports Found</h3>
                                <p>There are no shipping reports matching your current filters</p>
                            </div>
                        </td>
                    </tr>
                @endforelse
            </tbody>
        </table>
    </div>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        // Initialize statistics
        updateStatistics();
        
        // Set default dates for demo purposes
        const today = new Date();
        const lastMonth = new Date(today.getFullYear(), today.getMonth() - 1, today.getDate());
        
        const formatDate = (date) => {
            return date.toISOString().split('T')[0];
        };
        
        document.getElementById('fromDate').value = formatDate(lastMonth);
        document.getElementById('toDate').value = formatDate(today);

        // Add event listeners for filters
        document.getElementById('fromDate').addEventListener('change', filterTable);
        document.getElementById('toDate').addEventListener('change', filterTable);
        document.getElementById('companyFilter').addEventListener('change', filterTable);
        document.getElementById('statusFilter').addEventListener('change', filterTable);

        function updateStatistics() {
            const rows = document.querySelectorAll('#reportTableBody tr');
            let totalShipments = 0;
            let pendingShipments = 0;
            let inTransitShipments = 0;
            let dispatchedShipments = 0;
            let deliveredShipments = 0;
            let overdueInvoices = 0;

            rows.forEach(row => {
                // Skip if row is hidden or empty state
                if (row.style.display === 'none' || row.querySelector('.empty-state')) return;
                
                totalShipments++;

                // Count shipment statuses
                const status = row.getAttribute('data-status');
                if (status === 'pending') pendingShipments++;
                if (status === 'in_transit') inTransitShipments++;
                if (status === 'dispatched') dispatchedShipments++;
                if (status === 'delivered') deliveredShipments++;
                
                // Count invoice statuses
                const invoiceStatus = row.getAttribute('data-invoice-status');
                if (invoiceStatus === 'overdue') overdueInvoices++;
            });

            // Update statistics cards
            document.getElementById('totalShipments').textContent = totalShipments;
            document.getElementById('pendingShipments').textContent = pendingShipments;
            document.getElementById('inTransitShipments').textContent = inTransitShipments;
            document.getElementById('dispatchedShipments').textContent = dispatchedShipments;
            document.getElementById('deliveredShipments').textContent = deliveredShipments;
            document.getElementById('overdueInvoices').textContent = overdueInvoices;
        }

        function filterTable() {
            const fromDate = document.getElementById('fromDate').value;
            const toDate = document.getElementById('toDate').value;
            const companyFilter = document.getElementById('companyFilter').value;
            const statusFilter = document.getElementById('statusFilter').value;

            const rows = document.querySelectorAll('#reportTableBody tr');
            let visibleCount = 0;

            rows.forEach(row => {
                // Skip empty state row
                if (row.querySelector('.empty-state')) return;
                
                let showRow = true;

                // Date filter
                if (fromDate && toDate) {
                    const rowDate = row.getAttribute('data-date');
                    if (rowDate) {
                        const shipmentDate = new Date(rowDate);
                        const from = new Date(fromDate);
                        const to = new Date(toDate);
                        to.setHours(23, 59, 59, 999); // End of day

                        if (shipmentDate < from || shipmentDate > to) {
                            showRow = false;
                        }
                    }
                }

                // Company filter
                if (companyFilter) {
                    const companyName = row.getAttribute('data-company');
                    if (companyName !== companyFilter) {
                        showRow = false;
                    }
                }

                // Status filter
                if (statusFilter && row.getAttribute('data-status') !== statusFilter) {
                    showRow = false;
                }

                // Show/hide row
                row.style.display = showRow ? '' : 'none';
                if (showRow) visibleCount++;
            });

            // Show empty state if no rows visible
            const emptyState = document.querySelector('.empty-state');
            if (emptyState) {
                const emptyRow = emptyState.closest('tr');
                emptyRow.style.display = visibleCount === 0 ? '' : 'none';
            }

            // Update statistics with filtered data
            updateStatistics();
        }

        // Add hover effects to table rows
        const tableRows = document.querySelectorAll('.report-table tbody tr');
        tableRows.forEach(row => {
            row.addEventListener('mouseenter', function() {
                this.style.transform = 'translateY(-2px)';
            });
            
            row.addEventListener('mouseleave', function() {
                this.style.transform = 'translateY(0)';
            });
        });

        // Initial filter application
        filterTable();
    });
</script>
@endsection