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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Reports',
    'workspaceCrumbCurrent' => 'General Ledger',
    'workspaceTitle'        => 'General Ledger',
    'workspaceDesc'         => 'Posted journal activity with running balances by account.',
]) ?>

<?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">Filter by date range and optional account.</p>
    </div>
    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('accounting/reports/general-ledger') ?>" 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</label>
                    <select name="account_id" class="form-select form-select-sm">
                        <option value="">All accounts</option>
                        <?php foreach ($accounts as $a): ?>
                            <option value="<?= $a['id'] ?>" <?= (string) ($filters['account_id'] ?? '') === (string) $a['id'] ? 'selected' : '' ?>>
                                <?= esc($a['code'] . ' — ' . $a['name']) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label class="enterprise-field-label">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/general-ledger',
                        'query'      => $filters,
                    ]) ?>
                </div>
            </div>
        </form>
    </div>
</div>

<?php if (! empty($filters['account_id']) && isset($report['opening']['balance'])): ?>
    <div class="alert alert-light border mb-4 py-2 small">
        Opening balance before <?= esc($filters['date_from']) ?>:
        <strong><?= number_format((float) $report['opening']['balance'], 2) ?></strong>
    </div>
<?php endif; ?>

<?php if (empty($report['accounts'])): ?>
    <div class="enterprise-data-panel">
        <div class="enterprise-data-panel__body text-center text-muted py-5">No posted ledger activity for these filters.</div>
    </div>
<?php else: ?>
    <?php foreach ($report['accounts'] as $account): ?>
        <div class="enterprise-data-panel mb-4">
            <div class="enterprise-data-panel__head">
                <h2 class="enterprise-data-panel__title">
                    <code><?= esc($account['account_code']) ?></code>
                    <?= esc($account['account_name']) ?>
                </h2>
                <p class="enterprise-data-panel__subtitle"><?= esc(ucfirst($account['account_type'])) ?></p>
            </div>
            <div class="enterprise-data-panel__body">
                <div class="table-responsive">
                    <table class="enterprise-data-table">
                        <thead>
                            <tr>
                                <th>Date</th>
                                <th>Journal</th>
                                <th>Reference</th>
                                <th>Description</th>
                                <th class="text-end">Debit</th>
                                <th class="text-end">Credit</th>
                                <th class="text-end">Balance</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($account['lines'] as $line): ?>
                                <tr>
                                    <td><?= esc($line['entry_date']) ?></td>
                                    <td><a href="<?= base_url('accounting/journal-entries/' . $line['journal_id']) ?>"><code><?= esc($line['number']) ?></code></a></td>
                                    <td><?= esc($line['reference'] ?: '—') ?></td>
                                    <td><?= esc($line['description'] ?: '—') ?></td>
                                    <td class="text-end"><?= $line['debit'] > 0 ? number_format($line['debit'], 2) : '—' ?></td>
                                    <td class="text-end"><?= $line['credit'] > 0 ? number_format($line['credit'], 2) : '—' ?></td>
                                    <td class="text-end"><?= number_format($line['balance'], 2) ?></td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    <?php endforeach; ?>
<?php endif; ?>

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