<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Supplier Payments Report</title>
    <style>
        @page { 
            size: A4 landscape; 
            margin: 15px 15px 30px 15px;
        }
        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;
        }

        .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: 8pt; 
            margin-bottom: 15px; 
        }
        .items-table th { 
            background-color: #1a365d; 
            color: #fff; 
            padding: 8px; 
            text-align: center; 
        }
        .items-table td { 
            border: 1px solid #e2e8f0; 
            padding: 6px; 
            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">
            Supplier Payments 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
                        <div><span class="label">Currency: </span><span class="value">{{ $currency->code }}</span></div>
                    </td>
                    <td class="info-right">
                        <span class="section-title">Report Information</span>
                        <div><span class="label">Period: </span><span class="value">{{ $dateFrom }} to {{ $dateTo }}</span></div>
                        <div><span class="label">Report Date: </span><span class="value">{{ date('d M Y') }}</span></div>
                        <div><span class="label">Currency: </span><span class="value">{{ $currency->code }}</span></div>
                    </td>
                </tr>
            </table>
        </div>

        <div class="doc-info">
            <div>
                <div><span class="label">Currency:</span> <strong>{{ $currency->code }}</strong></div>
                <div><span class="label">Period:</span> <strong>{{ $dateFrom }} to {{ $dateTo }}</strong></div>
            </div>
            <div>
                <div><span class="label">Report Date:</span> <strong>{{ date('d M Y') }}</strong></div>
            </div>
        </div>

        <table class="items-table">
            <thead>
                <tr>
                    <th>Date</th>
                    <th>Description</th>
                    <th>Reference</th>
                    <th>Supplier Code</th>
                    <th>Supplier Name</th>
                    <th>DR</th>
                    <th>CR</th>
                    <th>Balance</th>
                </tr>
            </thead>
            <tbody>
                @php
                    $grandTotal = 0;
                @endphp
                @foreach($paymentsData as $row)
                    @php
                        $grandTotal = $row['balance'];
                    @endphp
                    <tr>
                        <td>{{ $row['date'] }}</td>
                        <td>{{ $row['description'] }}</td>
                        <td>{{ $row['reference'] }}</td>
                        <td>{{ $row['supplier_code'] }}</td>
                        <td>{{ $row['supplier_name'] }}</td>
                        <td class="text-right">{{ number_format($row['dr'], 2) }}</td>
                        <td class="text-right">{{ number_format($row['cr'], 2) }}</td>
                        <td class="text-right @if($row['balance'] < 0) negative @endif">
                            @if($row['balance'] < 0)
                                ({{ number_format(abs($row['balance']), 2) }})
                            @else
                                {{ number_format($row['balance'], 2) }}
                            @endif
                        </td>
                    </tr>
                @endforeach

                <tr class="total-row">
                    <td colspan="7" class="text-right">Grand Total:</td>
                    <td class="text-right @if($grandTotal < 0) negative @endif">
                        @if($grandTotal < 0)
                            ({{ number_format(abs($grandTotal), 2) }})
                        @else
                            {{ number_format($grandTotal, 2) }}
                        @endif
                    </td>
                </tr>
            </tbody>
        </table>
        
        <!-- 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>