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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Sales & CRM',
    'workspaceCrumbCurrent' => 'Expected Revenue',
    'workspaceTitle'        => 'Expected Revenue',
    'workspaceDesc'         => 'Weighted pipeline value from open opportunities.',
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Open Pipeline Revenue</h2>
        <p class="enterprise-data-panel__subtitle">Weighted revenue = expected revenue × probability ÷ 100.</p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('sale/expected-revenue') ?>" method="get">
            <div class="enterprise-filter-grid">
                <div>
                    <label class="enterprise-field-label">Stage</label>
                    <select name="stage_id" class="form-select form-select-sm">
                        <option value="">Any</option>
                        <?php foreach ($stages as $st): ?>
                            <option value="<?= $st['id'] ?>" <?= (string) $filters['stageId'] === (string) $st['id'] ? 'selected' : '' ?>><?= esc($st['name']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label class="enterprise-field-label">Company</label>
                    <select name="company_id" class="form-select form-select-sm">
                        <option value="">Any</option>
                        <?php foreach ($companies as $c): ?>
                            <option value="<?= $c['id'] ?>" <?= (string) $filters['companyId'] === (string) $c['id'] ? 'selected' : '' ?>><?= esc($c['name']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="enterprise-filter-actions">
                    <button type="submit" class="btn btn-sm btn-primary">Apply Filters</button>
                </div>
            </div>
        </form>
    </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 Revenue</th>
                        <th class="text-end">Probability</th>
                        <th class="text-end">Weighted Revenue</th>
                        <th>Close Date</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($rows as $row): ?>
                        <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 (count($rows) === 0): ?>
                        <tr><td colspan="7" class="text-center text-muted py-5">No open opportunities match the filters.</td></tr>
                    <?php else: ?>
                        <tr class="fw-semibold">
                            <td colspan="3" class="text-end">Totals</td>
                            <td class="text-end"><?= number_format((float) $totalExpected, 2) ?></td>
                            <td></td>
                            <td class="text-end"><?= number_format((float) $totalWeighted, 2) ?></td>
                            <td></td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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