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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Reports',
    'workspaceCrumbCurrent' => 'Cash Flow',
    'workspaceTitle'        => 'Cash Flow',
    'workspaceDesc'         => 'Cash movements on bank and cash ledger accounts, classified by counterpart type.',
]) ?>

<?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; ?>

<?php if (! empty($report['warning'])): ?>
    <div class="alert alert-warning mb-4"><?= esc($report['warning']) ?></div>
<?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">Uses CoA accounts linked from Banking → Bank Accounts and Cash Accounts.</p>
    </div>
    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('accounting/reports/cash-flow') ?>" 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 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/cash-flow',
                        'query'      => $filters,
                    ]) ?>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="enterprise-data-panel mb-4">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Cash Position</h2>
        <p class="enterprise-data-panel__subtitle"><?= esc($filters['date_from']) ?> → <?= esc($filters['date_to']) ?></p>
    </div>
    <div class="enterprise-data-panel__body">
        <div class="table-responsive">
            <table class="enterprise-data-table">
                <tbody>
                    <tr>
                        <td>Opening cash</td>
                        <td class="text-end"><?= number_format((float) $report['opening_cash'], 2) ?></td>
                    </tr>
                    <tr>
                        <td>Operating activities</td>
                        <td class="text-end"><?= number_format((float) $report['operating']['net'], 2) ?></td>
                    </tr>
                    <tr>
                        <td>Investing activities</td>
                        <td class="text-end"><?= number_format((float) $report['investing']['net'], 2) ?></td>
                    </tr>
                    <tr>
                        <td>Financing activities</td>
                        <td class="text-end"><?= number_format((float) $report['financing']['net'], 2) ?></td>
                    </tr>
                    <?php if ((float) ($report['transfers']['net'] ?? 0) != 0.0): ?>
                        <tr>
                            <td>Internal transfers / other</td>
                            <td class="text-end"><?= number_format((float) $report['transfers']['net'], 2) ?></td>
                        </tr>
                    <?php endif; ?>
                    <?php if ((float) ($report['unclassified']['net'] ?? 0) != 0.0): ?>
                        <tr>
                            <td>Unclassified</td>
                            <td class="text-end"><?= number_format((float) $report['unclassified']['net'], 2) ?></td>
                        </tr>
                    <?php endif; ?>
                    <tr>
                        <th>Net change in cash</th>
                        <th class="text-end"><?= number_format((float) $report['net_change'], 2) ?></th>
                    </tr>
                    <tr>
                        <th>Closing cash</th>
                        <th class="text-end"><?= number_format((float) $report['closing_cash'], 2) ?></th>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php
$sections = [
    'operating' => ['title' => 'Operating Activities', 'data' => $report['operating'] ?? []],
    'investing' => ['title' => 'Investing Activities', 'data' => $report['investing'] ?? []],
    'financing' => ['title' => 'Financing Activities', 'data' => $report['financing'] ?? []],
];
foreach ($sections as $section):
    $data = $section['data'];
    $rows = $data['rows'] ?? [];
    ?>
    <div class="enterprise-data-panel mb-4">
        <div class="enterprise-data-panel__head">
            <h2 class="enterprise-data-panel__title"><?= esc($section['title']) ?></h2>
            <p class="enterprise-data-panel__subtitle">
                In <?= number_format((float) ($data['inflows'] ?? 0), 2) ?>
                · Out <?= number_format((float) ($data['outflows'] ?? 0), 2) ?>
                · Net <?= number_format((float) ($data['net'] ?? 0), 2) ?>
            </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>Counterpart</th>
                            <th>Description</th>
                            <th class="text-end">Cash Impact</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($rows as $row): ?>
                            <tr>
                                <td><?= esc($row['entry_date']) ?></td>
                                <td>
                                    <a href="<?= base_url('accounting/journal-entries/' . $row['journal_id']) ?>">
                                        <code><?= esc($row['number']) ?></code>
                                    </a>
                                </td>
                                <td><?= esc($row['counterpart']) ?></td>
                                <td><?= esc($row['description'] ?: ($row['reference'] ?: '—')) ?></td>
                                <td class="text-end"><?= number_format((float) $row['amount'], 2) ?></td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if ($rows === []): ?>
                            <tr><td colspan="5" class="text-center text-muted py-4">No movements in this section.</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
<?php endforeach; ?>

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