<?php
$page_title = 'Transactions';
require __DIR__ . '/_guard.php';

$tx = db_all(
    'SELECT t.*, i.number AS invoice_number
       FROM transactions t LEFT JOIN invoices i ON t.invoice_id=i.id
       WHERE t.member_id=:m ORDER BY t.occurred_at DESC',
    ['m'=>$member['id']]
);

$totals = ['charge'=>0,'payment'=>0,'refund'=>0];
foreach ($tx as $t) {
    $tp = $t['type'];
    if (isset($totals[$tp])) $totals[$tp] += (int)$t['amount_cents'];
}

function cr(int $c): string {
    $neg=$c<0; $abs=abs($c);
    return ($neg?'-':'').'R '.number_format($abs/100,2,'.','&nbsp;');
}
?>
<style>
.m-page{padding:2rem 0 3rem;}
.m-section{background:#fff;border:1px solid var(--line);border-radius:var(--radius);margin-bottom:1.25rem;overflow:hidden;}
.m-section-head{padding:1rem 1.25rem;border-bottom:1px solid var(--line);display:flex;justify-content:space-between;align-items:center;}
.m-section-head h2{margin:0;font-size:1rem;}
.mtbl{width:100%;border-collapse:collapse;font-size:.875rem;}
.mtbl th{padding:.5rem 1.25rem;background:var(--surface-alt);font-size:.7rem;text-transform:uppercase;letter-spacing:.05em;color:var(--ink-muted);border-bottom:1px solid var(--line);text-align:left;white-space:nowrap;}
.mtbl td{padding:.65rem 1.25rem;border-bottom:1px solid var(--line);vertical-align:middle;}
.mtbl tr:last-child td{border-bottom:none;}
.mtbl tbody tr:hover td{background:#fafafa;}
.stat-cards{display:grid;grid-template-columns:repeat(3,1fr);gap:1rem;margin-bottom:1.25rem;}
.stat-c{background:#fff;border:1px solid var(--line);border-radius:var(--radius);padding:1.1rem 1.25rem;text-align:center;}
.stat-c .lbl{font-size:.72rem;text-transform:uppercase;letter-spacing:.05em;color:var(--ink-muted);margin:0 0 .2rem;}
.stat-c .val{font-size:1.5rem;font-weight:800;margin:0;}
</style>

<div class="m-page">
<div class="container">

<div style="margin-bottom:1.5rem;">
    <h1 style="margin:0 0 .25rem;">Transactions</h1>
    <p style="margin:0;color:var(--ink-muted);font-size:.9rem;">Every charge and payment on your account.</p>
</div>

<div class="stat-cards">
    <div class="stat-c">
        <p class="lbl">Total charged</p>
        <p class="val" style="color:var(--brand-accent);"><?= cr($totals['charge']) ?></p>
    </div>
    <div class="stat-c">
        <p class="lbl">Total paid</p>
        <p class="val" style="color:var(--brand-primary);"><?= cr(-$totals['payment']) ?></p>
    </div>
    <div class="stat-c">
        <p class="lbl">Transactions</p>
        <p class="val"><?= count($tx) ?></p>
    </div>
</div>

<div class="m-section">
    <div class="m-section-head">
        <h2>All transactions</h2>
    </div>
    <?php if (empty($tx)): ?>
        <div style="padding:1.25rem;color:var(--ink-muted);font-size:.875rem;">
            No transactions yet. They'll appear here once your first invoice is processed.
        </div>
    <?php else: ?>
    <table class="mtbl">
        <thead>
            <tr>
                <th>Date</th>
                <th>Type</th>
                <th>Description</th>
                <th>Invoice</th>
                <th>Reference</th>
                <th style="text-align:right;">Amount</th>
            </tr>
        </thead>
        <tbody>
        <?php foreach ($tx as $t):
            $amt = (int)$t['amount_cents'];
            $is_credit = $amt < 0;
        ?>
            <tr>
                <td style="color:var(--ink-muted);white-space:nowrap;">
                    <?= date('j M Y', strtotime($t['occurred_at'])) ?>
                </td>
                <td>
                    <span class="tag <?= $is_credit?'tag-ok':'tag-err' ?>">
                        <?= htmlspecialchars($t['type']) ?>
                    </span>
                </td>
                <td><?= htmlspecialchars($t['description']) ?></td>
                <td>
                    <?= $t['invoice_number']
                        ? '<code style="font-size:.8rem;">'.htmlspecialchars($t['invoice_number']).'</code>'
                        : '<span style="color:var(--ink-muted);">—</span>' ?>
                </td>
                <td style="color:var(--ink-muted);font-size:.82rem;">
                    <?= $t['reference'] ? htmlspecialchars($t['reference']) : '—' ?>
                </td>
                <td style="text-align:right;font-weight:600;color:<?= $is_credit?'var(--brand-primary)':'var(--brand-accent)' ?>;">
                    <?= cr($amt) ?>
                </td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
    <?php endif; ?>
</div>

</div>
</div>
<?php require __DIR__ . '/_footer.php'; ?>