<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Fee Statement</title>
    <style>
        body { font-family: sans-serif; font-size: 13px; }
        table { width: 100%; border-collapse: collapse; margin-top: 15px; }
        th, td { border: 1px solid #000; padding: 6px; text-align: right; }
        th { background-color: #f0f0f0; }
        td.left { text-align: left; }
    </style>
</head>
<body>
    <h2>Student Fee Statement</h2>
    <p><strong>Registration Number:</strong> {{ $student->registration_number }}</p>
    <p><strong>Name:</strong> {{ $student->first_name }} {{ $student->last_name }}</p>
    <p><strong>Programme:</strong> {{ $programme }}</p>

    <table>
        <thead>
            <tr>
                <th>Date</th>
                <th class="left">Description</th>
                <th>Debit ($)</th>
                <th>Credit ($)</th>
                <th>Balance ($)</th>
            </tr>
        </thead>
        <tbody>
            @php $balance = 0; @endphp
            @foreach ($transactions as $tx)
                @php
                    $debit = $tx->debit ?? 0;
                    $credit = $tx->credit ?? 0;
                    $balance += ($debit - $credit);
                @endphp
                <tr>
                    <td>{{ \Carbon\Carbon::parse($tx->date)->format('d M Y') }}</td>
                    <td class="left">{{ $tx->description }}</td>
                    <td>{{ $debit > 0 ? number_format($debit, 2) : '' }}</td>
                    <td>{{ $credit > 0 ? number_format($credit, 2) : '' }}</td>
                    <td>{{ number_format($balance, 2) }}</td>
                </tr>
            @endforeach
        </tbody>
    </table>

    <h4 style="margin-top: 20px;">Total Balance: ${{ number_format($balance, 2) }}</h4>
</body>
</html>
