<?php $this->start('body');
    $acc     = $this->data['acc'];
    $lines   = $this->data['lines'] ?? [];
    $balance = (float)($this->data['balance'] ?? 0);
    $from    = $this->v('from');
    $to      = $this->v('to');
    $accent  = ['ASSET'=>'#22c55e','LIABILITY'=>'#f59e0b','EQUITY'=>'#8b5cf6','REVENUE'=>'#3b82f6','EXPENSE'=>'#ef4444'][strtoupper($acc['account_type'])] ?? '#64748b';
?>

<div class="d-flex align-items-center justify-content-between mb-3">
    <div>
        <h5 class="mb-0" style="font-weight:600;"><code class="text-body"><?= htmlspecialchars($acc['account_code']); ?></code> · <?= htmlspecialchars($acc['account_name']); ?></h5>
        <div class="text-muted" style="font-size:.72rem;">
            <span class="badge" style="background:<?= $accent; ?>;color:#fff;text-transform:capitalize;"><?= htmlspecialchars(strtolower($acc['account_type'])); ?></span>
            <?php if ((int)$acc['is_bank']===1): ?> · <i class="fas fa-university me-1"></i>Bank<?php endif; ?>
        </div>
    </div>
    <a href="<?= PROOT; ?>ChartOfAccounts" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i> COA</a>
</div>

<form method="get" action="<?= PROOT; ?>ChartOfAccounts/view/<?= (int)$acc['c0d']; ?>" class="card border-0 shadow-sm mb-3">
    <div class="card-body p-2">
        <div class="row g-2 align-items-end">
            <div class="col-md-4"><label class="form-label">From</label><input type="date" name="from" value="<?= htmlspecialchars($from); ?>" class="form-control form-control-sm"></div>
            <div class="col-md-4"><label class="form-label">To</label>  <input type="date" name="to"   value="<?= htmlspecialchars($to); ?>"   class="form-control form-control-sm"></div>
            <div class="col-md-4 text-end"><button type="submit" class="btn btn-sm btn-outline-secondary"><i class="fas fa-filter me-1"></i>Filter</button></div>
        </div>
    </div>
</form>

<div class="card border-0 shadow-sm mb-3">
    <div class="card-body p-3 d-flex justify-content-between">
        <div><div class="text-muted small">Activity lines</div><div style="font-weight:600;font-size:1.1rem;"><?= count($lines); ?></div></div>
        <div class="text-end"><div class="text-muted small">Balance (normal-side)</div><div style="font-weight:700;font-size:1.4rem;color:<?= $balance<0?'#ef4444':$accent; ?>;"><?= htmlspecialchars($acc['currency']); ?> <?= number_format($balance); ?></div></div>
    </div>
</div>

<div class="card border-0 shadow-sm">
    <div class="table-responsive">
        <table class="table table-sm mb-0 align-middle">
            <thead class="table-light">
                <tr>
                    <th>Date</th><th>JE #</th><th>Source</th><th>Ref</th><th>Narrative</th>
                    <th class="text-end">Debit</th><th class="text-end">Credit</th><th class="text-end">Running</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($lines as $ln): ?>
                    <tr>
                        <td class="text-muted small"><?= htmlspecialchars($ln['entry_date']); ?></td>
                        <td><a href="<?= PROOT; ?>Journals/view/<?= (int)$ln['je_id']; ?>" class="text-decoration-none"><strong><?= htmlspecialchars($ln['je_no']); ?></strong></a></td>
                        <td><span class="badge bg-light text-muted" style="text-transform:capitalize;"><?= htmlspecialchars(strtolower($ln['source_type'])); ?></span></td>
                        <td class="text-muted small"><?= htmlspecialchars($ln['source_ref']); ?></td>
                        <td class="text-muted"><?= htmlspecialchars($ln['line_narr'] ?: $ln['je_narr']); ?></td>
                        <td class="text-end"><?= (float)$ln['debit']  > 0 ? number_format((float)$ln['debit'])  : '—'; ?></td>
                        <td class="text-end"><?= (float)$ln['credit'] > 0 ? number_format((float)$ln['credit']) : '—'; ?></td>
                        <td class="text-end" style="font-weight:600;color:<?= (float)$ln['running_balance']<0?'#ef4444':''; ?>;"><?= number_format((float)$ln['running_balance']); ?></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($lines)): ?>
                    <tr><td colspan="8" class="text-center text-muted py-4">No activity on this account in this period.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<?php $this->end(); ?>
