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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Reports',
    'workspaceCrumbCurrent' => 'Profit & Loss',
    'workspaceTitle'        => 'Profit & Loss',
    'workspaceDesc'         => 'Revenue and expenses 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/profit-and-loss') ?>" 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/profit-and-loss',
                        '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">Revenue</h2>
        <p class="enterprise-data-panel__subtitle">Credit-normal accounts for the period.</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 ($report['revenues'] ?? [] 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 (empty($report['revenues'])): ?>
                        <tr><td colspan="3" class="text-center text-muted py-4">No revenue activity in this period.</td></tr>
                    <?php endif; ?>
                </tbody>
                <?php if (! empty($report['revenues'])): ?>
                    <tfoot>
                        <tr>
                            <th colspan="2" class="text-end">Total Revenue</th>
                            <th class="text-end"><?= number_format((float) $report['total_revenue'], 2) ?></th>
                        </tr>
                    </tfoot>
                <?php endif; ?>
            </table>
        </div>
    </div>
</div>

<div class="enterprise-data-panel mb-4">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Expenses</h2>
        <p class="enterprise-data-panel__subtitle">Debit-normal accounts for the period.</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 ($report['expenses'] ?? [] 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 (empty($report['expenses'])): ?>
                        <tr><td colspan="3" class="text-center text-muted py-4">No expense activity in this period.</td></tr>
                    <?php endif; ?>
                </tbody>
                <?php if (! empty($report['expenses'])): ?>
                    <tfoot>
                        <tr>
                            <th colspan="2" class="text-end">Total Expenses</th>
                            <th class="text-end"><?= number_format((float) $report['total_expense'], 2) ?></th>
                        </tr>
                    </tfoot>
                <?php endif; ?>
            </table>
        </div>
    </div>
</div>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Net Income</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>Total Revenue</td>
                        <td class="text-end"><?= number_format((float) $report['total_revenue'], 2) ?></td>
                    </tr>
                    <tr>
                        <td>Total Expenses</td>
                        <td class="text-end"><?= number_format((float) $report['total_expense'], 2) ?></td>
                    </tr>
                    <tr>
                        <th>Net <?= (float) $report['net_income'] >= 0 ? 'Income' : 'Loss' ?></th>
                        <th class="text-end"><?= number_format((float) $report['net_income'], 2) ?></th>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

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