<?= $this->extend('layouts/enterprise') ?>

<?= $this->section('content') ?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Reports',
    'workspaceCrumbCurrent' => 'Balance Sheet',
    'workspaceTitle'        => 'Balance Sheet',
    'workspaceDesc'         => 'Assets, liabilities, and equity as of period end, including period net income.',
]) ?>

<?php $kpis = $report['kpis'] ?? []; ?>
<?php if ($kpis !== []): ?>
<section class="enterprise-kpi-grid mb-4">
    <?php foreach ($kpis as $kpi): ?>
        <article class="enterprise-kpi-card">
            <div class="enterprise-kpi-header">
                <span class="enterprise-kpi-label"><?= esc($kpi['label']) ?></span>
                <span class="enterprise-kpi-icon"><i class="fa-solid <?= esc($kpi['icon']) ?>"></i></span>
            </div>
            <div class="enterprise-kpi-value"><?= esc($kpi['value']) ?></div>
            <div class="enterprise-kpi-footer">
                <span class="enterprise-kpi-badge enterprise-kpi-badge--<?= esc($kpi['badgeType']) ?>">
                    <?= esc($kpi['badge']) ?>
                </span>
                <span class="enterprise-kpi-dept"><?= esc($kpi['dept']) ?></span>
            </div>
        </article>
    <?php endforeach; ?>
</section>
<?php endif; ?>

<div class="enterprise-data-panel mb-4">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Filters</h2>
        <p class="enterprise-data-panel__subtitle">
            Balances as of <strong>To</strong> date. Net income uses the From → To period.
        </p>
    </div>
    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('accounting/reports/balance-sheet') ?>" method="get">
            <div class="enterprise-filter-grid">
                <div>
                    <label class="enterprise-field-label">From (earnings period)</label>
                    <input type="date" name="date_from" class="form-control form-control-sm" value="<?= esc($filters['date_from']) ?>">
                </div>
                <div>
                    <label class="enterprise-field-label">To (as of)</label>
                    <input type="date" name="date_to" class="form-control form-control-sm" value="<?= esc($filters['date_to']) ?>">
                </div>
                <div class="enterprise-filter-actions">
                    <button type="submit" class="btn btn-sm btn-primary">Apply Filters</button>
                    <?= view('Modules\Reports\Views\partials\export_buttons', [
                        'actionPath' => 'accounting/reports/balance-sheet',
                        'query'      => $filters,
                    ]) ?>
                </div>
            </div>
        </form>
    </div>
</div>

<?php
$renderSection = static function (string $title, string $subtitle, array $rows, float $total, array $filters, ?string $extraLabel = null, ?float $extraAmount = null, ?float $grandTotal = null): void {
    ?>
    <div class="enterprise-data-panel mb-4">
        <div class="enterprise-data-panel__head">
            <h2 class="enterprise-data-panel__title"><?= esc($title) ?></h2>
            <p class="enterprise-data-panel__subtitle"><?= esc($subtitle) ?></p>
        </div>
        <div class="enterprise-data-panel__body">
            <div class="table-responsive">
                <table class="enterprise-data-table">
                    <thead>
                        <tr>
                            <th>Code</th>
                            <th>Account</th>
                            <th class="text-end">Amount</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($rows as $row): ?>
                            <tr>
                                <td><code><?= esc($row['code']) ?></code></td>
                                <td>
                                    <a href="<?= base_url('accounting/reports/general-ledger?account_id=' . $row['id'] . '&date_from=' . urlencode($filters['date_from']) . '&date_to=' . urlencode($filters['date_to'])) ?>">
                                        <?= esc($row['name']) ?>
                                    </a>
                                </td>
                                <td class="text-end"><?= number_format((float) $row['amount'], 2) ?></td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if ($rows === [] && $extraLabel === null): ?>
                            <tr><td colspan="3" class="text-center text-muted py-4">No balances.</td></tr>
                        <?php endif; ?>
                        <?php if ($extraLabel !== null): ?>
                            <tr>
                                <td></td>
                                <td><?= esc($extraLabel) ?></td>
                                <td class="text-end"><?= number_format((float) $extraAmount, 2) ?></td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                    <tfoot>
                        <tr>
                            <th colspan="2" class="text-end"><?= $grandTotal !== null ? 'Total' : 'Subtotal' ?></th>
                            <th class="text-end"><?= number_format($grandTotal ?? $total, 2) ?></th>
                        </tr>
                    </tfoot>
                </table>
            </div>
        </div>
    </div>
    <?php
};

$renderSection('Assets', 'As of ' . $report['as_of'], $report['assets'] ?? [], (float) $report['total_assets'], $filters);
$renderSection('Liabilities', 'As of ' . $report['as_of'], $report['liabilities'] ?? [], (float) $report['total_liabilities'], $filters);
$renderSection(
    'Equity',
    'Equity accounts plus net income for ' . $filters['date_from'] . ' → ' . $filters['date_to'],
    $report['equity'] ?? [],
    (float) $report['total_equity'],
    $filters,
    'Net Income (Loss) for period',
    (float) $report['net_income'],
    (float) $report['total_equity_with_earnings']
);
?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Balance Check</h2>
        <p class="enterprise-data-panel__subtitle">
            <?= ! empty($report['balanced'])
                ? 'Assets equal liabilities plus equity (including period earnings).'
                : 'Assets do not equal liabilities plus equity — review opening balances and postings.' ?>
        </p>
    </div>
    <div class="enterprise-data-panel__body">
        <div class="table-responsive">
            <table class="enterprise-data-table">
                <tbody>
                    <tr>
                        <td>Total Assets</td>
                        <td class="text-end"><?= number_format((float) $report['total_assets'], 2) ?></td>
                    </tr>
                    <tr>
                        <td>Total Liabilities</td>
                        <td class="text-end"><?= number_format((float) $report['total_liabilities'], 2) ?></td>
                    </tr>
                    <tr>
                        <td>Total Equity (incl. net income)</td>
                        <td class="text-end"><?= number_format((float) $report['total_equity_with_earnings'], 2) ?></td>
                    </tr>
                    <tr>
                        <th>Liabilities + Equity</th>
                        <th class="text-end"><?= number_format((float) $report['total_liabilities_equity'], 2) ?></th>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
