<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Supplier Ledger - {{ $supplier->name }}</title>
    @include('reports.pdf-style')
</head>
<body>
    <div class="header">
        <h1>Supplier Ledger</h1>
        <div class="subtitle">{{ \Carbon\Carbon::parse($from)->format('d M Y') }} — {{ \Carbon\Carbon::parse($to)->format('d M Y') }}</div>
    </div>

    <div class="info-box">
        <table style="width:100%;">
            <tr>
                <td style="width:50%;">
                    <div class="label">Supplier</div>
                    <div class="value">{{ $supplier->name }}</div>
                </td>
                <td style="width:25%;">
                    <div class="label">Phone</div>
                    <div class="value">{{ $supplier->phone ?: '-' }}</div>
                </td>
                <td style="width:25%; text-align:right;">
                    <div class="label">Current Balance Due</div>
                    <div class="value" style="color:#c0392b;">Rs. {{ number_format($supplier->balance, 2) }}</div>
                </td>
            </tr>
        </table>
    </div>

    <table class="ledger">
        <thead>
            <tr>
                <th>Date</th>
                <th>Description</th>
                <th class="text-right">Purchase (Debit)</th>
                <th class="text-right">Payment (Credit)</th>
                <th class="text-right">Running Balance</th>
            </tr>
        </thead>
        <tbody>
            @forelse($ledger as $entry)
            <tr>
                <td>{{ \Carbon\Carbon::parse($entry['date'])->format('d M Y') }}</td>
                <td>{{ $entry['description'] }}</td>
                <td class="text-right debit">{{ $entry['debit'] > 0 ? 'Rs. ' . number_format($entry['debit'], 2) : '' }}</td>
                <td class="text-right credit">{{ $entry['credit'] > 0 ? 'Rs. ' . number_format($entry['credit'], 2) : '' }}</td>
                <td class="text-right balance">Rs. {{ number_format($entry['balance'], 2) }}</td>
            </tr>
            @empty
            <tr><td colspan="5" style="text-align:center; color:#999;">No transactions in this period</td></tr>
            @endforelse
        </tbody>
        <tr class="total-bar">
            <td colspan="2" style="text-align:right;">TOTAL</td>
            <td style="text-align:right;">Rs. {{ number_format(array_sum(array_column($ledger, 'debit')), 2) }}</td>
            <td style="text-align:right;">Rs. {{ number_format(array_sum(array_column($ledger, 'credit')), 2) }}</td>
            <td style="text-align:right;">Rs. {{ number_format(!empty($ledger) ? end($ledger)['balance'] : 0, 2) }}</td>
        </tr>
    </table>

    <div class="page-footer">Generated on {{ now()->format('d M Y, h:i A') }}</div>
</body>
</html>
