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

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

<?php
$statusPill = static function (string $status): string {
    $map = [
        'new'         => 'on_leave',
        'contacted'   => 'active',
        'qualified'   => 'active',
        'unqualified' => 'inactive',
        'converted'   => 'active',
    ];
    $class = $map[$status] ?? 'inactive';
    return '<span class="enterprise-status-pill enterprise-status-pill--' . esc($class) . '">' . esc(ucfirst($status)) . '</span>';
};
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Sales & CRM',
    'workspaceCrumbCurrent' => 'Lead #' . $lead['id'],
    'workspaceTitle'        => esc(trim($lead['first_name'] . ' ' . $lead['last_name'])),
    'workspaceDesc'         => 'Lead profile, score, and conversion actions.',
]) ?>

<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-address-card"></i>
                <h2 class="enterprise-settings-card__title">Contact 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(trim($lead['first_name'] . ' ' . $lead['last_name'])) ?></dd>
                <dt class="col-sm-4 text-muted">Company</dt>
                <dd class="col-sm-8"><?= esc($lead['company_name'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Job Title</dt>
                <dd class="col-sm-8"><?= esc($lead['job_title'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Email</dt>
                <dd class="col-sm-8"><?= esc($lead['email'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Phone</dt>
                <dd class="col-sm-8"><?= esc($lead['phone'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Source</dt>
                <dd class="col-sm-8"><?= esc($source['name'] ?? '—') ?></dd>
                <dt class="col-sm-4 text-muted">Assigned To</dt>
                <dd class="col-sm-8"><?= esc($assignee ? $assignee->getFullName() : 'Unassigned') ?></dd>
                <dt class="col-sm-4 text-muted">Score</dt>
                <dd class="col-sm-8"><span class="badge bg-primary-subtle text-primary fs-6"><?= (int) $lead['score'] ?></span></dd>
                <dt class="col-sm-4 text-muted">Priority</dt>
                <dd class="col-sm-8"><?= esc(ucfirst($lead['priority'])) ?></dd>
                <dt class="col-sm-4 text-muted">Status</dt>
                <dd class="col-sm-8"><?= $statusPill($lead['status']) ?></dd>
                <dt class="col-sm-4 text-muted">Estimated Value</dt>
                <dd class="col-sm-8"><?= $lead['estimated_value'] !== null ? number_format((float) $lead['estimated_value'], 2) : '—' ?></dd>
                <dt class="col-sm-4 text-muted">Notes</dt>
                <dd class="col-sm-8"><?= esc($lead['notes'] ?: '—') ?></dd>
                <?php if ($lead['converted_at']): ?>
                    <dt class="col-sm-4 text-muted">Converted At</dt>
                    <dd class="col-sm-8"><?= esc($lead['converted_at']) ?></dd>
                <?php endif; ?>
            </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">
                <?php if ($lead['status'] !== 'converted'): ?>
                    <a href="<?= base_url('sale/leads/' . $lead['id'] . '/edit') ?>" class="btn btn-outline-primary btn-sm">
                        <i class="fa-solid fa-pen me-1"></i> Edit Lead
                    </a>
                    <a href="<?= base_url('sale/lead-conversion') ?>" class="btn btn-primary btn-sm">
                        <i class="fa-solid fa-arrow-right-arrow-left me-1"></i> Convert Lead
                    </a>
                <?php endif; ?>
                <a href="<?= base_url('sale/leads') ?>" class="btn btn-outline-secondary btn-sm">Back to Leads</a>
                <?php if ($lead['status'] !== 'converted'): ?>
                    <form action="<?= base_url('sale/leads/' . $lead['id'] . '/delete') ?>" method="post" onsubmit="return confirm('Delete this lead?');">
                        <?= 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>
                <?php endif; ?>
            </div>
        </article>
    </div>
</div>

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