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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Reports',
    'workspaceCrumbCurrent' => 'Revenue Forecast',
    'workspaceTitle'        => 'Revenue Forecast',
    'workspaceDesc'         => 'Weighted pipeline by close date and stage, plus high-probability commit.',
]) ?>

<?= view('Modules\Sale\Views\reports\_kpis', ['kpis' => $report['kpis'] ?? []]) ?>

<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">Forecast window uses expected close date (open opportunities).</p>
    </div>
    <?= view('Modules\Sale\Views\reports\_filters', [
        'actionPath'  => 'sale/reports/revenue-forecast',
        'filters'     => $filters,
        'companies'   => $companies,
        'showCompany' => true,
    ]) ?>
</div>

<section class="enterprise-charts-grid mb-4">
    <article class="enterprise-chart-card">
        <div class="enterprise-chart-header">
            <h3 class="enterprise-chart-title">WEIGHTED BY CLOSE MONTH</h3>
        </div>
        <div class="enterprise-chart-body">
            <canvas id="forecastMonthChart" height="220"></canvas>
        </div>
    </article>
    <article class="enterprise-chart-card">
        <div class="enterprise-chart-header">
            <h3 class="enterprise-chart-title">WEIGHTED BY STAGE</h3>
        </div>
        <div class="enterprise-chart-body">
            <canvas id="forecastStageChart" height="220"></canvas>
        </div>
    </article>
</section>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Open Pipeline Detail</h2>
        <p class="enterprise-data-panel__subtitle">Weighted revenue = expected revenue × probability ÷ 100.</p>
    </div>
    <div class="enterprise-data-panel__body">
        <div class="table-responsive">
            <table class="enterprise-data-table">
                <thead>
                    <tr>
                        <th>Opportunity</th>
                        <th>Company</th>
                        <th>Stage</th>
                        <th class="text-end">Expected</th>
                        <th class="text-end">Probability</th>
                        <th class="text-end">Weighted</th>
                        <th>Close Date</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $sumExpected = 0.0;
                    $sumWeighted = 0.0;
                    foreach ($report['rows'] ?? [] as $row):
                        $sumExpected += (float) $row['expected_revenue'];
                        $sumWeighted += (float) $row['weighted_revenue'];
                    ?>
                        <tr>
                            <td>
                                <a href="<?= base_url('sale/opportunities/' . $row['id']) ?>" class="enterprise-data-table__primary text-decoration-none">
                                    <?= esc($row['name']) ?>
                                </a>
                            </td>
                            <td><?= esc($row['company_name'] ?: '—') ?></td>
                            <td><?= esc($row['stage_name'] ?: '—') ?></td>
                            <td class="text-end"><?= number_format((float) $row['expected_revenue'], 2) ?></td>
                            <td class="text-end"><?= (int) $row['probability'] ?>%</td>
                            <td class="text-end fw-semibold"><?= number_format((float) $row['weighted_revenue'], 2) ?></td>
                            <td><?= $row['expected_close_date'] ? esc($row['expected_close_date']) : '—' ?></td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (empty($report['rows'])): ?>
                        <tr><td colspan="7" class="text-center text-muted py-5">No open opportunities in this forecast window.</td></tr>
                    <?php else: ?>
                        <tr class="fw-semibold">
                            <td colspan="3" class="text-end">Totals</td>
                            <td class="text-end"><?= number_format($sumExpected, 2) ?></td>
                            <td></td>
                            <td class="text-end"><?= number_format($sumWeighted, 2) ?></td>
                            <td></td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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

<?= $this->section('scripts') ?>
<script>
window.saleReportCharts = <?= json_encode($report['charts'] ?? [], JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP) ?>;
</script>
<script src="<?= base_url('assets/js/sale-reports.js') ?>"></script>
<?= $this->endSection() ?>
