<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h4 class="mb-1"><?= lang('finance.member_statement') ?></h4>
        <p class="text-muted mb-0"><?= e($member['full_name_en']) ?> (<?= e($member['membership_number']) ?>) — <?= e($member['membership_type'] ?? '') ?></p>
    </div>
    <div class="d-flex gap-2">
        <button class="btn btn-outline-secondary" onclick="window.print()"><i class="fas fa-print me-1"></i><?= lang('common.print') ?></button>
    </div>
</div>

<div class="card mb-3">
    <div class="card-body py-2">
        <form method="GET" action="/finance/statement/<?= $member['id'] ?>" class="row g-2 align-items-end">
            <div class="col-md-3"><label class="form-label form-label-sm"><?= lang('finance.date_from') ?></label><input type="date" name="date_from" class="form-control form-control-sm" value="<?= e($dateFrom) ?>"></div>
            <div class="col-md-3"><label class="form-label form-label-sm"><?= lang('finance.date_to') ?></label><input type="date" name="date_to" class="form-control form-control-sm" value="<?= e($dateTo) ?>"></div>
            <div class="col-md-2"><button type="submit" class="btn btn-sm btn-primary"><?= lang('common.filter') ?></button></div>
        </form>
    </div>
</div>

<div class="row g-3 mb-3">
    <div class="col-md-3"><div class="card border-primary"><div class="card-body py-2 text-center"><small class="text-muted">Outstanding</small><br><strong class="text-danger"><?= money($member['outstanding_balance']) ?></strong></div></div></div>
    <div class="col-md-3"><div class="card border-success"><div class="card-body py-2 text-center"><small class="text-muted">Credit Balance</small><br><strong class="text-success"><?= money($member['credit_balance']) ?></strong></div></div></div>
    <div class="col-md-3"><div class="card border-info"><div class="card-body py-2 text-center"><small class="text-muted">Total Paid</small><br><strong><?= money($member['total_paid']) ?></strong></div></div></div>
    <div class="col-md-3"><div class="card border-warning"><div class="card-body py-2 text-center"><small class="text-muted">Overdue</small><br><strong class="text-warning"><?= money($balance['overdue_amount'] ?? 0) ?></strong></div></div></div>
</div>

<div class="card">
    <div class="card-body">
        <table class="table table-sm table-striped">
            <thead><tr><th><?= lang('finance.date') ?></th><th><?= lang('finance.type') ?></th><th><?= lang('finance.description') ?></th><th class="text-end"><?= lang('finance.debit') ?></th><th class="text-end"><?= lang('finance.credit') ?></th><th class="text-end"><?= lang('finance.balance') ?></th></tr></thead>
            <tbody>
            <?php if (empty($ledger)): ?>
            <tr><td colspan="6" class="text-center text-muted py-4">No transactions found for this period.</td></tr>
            <?php endif; ?>
            <?php foreach ($ledger as $entry): ?>
            <tr>
                <td><?= format_date($entry['transaction_date']) ?></td>
                <td><?= status_badge($entry['transaction_type']) ?></td>
                <td><?= e($entry['description_en']) ?></td>
                <td class="text-end"><?= (float)$entry['debit_amount'] > 0 ? '<span class="text-danger">' . format_number($entry['debit_amount']) . '</span>' : '' ?></td>
                <td class="text-end"><?= (float)$entry['credit_amount'] > 0 ? '<span class="text-success">' . format_number($entry['credit_amount']) . '</span>' : '' ?></td>
                <td class="text-end"><strong><?= format_number($entry['balance_after']) ?></strong></td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>