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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Sales & CRM',
    'workspaceCrumbCurrent' => 'Probability',
    'workspaceTitle'        => 'Win Probability',
    'workspaceDesc'         => 'Review and quickly update opportunity probabilities.',
]) ?>

<div class="row g-4 mb-4">
    <div class="col-12">
        <div class="enterprise-data-panel">
            <div class="enterprise-data-panel__head">
                <h2 class="enterprise-data-panel__title">Stage Defaults</h2>
                <p class="enterprise-data-panel__subtitle">Default probability applied when an opportunity is assigned to a stage.</p>
            </div>
            <div class="enterprise-data-panel__body">
                <div class="table-responsive">
                    <table class="enterprise-data-table">
                        <thead>
                            <tr>
                                <th>Stage</th>
                                <th>Code</th>
                                <th>Default Probability</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($stages as $st): ?>
                                <tr>
                                    <td><span class="enterprise-data-table__primary"><?= esc($st['name']) ?></span></td>
                                    <td><code><?= esc($st['code']) ?></code></td>
                                    <td><?= (int) $st['default_probability'] ?>%</td>
                                </tr>
                            <?php endforeach; ?>
                            <?php if (count($stages) === 0): ?>
                                <tr><td colspan="3" class="text-center text-muted py-4">No active sales stages.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Opportunity Probabilities</h2>
        <p class="enterprise-data-panel__subtitle">Update win probability inline for each opportunity.</p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('sale/probability') ?>" method="get">
            <div class="enterprise-filter-grid">
                <div>
                    <label class="enterprise-field-label">Search</label>
                    <input type="text" name="q" class="form-control form-control-sm" placeholder="Name, company, stage"
                           value="<?= esc($filters['search']) ?>">
                </div>
                <div>
                    <label class="enterprise-field-label">Status</label>
                    <select name="status" class="form-select form-select-sm">
                        <option value="">Any</option>
                        <?php foreach (['open', 'won', 'lost', 'abandoned'] as $s): ?>
                            <option value="<?= $s ?>" <?= $filters['status'] === $s ? 'selected' : '' ?>><?= ucfirst($s) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <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 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">
        <?php if (session()->getFlashdata('errors')): ?>
            <div class="alert alert-danger py-2 small mx-3 mt-3">
                <ul class="mb-0 ps-3">
                    <?php foreach ((array) session()->getFlashdata('errors') as $err): ?>
                        <li><?= esc(is_array($err) ? implode(', ', $err) : $err) ?></li>
                    <?php endforeach; ?>
                </ul>
            </div>
        <?php endif; ?>

        <div class="table-responsive">
            <table class="enterprise-data-table">
                <thead>
                    <tr>
                        <th>Opportunity</th>
                        <th>Company</th>
                        <th>Stage</th>
                        <th>Status</th>
                        <th style="min-width:180px;">Probability</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($opportunities as $o): ?>
                        <tr>
                            <td>
                                <a href="<?= base_url('sale/opportunities/' . $o['id']) ?>" class="enterprise-data-table__primary text-decoration-none">
                                    <?= esc($o['name']) ?>
                                </a>
                            </td>
                            <td><?= esc($o['company_name'] ?: '—') ?></td>
                            <td><?= esc($o['stage_name'] ?: '—') ?></td>
                            <td><?= esc(ucfirst($o['status'])) ?></td>
                            <td>
                                <form action="<?= base_url('sale/probability/' . $o['id'] . '/update-probability') ?>" method="post" class="d-flex gap-2 align-items-center">
                                    <?= csrf_field() ?>
                                    <input type="number" name="probability" class="form-control form-control-sm" style="max-width:90px;"
                                           min="0" max="100" value="<?= (int) $o['probability'] ?>" required>
                                    <button type="submit" class="btn btn-sm btn-outline-primary">Save</button>
                                </form>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($opportunities) === 0): ?>
                        <tr><td colspan="5" class="text-center text-muted py-5">No opportunities found.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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