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

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

<?php
$statusPill = static function (string $status): string {
    $map = [
        'open'      => 'active',
        'won'       => 'active',
        'lost'      => 'inactive',
        'abandoned' => 'inactive',
    ];
    $class = $map[$status] ?? 'inactive';
    return '<span class="enterprise-status-pill enterprise-status-pill--' . esc($class) . '">' . esc(ucfirst($status)) . '</span>';
};

$weighted = (float) $opportunity['expected_revenue'] * (int) $opportunity['probability'] / 100;
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Sales & CRM',
    'workspaceCrumbCurrent' => 'Opportunity #' . $opportunity['id'],
    'workspaceTitle'        => esc($opportunity['name']),
    'workspaceDesc'         => 'Opportunity details and pipeline status.',
]) ?>

<div class="row g-4">
    <div class="col-lg-8">
        <article class="enterprise-settings-card">
            <div class="enterprise-settings-card__head">
                <i class="fa-solid fa-handshake"></i>
                <h2 class="enterprise-settings-card__title">Opportunity Information</h2>
            </div>
            <dl class="row mb-0 small">
                <dt class="col-sm-4 text-muted">Name</dt>
                <dd class="col-sm-8 fw-semibold"><?= esc($opportunity['name']) ?></dd>
                <dt class="col-sm-4 text-muted">Company</dt>
                <dd class="col-sm-8">
                    <?php if ($company): ?>
                        <a href="<?= base_url('sale/companies/' . $company['id']) ?>"><?= esc($company['name']) ?></a>
                    <?php else: ?>
                        —
                    <?php endif; ?>
                </dd>
                <dt class="col-sm-4 text-muted">Sales Stage</dt>
                <dd class="col-sm-8"><?= esc($stage['name'] ?? '—') ?></dd>
                <dt class="col-sm-4 text-muted">Competitor</dt>
                <dd class="col-sm-8"><?= esc($competitor['name'] ?? '—') ?></dd>
                <dt class="col-sm-4 text-muted">Expected Revenue</dt>
                <dd class="col-sm-8"><?= number_format((float) $opportunity['expected_revenue'], 2) ?></dd>
                <dt class="col-sm-4 text-muted">Probability</dt>
                <dd class="col-sm-8"><?= (int) $opportunity['probability'] ?>%</dd>
                <dt class="col-sm-4 text-muted">Weighted Revenue</dt>
                <dd class="col-sm-8"><?= number_format($weighted, 2) ?></dd>
                <dt class="col-sm-4 text-muted">Expected Close</dt>
                <dd class="col-sm-8"><?= $opportunity['expected_close_date'] ? esc($opportunity['expected_close_date']) : '—' ?></dd>
                <dt class="col-sm-4 text-muted">Status</dt>
                <dd class="col-sm-8"><?= $statusPill($opportunity['status']) ?></dd>
                <dt class="col-sm-4 text-muted">Description</dt>
                <dd class="col-sm-8"><?= esc($opportunity['description'] ?: '—') ?></dd>
            </dl>
        </article>
    </div>

    <div class="col-lg-4">
        <article class="enterprise-settings-card">
            <div class="enterprise-settings-card__head">
                <i class="fa-solid fa-bolt"></i>
                <h2 class="enterprise-settings-card__title">Actions</h2>
            </div>
            <div class="d-grid gap-2">
                <a href="<?= base_url('sale/opportunities/' . $opportunity['id'] . '/edit') ?>" class="btn btn-outline-primary btn-sm">
                    <i class="fa-solid fa-pen me-1"></i> Edit Opportunity
                </a>
                <a href="<?= base_url('sale/opportunities') ?>" class="btn btn-outline-secondary btn-sm">Back to Pipeline</a>
                <form action="<?= base_url('sale/opportunities/' . $opportunity['id'] . '/delete') ?>" method="post" onsubmit="return confirm('Delete this opportunity?');">
                    <?= csrf_field() ?>
                    <button type="submit" class="btn btn-outline-danger btn-sm w-100">
                        <i class="fa-solid fa-trash me-1"></i> Delete
                    </button>
                </form>
            </div>
        </article>
    </div>
</div>

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