<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    @php use Illuminate\Support\Str; @endphp
    <title>Outstanding Sales Report</title>
    <style>
        @page { 
            size: A4; 
            margin: 15px 15px 30px 15px; /* Increased bottom margin for footer */
        }
        html, body {
            font-family: 'DejaVu Sans', sans-serif;
            font-size: 9pt;
            margin: 0; padding: 0;
            color: #333;
            position: relative;
        }
        body { 
            padding: 15px; 
            box-sizing: border-box; 
            padding-bottom: 40px; /* Space for footer */
        }

        .pdf-page { 
            page-break-after: always; 
            position: relative;
            min-height: 100%;
        }
        .pdf-page:last-child { page-break-after: auto; }

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

        .header-container { 
            border-bottom: 1px solid #e2e8f0; 
            padding-bottom: 15px; 
            margin-bottom: 15px; 
        }

        .info-row { width: 100%; border-collapse: collapse; }
        .info-row td { padding: 5px; vertical-align: top; }
        .info-left, .info-right { width: 50%; }
        .info-left { text-align: left; }
        .info-right { text-align: right; }
        .compact-info { font-size: 10pt; }

        .label, .value { 
            display: inline-block; 
            vertical-align: bottom; 
            font-size: 9pt; 
            font-family: 'DejaVu Sans', sans-serif; 
        }
        
        .label {
            display: inline-block;
            vertical-align: bottom;
            font-family: 'DejaVu Sans', sans‑serif;
            font-size: 9pt;
            font-weight: normal;
            color: #1a365d;
        }

        .section-title { 
            font-size: 10pt; 
            font-weight: bold; 
            color: #1a365d; 
            text-transform: uppercase; 
            margin-bottom: 5px; 
            border-bottom: 1px solid #1a365d; 
            padding-bottom: 2px; 
            display: block; 
        }

        .logo-full { text-align: left; margin-bottom: 1px; }
        .logo-full img { max-width: 100px; }

        .doc-info { 
            display: flex; 
            justify-content: space-between; 
            font-size: 9pt; 
            padding: 10px; 
            background-color: #f8fafc; 
            border: 1px solid #e2e8f0; 
            border-radius: 4px; 
            margin-bottom: 15px; 
        }
        .doc-info div { line-height: 1.5; }

        .items-table { 
            width: 100%; 
            border-collapse: collapse; 
            font-size: 9pt; 
            margin-bottom: 15px; 
        }
        .items-table th { 
            background-color: #1a365d; 
            color: #fff; 
            padding: 8px; 
            text-align: center; 
        }
        .items-table td { 
            border: 1px solid #e2e8f0; 
            padding: 8px; 
            text-align: center; 
        }
        .items-table tr:nth-child(even) { background-color: #f8fafc; }

        .total-row { 
            border-top: 1px solid #333; 
            font-weight: bold; 
            background-color: #1a365d;
            color: #fff;
        }

        .negative { color: red; }
        .text-right { text-align: right; }
        
        /* Footer styles */
        .footer {
            position: absolute;
            bottom: -25px;
            left: 0;
            right: 0;
            text-align: center;
            font-size: 8pt;
            color: #666;
            border-top: 1px solid #e2e8f0;
            padding-top: 5px;
        }
        .footer table {
            width: 100%;
        }
        .footer td {
            padding: 0;
        }
        .footer .text-right {
            text-align: right;
        }
    </style>
</head>
<body>
    <div class="pdf-page">
        <div class="document-title">
            Outstanding Sales Report
        </div>

        <div class="header-container">
            <table class="info-row compact-info">
                <tr>
                    <td class="info-left">
                        <span class="section-title">Company Information</span>
                        <div class="logo-full">
                            @if($company && $company->logo_path)
                                <img src="file://{{ public_path($company->logo_path) }}" alt="Company Logo">
                            @endif
                        </div>
                        <div><span class="label">Name: </span><span class="value">{{ $company->name ?? 'Company Name' }}</span></div>
                        <div><span class="label">Address: </span><span class="value">{{ $company->address ?? 'Company Address' }}</span></div>
                        <div><span class="label">Phone: </span><span class="value">{{ $company->phone ?? 'Company Phone' }}</span></div>
                        <div><span class="label">Email: </span><span class="value">{{ $company->email ?? 'Company Email' }}</span></div>
                        @if($company && $company->vat_no)
                            <div><span class="label">VAT No: </span><span class="value">{{ $company->vat_no }}</span></div>
                        @endif
                        @if($company && $company->tin_no)
                            <div><span class="label">TIN No: </span><span class="value">{{ $company->tin_no }}</span></div>
                        @endif
                    </td>
                    <td class="info-right">
                        <span class="section-title">Report Information</span>
                        <div><span class="label">Report Type: </span><span class="value">{{ ucfirst($reportType) }}</span></div>
                        <div><span class="label">Period: </span><span class="value">{{ $fromDate }} to {{ $toDate }}</span></div>
                        <div><span class="label">Report Date: </span><span class="value">{{ date('d M Y') }}</span></div>
                    </td>
                </tr>
            </table>
        </div>

        <div class="doc-info">
            <div>
                <div><span class="label">Total Outstanding:</span> <strong>USD{{ number_format($totalOutstanding, 2) }}</strong></div>
            </div>
        </div>

        @if($reportType === 'summary')
            <table class="items-table">
                <thead>
                    <tr>
                        <th>Date</th>
                        <th>Customer Code</th>
                        <th>Name</th>
                        <th>Balance (USD)</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($invoiceBalances as $item)
                        <tr>
                            <td>{{ \Carbon\Carbon::parse($item['invoice']->document_date)->format('d/m/Y') }}</td>
                            <td>{{ $item['invoice']->customer->customer_code }}</td>
                            <td>{{ $item['invoice']->customer->customer_account }}</td>
                            <td class="text-right">{{ number_format($item['balance'], 2) }}</td>
                        </tr>
                    @endforeach
                </tbody>
                <tfoot>
                    <tr class="total-row">
                        <td colspan="3" class="text-right">Total Outstanding:</td>
                        <td class="text-right">USD{{ number_format($totalOutstanding, 2) }}</td>
                    </tr>
                </tfoot>
            </table>
        @else
            @foreach($customerInvoices as $customerData)
                <h4 class="section-title">Customer: {{ $customerData['customer']->customer_account }} | Code: {{ $customerData['customer']->customer_code }}</h4>
                
                <table class="items-table">
                    <thead>
                        <tr>
                            <th>Date</th>
                            <th>Invoice</th>
                            <th>Amount (USD)</th>
                            <th>Tax</th>
                            <th>Discount</th>
                            <th>Amount Paid (USD)</th>
                            <th>Balance (USD)</th>
                        </tr>
                    </thead>
                    <tbody>
                        @php
                            $customerBalance = 0;
                        @endphp
                        @foreach($customerData['invoices'] as $item)
                            @php
                                $customerBalance += $item['balance'];
                            @endphp
                            <tr>
                                <td>{{ \Carbon\Carbon::parse($item['invoice']->document_date)->format('d/m/Y') }}</td>
                                <td>{{ $item['invoice']->document_number }}</td>
                                <td class="text-right">{{ number_format($item['invoice']->inclusive_total, 2) }}</td>
                                <td class="text-right">{{ number_format($item['invoice']->tax_amount, 2) }}</td>
                                <td class="text-right">{{ number_format($item['invoice']->discount_total, 2) }}</td>
                                <td class="text-right">{{ number_format($item['amount_paid'], 2) }}</td>
                                <td class="text-right">{{ number_format($item['balance'], 2) }}</td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
                
                <div class="text-right mb-3">
                    <strong>Closing balance for {{ strtoupper($customerData['customer']->customer_account) }} : USD{{ number_format($customerBalance, 2) }}</strong>
                </div>
                <hr>
            @endforeach
            
            <div class="mt-3">
                <div class="text-right">
                    <strong>Total Outstanding: USD{{ number_format($totalOutstanding, 2) }}</strong>
                </div>
            </div>
        @endif
        
        <!-- Footer for each page -->
        <div class="footer">
            <table>
                <tr>
                    <td>&copy; {{ date('Y') }} Melsoft (Private) Limited. All rights reserved.</td>
                    <td class="text-right">Printed: {{ now()->setTimezone('Africa/Harare')->format('d M Y H:i') }}</td>
                </tr>
            </table>
        </div>
    </div>
</body>
</html>