<?php $this->start('body');
    $asAt = $this->v('as_at');
    $rows = $this->data['rows'] ?? [];
    $sumD = array_sum(array_map(fn($r) => (float)$r['total_debit'],  $rows));
    $sumC = array_sum(array_map(fn($r) => (float)$r['total_credit'], $rows));
    $balances = abs($sumD - $sumC) < 0.01;

    $typeChip = [
        'ASSET'     => ['bg' => '#eae0d0', 'ink' => '#7a6039'],
        'LIABILITY' => ['bg' => '#e8d5cf', 'ink' => '#8a4a3a'],
        'EQUITY'    => ['bg' => '#d8e0ce', 'ink' => '#4f6b3f'],
        'REVENUE'   => ['bg' => '#d8e5d1', 'ink' => '#4a6b3a'],
        'EXPENSE'   => ['bg' => '#e8d5cf', 'ink' => '#8a4a3a'],
    ];
?>

<?php $q = 'as_at=' . urlencode($asAt); ?>
<div class="d-flex align-items-center justify-content-between mb-3">
    <h5 class="mb-0" style="font-weight:600;color:#4a3f36;"><i class="fas fa-balance-scale-right me-2" style="color:#c9a875;"></i>Trial Balance</h5>
    <div class="btn-group">
        <a href="<?= PROOT; ?>FinancialStatements/trialBalanceXlsx?<?= $q; ?>" class="btn btn-sm" style="background:#faf6f0;border:1px solid #eae0d0;color:#4a3f36;"><i class="fas fa-file-excel me-1" style="color:#4a6b3f;"></i>Excel</a>
        <a href="<?= PROOT; ?>FinancialStatements/trialBalancePdf?<?= $q; ?>"  class="btn btn-sm" style="background:#faf6f0;border:1px solid #eae0d0;color:#4a3f36;"><i class="fas fa-file-pdf   me-1" style="color:#8a4a3a;"></i>PDF</a>
    </div>
</div>

<form method="get" action="<?= PROOT; ?>FinancialStatements/trialBalance" class="card border-0 shadow-sm mb-3" style="background:#faf6f0;">
    <div class="card-body p-2">
        <div class="row g-2 align-items-end">
            <div class="col-md-3"><label class="form-label" style="color:#4a3f36;">As at</label><input type="date" name="as_at" value="<?= htmlspecialchars($asAt); ?>" class="form-control form-control-sm"></div>
            <div class="col-md-9 text-end">
                <button type="submit" class="btn btn-sm" style="background:#c9a875;border-color:#c9a875;color:#4a3f36;font-weight:600;"><i class="fas fa-filter me-1"></i>Refresh</button>
            </div>
        </div>
    </div>
</form>

<div class="card border-0 shadow-sm mb-3">
    <div class="card-header bg-white d-flex align-items-center justify-content-between" style="border-bottom:1px solid #eae0d0;">
        <strong style="color:#4a3f36;"><i class="fas fa-list me-2" style="color:#c9a875;"></i>All accounts &middot; as at <?= htmlspecialchars($asAt); ?></strong>
        <span class="badge" style="background:<?= $balances?'#7a9e6e':'#b56b58'; ?>;color:#fff;font-size:.66rem;">
            <?= $balances ? '<i class="fas fa-check me-1"></i>Debits = Credits' : '<i class="fas fa-exclamation-triangle me-1"></i>Out of balance'; ?>
        </span>
    </div>
    <div class="table-responsive">
        <table class="table table-sm mb-0 align-middle">
            <thead style="background:#f5efe6;">
                <tr>
                    <th style="color:#4a3f36;">Code</th>
                    <th style="color:#4a3f36;">Account</th>
                    <th style="color:#4a3f36;">Type</th>
                    <th class="text-end" style="color:#4a3f36;">Debit</th>
                    <th class="text-end" style="color:#4a3f36;">Credit</th>
                    <th class="text-end" style="color:#4a3f36;">Balance</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($rows as $r):
                    $chip = $typeChip[$r['account_type']] ?? ['bg'=>'#e8e0d5','ink'=>'#5a5040'];
                    $bal = (float)$r['balance'];
                    $balColor = $bal >= 0 ? '#4a3f36' : '#b56b58';
                ?>
                    <tr>
                        <td class="text-muted small" style="color:#7a6f63;"><?= htmlspecialchars($r['account_code']); ?></td>
                        <td style="color:#4a3f36;"><?= htmlspecialchars($r['account_name']); ?><?php if ((int)$r['is_bank']): ?> <i class="fas fa-university ms-1" style="color:#c9a875;font-size:.7rem;" title="Bank account"></i><?php endif; ?></td>
                        <td><span class="badge" style="background:<?= $chip['bg']; ?>;color:<?= $chip['ink']; ?>;font-size:.62rem;text-transform:capitalize;"><?= strtolower($r['account_type']); ?></span></td>
                        <td class="text-end" style="color:<?= (float)$r['total_debit']>0?'#4a6b3f':'#a89a8a'; ?>;"><?= (float)$r['total_debit']>0 ? number_format((float)$r['total_debit']) : '—'; ?></td>
                        <td class="text-end" style="color:<?= (float)$r['total_credit']>0?'#8a4a3a':'#a89a8a'; ?>;"><?= (float)$r['total_credit']>0 ? number_format((float)$r['total_credit']) : '—'; ?></td>
                        <td class="text-end" style="font-weight:700;color:<?= $balColor; ?>;"><?= number_format($bal); ?></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($rows)): ?><tr><td colspan="6" class="text-center py-3" style="color:#a89a8a;">No posted activity as at this date.</td></tr><?php endif; ?>
            </tbody>
            <?php if (!empty($rows)): ?>
            <tfoot style="background:#f5efe6;">
                <tr>
                    <td colspan="3" class="text-end" style="font-weight:700;color:#4a3f36;">Totals</td>
                    <td class="text-end" style="font-weight:700;color:#4a6b3f;">TZS <?= number_format($sumD); ?></td>
                    <td class="text-end" style="font-weight:700;color:#8a4a3a;">TZS <?= number_format($sumC); ?></td>
                    <td class="text-end" style="font-weight:700;color:<?= $balances?'#7a9e6e':'#b56b58'; ?>;">
                        <?= $balances ? '0 · balanced' : number_format($sumD - $sumC); ?>
                    </td>
                </tr>
            </tfoot>
            <?php endif; ?>
        </table>
    </div>
</div>

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