<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>All Invoices - Print</title>
    <style>
        @media print {
            @page { size: A4; margin: 15px; }
            body { print-color-adjust: exact; -webkit-print-color-adjust: exact; }
        }

        html, body {
            font-family: 'DejaVu Sans', sans-serif;
            font-size: 10pt;
            line-height: 1.4;
            margin: 0;
            padding: 0;
            color: #333;
        }

        body { padding: 15px; box-sizing: border-box; }

        .document-title {
            text-align: center;
            font-size: 14pt;
            font-weight: bold;
            color: #28a745;
            margin-bottom: 15px;
            text-transform: uppercase;
        }

        .summary-section {
            background-color: #f8fafc;
            padding: 10px;
            margin-bottom: 15px;
            border: 1px solid #28a745;
            border-radius: 4px;
        }

        .summary-title {
            font-size: 12pt;
            font-weight: bold;
            color: #28a745;
            margin-bottom: 8px;
        }

        .summary-stats {
            display: flex;
            justify-content: space-around;
            font-size: 10pt;
        }

        .stat-item {
            text-align: center;
        }

        .stat-value {
            font-size: 12pt;
            font-weight: bold;
            color: #28a745;
        }

        .invoice-section {
            page-break-inside: avoid;
            margin-bottom: 15px;
            border: 1px solid #28a745;
            padding: 10px;
        }

        .invoice-header {
            background-color: #f8fafc;
            padding: 8px;
            margin-bottom: 10px;
            border-bottom: 1px solid #28a745;
        }

        .invoice-title {
            font-size: 12pt;
            font-weight: bold;
            color: #28a745;
            margin-bottom: 5px;
        }

        .invoice-info {
            display: flex;
            justify-content: space-between;
            font-size: 9pt;
            margin-bottom: 8px;
        }

        .customer-info, .invoice-details {
            flex: 1;
        }

        .customer-info { margin-right: 20px; }

        .label {
            font-weight: bold;
            color: #28a745;
            margin-right: 5px;
        }

        .items-table {
            width: 100%;
            border-collapse: collapse;
            font-size: 8pt;
            margin-bottom: 10px;
        }

        .items-table th {
            background-color: #ffc107;
            color: #333;
            padding: 5px;
            text-align: left;
            font-weight: bold;
        }

        .items-table td {
            border: 1px solid #28a745;
            padding: 5px;
            text-align: left;
        }

        .items-table td:nth-child(3) {
            text-align: right;
        }

        .total-row {
            background-color: #f8fafc;
            font-weight: bold;
        }

        .total-row td {
            text-align: right;
            padding: 5px;
        }

        .footer {
            position: fixed;
            bottom: 15px;
            left: 15px;
            right: 15px;
            font-size: 8pt;
            text-align: center;
            color: #666;
        }

        @media print {
            .no-print { display: none; }
            .page-break { page-break-before: always; }
        }
    </style>
</head>
<body>
    <div class="document-title">All Invoices Report</div>
    <div style="text-align: center; font-size: 10pt; margin-bottom: 15px;">
        Generated on: {{ now()->format('M d, Y H:i') }}
    </div>

    <div class="summary-section">
        <div class="summary-title">Summary Statistics</div>
        <div class="summary-stats">
            <div class="stat-item">
                <div>Total Invoices</div>
                <div class="stat-value">{{ $invoices->count() }}</div>
            </div>
            <div class="stat-item">
                <div>Paid Invoices</div>
                <div class="stat-value">{{ $invoices->where('status', 'paid')->count() }}</div>
            </div>
            <div class="stat-item">
                <div>Pending Invoices</div>
                <div class="stat-value">{{ $invoices->where('status', '!=', 'paid')->count() }}</div>
            </div>
            <div class="stat-item">
                <div>Total Amount</div>
                <div class="stat-value">${{ number_format($invoices->sum('total'), 2) }}</div>
            </div>
        </div>
    </div>

    @foreach($invoices as $invoice)
        <div class="invoice-section">
            <div class="invoice-header">
                <div class="invoice-title">Invoice: {{ $invoice->invoice_number }}</div>
                <div class="invoice-info">
                    <div class="customer-info">
                        <div><span class="label">Customer:</span> {{ $invoice->customer->customer_account ?? 'N/A' }}</div>
                        <div><span class="label">Status:</span>
                            <span style="color: {{ $invoice->status === 'paid' ? '#28a745' : ($invoice->status === 'sent' ? '#17a2b8' : '#6c757d') }};">
                                {{ ucfirst($invoice->status) }}
                            </span>
                        </div>
                    </div>
                    <div class="invoice-details">
                        <div><span class="label">Date:</span> {{ \Carbon\Carbon::parse($invoice->invoice_date)->format('M d, Y') }}</div>
                        <div><span class="label">Due Date:</span> {{ $invoice->due_date ? \Carbon\Carbon::parse($invoice->due_date)->format('M d, Y') : 'N/A' }}</div>
                        <div><span class="label">Created By:</span> {{ $invoice->creator->name ?? 'N/A' }}</div>
                    </div>
                </div>
            </div>

            <table class="items-table">
                <thead>
                    <tr>
                        <th>Item</th>
                        <th>Qty</th>
                        <th>Total</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($invoice->items ?? [] as $item)
                        <tr>
                            <td>{{ $item->item }}</td>
                            <td>{{ number_format($item->quantity, 2) }}</td>
                            <td>${{ number_format($item->line_total, 2) }}</td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="3" style="text-align: center; font-style: italic;">No items</td>
                        </tr>
                    @endforelse
                    <tr class="total-row">
                        <td colspan="2"><strong>Total Amount:</strong></td>
                        <td><strong>${{ number_format($invoice->total, 2) }}</strong></td>
                    </tr>
                </tbody>
            </table>
        </div>

        @if(!$loop->last)
            <div class="page-break"></div>
        @endif
    @endforeach

    <div class="footer">
        Generated by Credora System - {{ now()->format('M d, Y H:i') }}
    </div>

    <script>
        window.onload = function() {
            window.print();
        }
    </script>
</body>
</html>
