<?php $this->start('body');
    $asAt = $this->v('as_at');
    $bs   = $this->data['bs'] ?? [];
    $assets    = $bs['assets']              ?? [];
    $liabs     = $bs['liabilities']         ?? [];
    $equity    = $bs['equity']              ?? [];
    $retained  = (float)($bs['retained_earnings'] ?? 0);
    $totalA    = (float)($bs['total_assets']      ?? 0);
    $totalL    = (float)($bs['total_liabilities'] ?? 0);
    $totalECap = (float)($bs['total_equity_capital']?? 0);
    $totalE    = (float)($bs['total_equity_all']    ?? 0);
    $totalLE   = (float)($bs['total_liab_and_eq']   ?? 0);
    $balances  = !empty($bs['balances']);

    $renderRow = function (array $r): string {
        return '<tr><td class="text-muted small" style="color:#7a6f63;">' . htmlspecialchars($r['account_code']) .
               '</td><td style="color:#4a3f36;">' . htmlspecialchars($r['account_name']) .
               '</td><td class="text-end" style="font-weight:500;color:#4a3f36;">' . number_format((float)$r['balance']) . '</td></tr>';
    };
?>

<?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-columns me-2" style="color:#7a9e6e;"></i>Balance Sheet</h5>
    <div class="btn-group">
        <a href="<?= PROOT; ?>FinancialStatements/balanceSheetXlsx?<?= $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/balanceSheetPdf?<?= $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/balanceSheet" 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:#7a9e6e;border-color:#7a9e6e;color:#fff;font-weight:600;"><i class="fas fa-filter me-1"></i>Refresh</button>
            </div>
        </div>
    </div>
</form>

<?php if (!$balances): ?>
<div class="alert py-2 mb-3" style="background:#e8d5cf;border:1px solid #b56b58;color:#7a3a2c;">
    <i class="fas fa-exclamation-triangle me-1"></i>
    <strong>Books out of balance:</strong> Assets <?= number_format($totalA); ?> ≠ Liabilities + Equity <?= number_format($totalLE); ?>. Difference TZS <?= number_format(abs($totalA - $totalLE)); ?>. Check unposted JEs or one-sided entries.
</div>
<?php endif; ?>

<div class="row g-3">
    <!-- ── Assets ─────────────────────────────────────────────────────────── -->
    <div class="col-md-6">
        <div class="card border-0 shadow-sm h-100" style="border-top:3px solid #c9a875 !important;">
            <div class="card-header bg-white" style="border-bottom:1px solid #eae0d0;">
                <strong style="color:#4a3f36;"><i class="fas fa-hand-holding-usd me-2" style="color:#c9a875;"></i>Assets</strong>
            </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 class="text-end" style="color:#4a3f36;">Balance</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($assets as $r) echo $renderRow($r); ?>
                        <?php if (empty($assets)): ?><tr><td colspan="3" class="text-center py-3" style="color:#a89a8a;">No asset balances.</td></tr><?php endif; ?>
                    </tbody>
                    <tfoot style="background:#f5efe6;">
                        <tr>
                            <td colspan="2" class="text-end" style="font-weight:700;color:#4a3f36;">Total Assets</td>
                            <td class="text-end" style="font-weight:700;color:#7a6039;">TZS <?= number_format($totalA); ?></td>
                        </tr>
                    </tfoot>
                </table>
            </div>
        </div>
    </div>

    <!-- ── Liabilities + Equity ───────────────────────────────────────────── -->
    <div class="col-md-6">
        <div class="card border-0 shadow-sm h-100" style="border-top:3px solid #7a9e6e !important;">
            <div class="card-header bg-white" style="border-bottom:1px solid #eae0d0;">
                <strong style="color:#4a3f36;"><i class="fas fa-file-invoice me-2" style="color:#b56b58;"></i>Liabilities &amp; Equity</strong>
            </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 class="text-end" style="color:#4a3f36;">Balance</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr style="background:#faf6f0;"><td colspan="3" style="font-weight:600;color:#8a4a3a;text-transform:capitalize;font-size:.78rem;letter-spacing:.3px;">Liabilities</td></tr>
                        <?php foreach ($liabs as $r) echo $renderRow($r); ?>
                        <?php if (empty($liabs)): ?><tr><td colspan="3" class="text-center py-2" style="color:#a89a8a;">No liability balances.</td></tr><?php endif; ?>
                        <tr style="background:#f5efe6;"><td colspan="2" class="text-end" style="font-weight:600;color:#4a3f36;">Sub-total &middot; Liabilities</td><td class="text-end" style="font-weight:600;color:#8a4a3a;"><?= number_format($totalL); ?></td></tr>

                        <tr style="background:#faf6f0;"><td colspan="3" style="font-weight:600;color:#4f6b3f;text-transform:capitalize;font-size:.78rem;letter-spacing:.3px;">Equity</td></tr>
                        <?php foreach ($equity as $r) echo $renderRow($r); ?>
                        <tr><td class="text-muted small" style="color:#7a6f63;">3900</td><td style="color:#4a3f36;font-style:italic;">Retained earnings (YTD)</td><td class="text-end" style="font-weight:500;color:<?= $retained>=0?'#4a3f36':'#b56b58'; ?>;"><?= number_format($retained); ?></td></tr>
                        <tr style="background:#f5efe6;"><td colspan="2" class="text-end" style="font-weight:600;color:#4a3f36;">Sub-total &middot; Equity</td><td class="text-end" style="font-weight:600;color:#4f6b3f;"><?= number_format($totalE); ?></td></tr>
                    </tbody>
                    <tfoot style="background:#f5efe6;">
                        <tr>
                            <td colspan="2" class="text-end" style="font-weight:700;color:#4a3f36;">Total Liab. + Equity</td>
                            <td class="text-end" style="font-weight:700;color:<?= $balances?'#4f6b3f':'#8a4a3a'; ?>;">TZS <?= number_format($totalLE); ?></td>
                        </tr>
                    </tfoot>
                </table>
            </div>
        </div>
    </div>
</div>

<div class="mt-3 text-end" style="color:#7a6f63;font-size:.78rem;">
    Retained earnings shown as running YTD figure (no year-end close applied yet).
</div>

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