<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Customer Ledger Statement</title>
    <!-- Premium Google Fonts -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    
    <style>
        body {
            font-family: 'Inter', sans-serif;
            color: #1f2937;
            background-color: #ffffff;
            font-size: 11px;
            line-height: 1.5;
            padding: 40px;
            margin: 0;
        }
        
        .no-print {
            background: #f3f4f6;
            padding: 12px 24px;
            margin-bottom: 30px;
            border-radius: 12px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            border: 1px solid #e5e7eb;
        }
        .no-print span {
            font-weight: 500;
            color: #4b5563;
        }
        .print-btn {
            padding: 8px 16px;
            background: #4f46e5;
            color: white;
            border: none;
            border-radius: 8px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.2s;
            font-size: 11px;
        }
        .print-btn:hover {
            background: #4338ca;
        }

        .header-container {
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            border-bottom: 2px solid #f3f4f6;
            padding-bottom: 25px;
            margin-bottom: 30px;
        }
        .logo-section img {
            max-height: 50px;
            object-fit: contain;
        }
        .logo-placeholder {
            font-size: 20px;
            font-weight: 800;
            color: #4f46e5;
            letter-spacing: -0.5px;
        }
        .title-section {
            text-align: right;
        }
        .title-section h1 {
            margin: 0;
            font-size: 22px;
            font-weight: 800;
            color: #111827;
            letter-spacing: -0.5px;
            text-transform: uppercase;
        }
        .title-section p {
            margin: 5px 0 0 0;
            color: #6b7280;
            font-weight: 500;
        }

        .details-grid {
            display: grid;
            grid-template-cols: 1fr 1fr;
            gap: 40px;
            margin-bottom: 35px;
        }
        .details-card {
            background: #fafafa;
            border: 1px solid #f3f4f6;
            border-radius: 12px;
            padding: 20px;
        }
        .details-card h3 {
            margin: 0 0 10px 0;
            font-size: 11px;
            font-weight: 700;
            color: #9ca3af;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }
        .details-card h2 {
            margin: 0 0 8px 0;
            font-size: 15px;
            font-weight: 700;
            color: #111827;
        }
        .details-row {
            display: flex;
            justify-content: space-between;
            margin-bottom: 6px;
            font-size: 11px;
        }
        .details-row span.label {
            color: #6b7280;
        }
        .details-row span.value {
            font-weight: 500;
            color: #374151;
        }

        .period-badge {
            display: inline-block;
            background: #e0e7ff;
            color: #4f46e5;
            padding: 4px 10px;
            border-radius: 9999px;
            font-weight: 600;
            font-size: 10px;
            margin-bottom: 25px;
        }

        .summary-ribbon {
            display: grid;
            grid-template-cols: repeat(3, 1fr);
            gap: 20px;
            margin-bottom: 30px;
        }
        .summary-stat {
            border: 1px solid #e5e7eb;
            border-radius: 12px;
            padding: 16px;
            background: #fff;
        }
        .summary-stat p.stat-lbl {
            margin: 0 0 5px 0;
            font-size: 10px;
            font-weight: 600;
            color: #6b7280;
            text-transform: uppercase;
        }
        .summary-stat p.stat-val {
            margin: 0;
            font-size: 18px;
            font-weight: 700;
            color: #111827;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 15px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.02);
            border-radius: 8px;
            overflow: hidden;
        }
        th, td {
            padding: 10px 14px;
            text-align: left;
            border-bottom: 1px solid #f3f4f6;
        }
        th {
            background-color: #f9fafb;
            font-weight: 600;
            color: #4b5563;
            font-size: 10px;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }
        td {
            font-size: 11px;
        }
        tr:last-child td {
            border-bottom: none;
        }
        .text-right {
            text-align: right;
        }
        .font-mono {
            font-family: monospace;
            font-size: 12px;
            font-weight: 500;
        }
        
        .badge {
            display: inline-block;
            padding: 2px 6px;
            border-radius: 4px;
            font-size: 9px;
            font-weight: 600;
            text-transform: uppercase;
        }
        .badge-sale { bg-color: #eff6ff; color: #1e40af; border: 1px solid #bfdbfe; }
        .badge-credit { bg-color: #fdf2f8; color: #9d174d; border: 1px solid #fbcfe8; }
        .badge-debit { bg-color: #fffbeb; color: #92400e; border: 1px solid #fde68a; }

        .totals-section {
            display: flex;
            justify-content: flex-end;
            margin-top: 30px;
        }
        .totals-box {
            width: 320px;
            background: #fafafa;
            border: 1px solid #f3f4f6;
            border-radius: 12px;
            padding: 16px;
        }
        .total-row {
            display: flex;
            justify-content: space-between;
            padding: 6px 0;
            font-size: 11px;
        }
        .total-row.grand {
            border-top: 2px solid #e5e7eb;
            margin-top: 6px;
            padding-top: 10px;
            font-weight: 700;
            font-size: 13px;
            color: #4f46e5;
        }

        .footer-declaration {
            margin-top: 60px;
            border-top: 1px solid #f3f4f6;
            padding-top: 20px;
            display: flex;
            justify-content: space-between;
            color: #9ca3af;
            font-size: 10px;
        }

        @media print {
            body { padding: 0; }
            .no-print { display: none; }
            .details-card { background: #ffffff !important; border: 1px solid #e5e7eb !important; }
            .summary-stat { background: #ffffff !important; }
            .totals-box { background: #ffffff !important; border: 1px solid #e5e7eb !important; }
        }
    </style>
</head>
<body>
    <div class="no-print">
        <span>Ledger Report Preview. Click 'Print Statement' to save or print.</span>
        <button class="print-btn" onclick="window.print()">Print Statement</button>
    </div>

    <div class="header-container">
        <div class="logo-section">
            @php
                $logoUrl = $business ? \App\Models\Setting::get('invoice_logo_url', '', $business->id) : '';
            @endphp
            @if($logoUrl)
                <img src="{{ $logoUrl }}" alt="Logo">
            @else
                <span class="logo-placeholder">{{ $business->name ?? 'FBR Digital Invoicing' }}</span>
            @endif
        </div>
        <div class="title-section">
            <h1>Customer Statement</h1>
            <p>Date: {{ now()->format('d M, Y') }}</p>
        </div>
    </div>

    <div class="details-grid">
        <div class="details-card">
            <h3>Company Identity</h3>
            <h2>{{ $business->name }}</h2>
            <div class="details-row"><span class="label">NTN Number:</span><span class="value">{{ $business->ntn ?? '—' }}</span></div>
            <div class="details-row"><span class="label">STRN Number:</span><span class="value">{{ $business->strn ?? '—' }}</span></div>
            <div class="details-row"><span class="label">Email Address:</span><span class="value">{{ $business->email ?? '—' }}</span></div>
        </div>
        <div class="details-card">
            <h3>Customer Profile</h3>
            @if($customer)
                <h2>{{ $customer->name }}</h2>
                <div class="details-row"><span class="label">NTN / CNIC:</span><span class="value">{{ $customer->ntn_cnic ?? '—' }}</span></div>
                <div class="details-row"><span class="label">Type:</span><span class="value">{{ $customer->registration_type }}</span></div>
                <div class="details-row"><span class="label">Contact:</span><span class="value">{{ $customer->phone ?? '—' }}</span></div>
            @else
                <h2>All Active Customers</h2>
                <div class="details-row"><span class="label">Scope:</span><span class="value">Consolidated Ledger</span></div>
            @endif
        </div>
    </div>

    @if($request->filled('from_date') || $request->filled('to_date'))
        <div class="period-badge">
            Statement Period: {{ $request->input('from_date', 'Start') }} to {{ $request->input('to_date', 'Present') }}
        </div>
    @endif

    <div class="summary-ribbon">
        <div class="summary-stat">
            <p class="stat-lbl">Ledger Sales Subtotal</p>
            <p class="stat-val">Rs. {{ number_format($invoices->sum('subtotal'), 2) }}</p>
        </div>
        <div class="summary-stat">
            <p class="stat-lbl">Sales Tax Amount</p>
            <p class="stat-val">Rs. {{ number_format($invoices->sum('total_tax'), 2) }}</p>
        </div>
        <div class="summary-stat">
            <p class="stat-lbl">Grand Total Sales</p>
            <p class="stat-val" style="color: #4f46e5;">Rs. {{ number_format($invoices->sum('total_amount'), 2) }}</p>
        </div>
    </div>

    <table>
        <thead>
            <tr>
                <th style="width: 100px;">Invoice Date</th>
                <th>Reference No</th>
                <th>Type</th>
                <th>FBR Reference ID</th>
                <th>FBR Sync Status</th>
                <th class="text-right">Subtotal (Excl. Tax)</th>
                <th class="text-right">Sales Tax</th>
                <th class="text-right">Total Amount</th>
            </tr>
        </thead>
        <tbody>
            @forelse($invoices as $inv)
                <tr>
                    <td>{{ \Illuminate\Support\Carbon::parse($inv->invoice_date)->format('d M, Y') }}</td>
                    <td><strong>{{ $inv->local_invoice_no }}</strong></td>
                    <td>
                        @if($inv->invoice_type === 'Sale Invoice')
                            <span class="badge badge-sale">Sale Invoice</span>
                        @elseif($inv->invoice_type === 'Credit Note')
                            <span class="badge badge-credit">Credit Note</span>
                        @else
                            <span class="badge badge-debit">Debit Note</span>
                        @endif
                    </td>
                    <td><span style="color: #6b7280; font-family: monospace;">{{ $inv->fbr_invoice_number ?? '—' }}</span></td>
                    <td>
                        <span style="font-weight: 600; font-size: 10px; color: {{ ($inv->sync_status->value ?? $inv->sync_status) === 'synced' ? '#10b981' : (($inv->sync_status->value ?? $inv->sync_status) === 'failed' ? '#ef4444' : '#6b7280') }}">
                            {{ strtoupper($inv->sync_status->value ?? $inv->sync_status) }}
                        </span>
                    </td>
                    <td class="text-right font-mono">{{ number_format($inv->subtotal, 2) }}</td>
                    <td class="text-right font-mono" style="color: #6b7280;">{{ number_format($inv->total_tax, 2) }}</td>
                    <td class="text-right font-mono" style="font-weight: 600;">{{ number_format($inv->total_amount, 2) }}</td>
                </tr>
            @empty
                <tr>
                    <td colspan="8" style="text-align: center; padding: 30px; color: #9ca3af; font-weight: 500;">No ledger transaction records found for the matching filter set.</td>
                </tr>
            @endforelse
        </tbody>
    </table>

    <div class="totals-section">
        <div class="totals-box">
            <div class="total-row">
                <span>Subtotal (Excl. Tax):</span>
                <span class="font-mono">Rs. {{ number_format($invoices->sum('subtotal'), 2) }}</span>
            </div>
            <div class="total-row">
                <span>Sales Tax Total:</span>
                <span class="font-mono">Rs. {{ number_format($invoices->sum('total_tax'), 2) }}</span>
            </div>
            <div class="total-row grand">
                <span>Grand Total:</span>
                <span class="font-mono">Rs. {{ number_format($invoices->sum('total_amount'), 2) }}</span>
            </div>
        </div>
    </div>

    <div class="footer-declaration">
        <span>FBR Invoicing Statement System</span>
        <span>Signature / Stamp: ________________________</span>
    </div>

    <script>
        window.addEventListener('DOMContentLoaded', () => {
            setTimeout(() => {
                window.print();
            }, 600);
        });
    </script>
</body>
</html>
