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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Reports',
    'workspaceCrumbCurrent' => 'Trial Balance',
    'workspaceTitle'        => 'Trial Balance',
    'workspaceDesc'         => 'Net balances from posted journals for the selected period.',
]) ?>

<?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">Only posted journal entries are included.</p>
    </div>
    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('accounting/reports/trial-balance') ?>" method="get">
            <div class="enterprise-filter-grid">
                <div>
                    <label class="enterprise-field-label">From</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</label>
                    <input type="date" name="date_to" class="form-control form-control-sm" value="<?= esc($filters['date_to']) ?>">
                </div>
                <div>
                    <label class="enterprise-field-label">Account Type</label>
                    <select name="type" class="form-select form-select-sm">
                        <option value="">Any</option>
                        <?php foreach (['asset', 'liability', 'equity', 'revenue', 'expense'] as $t): ?>
                            <option value="<?= $t ?>" <?= ($filters['type'] ?? '') === $t ? 'selected' : '' ?>><?= ucfirst($t) ?></option>
                        <?php endforeach; ?>
                    </select>
                </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/trial-balance',
                        'query'      => $filters,
                    ]) ?>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Balances</h2>
        <p class="enterprise-data-panel__subtitle">
            <?= ! empty($report['balanced']) ? 'Trial balance is in balance.' : 'Debit and credit totals do not match.' ?>
        </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>Type</th>
                        <th class="text-end">Debit</th>
                        <th class="text-end">Credit</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($report['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><?= esc(ucfirst($row['type'])) ?></td>
                            <td class="text-end"><?= $row['debit'] > 0 ? number_format($row['debit'], 2) : '—' ?></td>
                            <td class="text-end"><?= $row['credit'] > 0 ? number_format($row['credit'], 2) : '—' ?></td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (empty($report['rows'])): ?>
                        <tr><td colspan="5" class="text-center text-muted py-5">No posted activity in this period.</td></tr>
                    <?php endif; ?>
                </tbody>
                <?php if (! empty($report['rows'])): ?>
                    <tfoot>
                        <tr>
                            <th colspan="3" class="text-end">Totals</th>
                            <th class="text-end"><?= number_format((float) $report['total_debit'], 2) ?></th>
                            <th class="text-end"><?= number_format((float) $report['total_credit'], 2) ?></th>
                        </tr>
                    </tfoot>
                <?php endif; ?>
            </table>
        </div>
    </div>
</div>

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