<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Supplier Journal Report</title>
    <style>
        body { font-family: Arial, sans-serif; font-size: 12px; margin: 20px; }
        .header { text-align: center; margin-bottom: 20px; }
        .info p { margin: 5px 0; }
        table { width: 100%; border-collapse: collapse; margin-top: 16px; }
        th, td { border: 1px solid #000; padding: 6px; text-align: left; }
        th { background: #f2f2f2; }
        .text-right { text-align: right; }
        .text-center { text-align: center; }
        .total-row { font-weight: bold; background: #e9ecef; }
    </style>
</head>
<body>
    @include('modules.reports.partials.pdf-company-details')
    <div class="header">
        <h2>Supplier Journal Report</h2>
    </div>

    <div class="info">
        <p><strong>Date:</strong> {{ date('F j, Y') }}</p>
        <p><strong>Currency:</strong> {{ $currnames }}</p>
        <p><strong>Period:</strong> {{ $pfrom }} to {{ $pto }}</p>
    </div>

    @if(isset($summary))
    <table>
        <thead>
            <tr>
                <th colspan="4">Current State Summary</th>
                <th class="text-right">Amount</th>
                <th class="text-right">Count</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td colspan="4">Pending</td>
                <td class="text-right">{{ number_format($summary['statusAmounts']['pending'] ?? 0, 2) }}</td>
                <td class="text-right">{{ $summary['statusCounts']['pending'] ?? 0 }}</td>
            </tr>
            <tr>
                <td colspan="4">Approved</td>
                <td class="text-right">{{ number_format($summary['statusAmounts']['approved'] ?? 0, 2) }}</td>
                <td class="text-right">{{ $summary['statusCounts']['approved'] ?? 0 }}</td>
            </tr>
            <tr>
                <td colspan="4">Rejected</td>
                <td class="text-right">{{ number_format($summary['statusAmounts']['rejected'] ?? 0, 2) }}</td>
                <td class="text-right">{{ $summary['statusCounts']['rejected'] ?? 0 }}</td>
            </tr>
            <tr>
                <td colspan="4">Reversed</td>
                <td class="text-right">{{ number_format($summary['statusAmounts']['reversed'] ?? 0, 2) }}</td>
                <td class="text-right">{{ $summary['statusCounts']['reversed'] ?? 0 }}</td>
            </tr>
            <tr class="total-row">
                <td colspan="4">Approval Rate</td>
                <td class="text-right">{{ number_format($summary['approvalRate'] ?? 0, 2) }}%</td>
                <td class="text-right">{{ $summary['totalEntries'] ?? 0 }}</td>
            </tr>
        </tbody>
    </table>
    @endif

    <table>
        <thead>
            <tr>
                <th class="text-center">Date</th>
                <th>Supplier Code</th>
                <th>Supplier Name</th>
                <th>Description</th>
                <th class="text-right">Debit</th>
                <th class="text-right">Credit</th>
                <th class="text-center">Status</th>
                <th class="text-center">Currency</th>
            </tr>
        </thead>
        <tbody>
            @forelse($journalEntries as $entry)
                @php
                    $debit = (float) ($entry->dr ?? 0);
                    $credit = (float) ($entry->cr ?? 0);
                @endphp
                <tr>
                    <td class="text-center">{{ \Carbon\Carbon::parse($entry->date)->format('M d, Y') }}</td>
                    <td>{{ $entry->supplier_code ?? 'N/A' }}</td>
                    <td>{{ $entry->supplier_name ?? 'N/A' }}</td>
                    <td>{{ $entry->narration }}</td>
                    <td class="text-right">{{ $debit > 0 ? number_format($debit, 2) : '-' }}</td>
                    <td class="text-right">{{ $credit > 0 ? number_format($credit, 2) : '-' }}</td>
                    <td class="text-center">{{ strtoupper($entry->status ?? 'pending') }}</td>
                    <td class="text-center">{{ $entry->currency_name ?? 'N/A' }}</td>
                </tr>
            @empty
                <tr>
                    <td colspan="8" style="text-align: center;">No supplier journal entries found</td>
                </tr>
            @endforelse
        </tbody>
        <tfoot>
            <tr class="total-row">
                <td colspan="4" class="text-right"><strong>Total</strong></td>
                <td class="text-right"><strong>{{ number_format($totalDebit, 2) }}</strong></td>
                <td class="text-right"><strong>{{ number_format($totalCredit, 2) }}</strong></td>
                <td></td>
                <td></td>
            </tr>
        </tfoot>
    </table>
</body>
</html>
