<?php
require_once __DIR__ . '/auth-check.php';
require_once __DIR__ . '/../includes/functions.php';

$leads = getData('leads');
$leadId = $_GET['id'] ?? '';
$lead = null;

foreach ($leads as $l) {
    if ($l['id'] == $leadId) {
        $lead = $l;
        break;
    }
}

if (!$lead) {
    setFlash('error', 'Lead not found');
    header('Location: leads.php');
    exit;
}

// Handle status + notes update
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    foreach ($leads as &$l) {
        if ($l['id'] == $leadId) {
            $l['status'] = $_POST['status'] ?? $l['status'];
            $l['notes'] = $_POST['notes'] ?? $l['notes'];
            break;
        }
    }
    saveData('leads', $leads);
    setFlash('success', 'Lead updated successfully');
    header('Location: lead-detail.php?id=' . $leadId);
    exit;
}

$pageTitle = 'Lead #' . $leadId;
include __DIR__ . '/sidebar.php';

$statusColors = [
    'New' => 'badge-new',
    'Contacted' => 'badge-contacted',
    'Replied' => 'badge-replied',
    'Closed' => 'badge-closed',
];
$statusClass = $statusColors[$lead['status'] ?? 'New'] ?? 'badge-new';
?>

<div class="admin-header">
    <h1 class="admin-title">Lead #<?php echo e($lead['id']); ?></h1>
    <a href="leads.php" class="btn-admin-outline" style="font-size:0.8rem;">← Back to Enquiries</a>
</div>

<div class="row g-4">
    <div class="col-lg-8">
        <div class="admin-card">
            <h3 style="font-family:var(--font-heading);font-size:0.9rem;text-transform:uppercase;color:var(--text);margin-bottom:1rem;">Lead Details</h3>
            <div class="row g-3">
                <?php if (($lead['form_type'] ?? '') === 'register'): ?>
                <div class="col-md-6"><strong style="color:var(--muted);font-size:0.78rem;text-transform:uppercase;">Company</strong><br><?php echo e($lead['company_name'] ?? '-'); ?></div>
                <div class="col-md-6"><strong style="color:var(--muted);font-size:0.78rem;text-transform:uppercase;">Contact</strong><br><?php echo e(($lead['first_name'] ?? '') . ' ' . ($lead['last_name'] ?? '')); ?></div>
                <div class="col-md-6"><strong style="color:var(--muted);font-size:0.78rem;text-transform:uppercase;">Stand Type</strong><br><?php echo e($lead['stand_type'] ?? '-'); ?></div>
                <div class="col-md-6"><strong style="color:var(--muted);font-size:0.78rem;text-transform:uppercase;">Stand Size</strong><br><?php echo e($lead['stand_size'] ?? '-'); ?></div>
                <div class="col-md-6"><strong style="color:var(--muted);font-size:0.78rem;text-transform:uppercase;">Sectors</strong><br><?php echo e($lead['sectors'] ?? '-'); ?></div>
                <div class="col-md-6"><strong style="color:var(--muted);font-size:0.78rem;text-transform:uppercase;">Website</strong><br><?php echo !empty($lead['website']) ? '<a href="'.e($lead['website']).'" target="_blank" style="color:var(--green2);">'.e($lead['website']).'</a>' : '-'; ?></div>
                <?php else: ?>
                <div class="col-md-6"><strong style="color:var(--muted);font-size:0.78rem;text-transform:uppercase;">Name</strong><br><?php echo e($lead['name'] ?? '-'); ?></div>
                <div class="col-md-6"><strong style="color:var(--muted);font-size:0.78rem;text-transform:uppercase;">Company</strong><br><?php echo e($lead['company'] ?? '-'); ?></div>
                <?php endif; ?>
                <div class="col-md-6"><strong style="color:var(--muted);font-size:0.78rem;text-transform:uppercase;">Email</strong><br><a href="mailto:<?php echo e($lead['email'] ?? ''); ?>" style="color:var(--green2);"><?php echo e($lead['email'] ?? '-'); ?></a></div>
                <div class="col-md-6"><strong style="color:var(--muted);font-size:0.78rem;text-transform:uppercase;">Phone</strong><br><a href="tel:<?php echo e(preg_replace('/[^0-9+]/','',$lead['phone'] ?? '')); ?>" style="color:var(--green2);"><?php echo e($lead['phone'] ?? '-'); ?></a></div>
                <div class="col-md-6"><strong style="color:var(--muted);font-size:0.78rem;text-transform:uppercase;">Country</strong><br><?php echo e($lead['country'] ?? '-'); ?></div>
                <div class="col-md-6"><strong style="color:var(--muted);font-size:0.78rem;text-transform:uppercase;">Date</strong><br><?php echo e($lead['date'] ?? '-'); ?></div>
                <?php if (!empty($lead['message'])): ?>
                <div class="col-12">
                    <strong style="color:var(--muted);font-size:0.78rem;text-transform:uppercase;">Message</strong>
                    <div style="background:var(--bg2);border-radius:4px;padding:1rem;margin-top:0.5rem;font-size:0.88rem;"><?php echo nl2br(e($lead['message'])); ?></div>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
    <div class="col-lg-4">
        <div class="admin-card">
            <h3 style="font-family:var(--font-heading);font-size:0.9rem;text-transform:uppercase;color:var(--text);margin-bottom:1rem;">Update Status</h3>
            <form method="POST">
                <div class="mb-3">
                    <label style="font-size:0.75rem;color:var(--muted);text-transform:uppercase;">Status</label>
                    <select name="status" class="form-select" style="background:var(--bg2);color:var(--text);border-color:var(--border);">
                        <option value="New" <?php echo ($lead['status'] ?? '') === 'New' ? 'selected' : ''; ?>>New</option>
                        <option value="Contacted" <?php echo ($lead['status'] ?? '') === 'Contacted' ? 'selected' : ''; ?>>Contacted</option>
                        <option value="Replied" <?php echo ($lead['status'] ?? '') === 'Replied' ? 'selected' : ''; ?>>Replied</option>
                        <option value="Closed" <?php echo ($lead['status'] ?? '') === 'Closed' ? 'selected' : ''; ?>>Closed</option>
                    </select>
                </div>
                <div class="mb-3">
                    <label style="font-size:0.75rem;color:var(--muted);text-transform:uppercase;">Notes</label>
                    <textarea name="notes" class="form-control" rows="5" style="background:var(--bg2);color:var(--text);border-color:var(--border);font-size:0.85rem;" placeholder="Add your notes here..."><?php echo e($lead['notes'] ?? ''); ?></textarea>
                </div>
                <button type="submit" class="btn-admin-primary w-100">Save Changes</button>
            </form>
        </div>
    </div>
</div>

<?php include __DIR__ . '/../includes/footer.php'; ?>
