<?php
session_start();
include 'layouts/dbconn.php';

if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

$lead_id = $_GET['id'] ?? '';

if (empty($lead_id)) {
    header('Location: list_leads.php');
    exit();
}

// Get lead details
$sql = "SELECT l.*, u.first_name as agent_first, u.last_name as agent_last
        FROM leads l
        LEFT JOIN users u ON l.assigned_agent_id = u.id
        WHERE l.id = ?";

$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $lead_id);
$stmt->execute();
$result = $stmt->get_result();
$lead = $result->fetch_assoc();

if (!$lead) {
    header('Location: list_leads.php');
    exit();
}
?>
<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Lead Details')); ?>
    <?php include 'layouts/head-css.php'; ?>
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>

        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <?php includeFileWithVariables('layouts/page-title.php', array('pagetitle' => 'CRM', 'title' => 'Lead Details')); ?>

                    <div class="row mb-3">
                        <div class="col-12">
                            <a href="list_leads.php" class="btn btn-light">
                                <i class="ri-arrow-left-line me-1"></i>Back to Leads
                            </a>
                            <a href="add_lead.php?id=<?php echo $lead_id; ?>" class="btn btn-primary ms-2">
                                <i class="ri-edit-line me-1"></i>Edit Lead
                            </a>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-xl-8">
                            <div class="card">
                                <div class="card-header">
                                    <h4 class="card-title mb-0">
                                        <?php echo htmlspecialchars($lead['first_name'] . ' ' . $lead['last_name']); ?>
                                    </h4>
                                </div>
                                <div class="card-body">
                                    <div class="row mb-3">
                                        <div class="col-md-6">
                                            <p class="mb-2"><strong>Email:</strong> <?php echo htmlspecialchars($lead['email'] ?? 'N/A'); ?></p>
                                            <p class="mb-2"><strong>Phone:</strong> <?php echo htmlspecialchars($lead['phone'] ?? 'N/A'); ?></p>
                                            <p class="mb-2"><strong>Interest Type:</strong> 
                                                <span class="badge bg-soft-info"><?php echo htmlspecialchars($lead['interest_type'] ?? 'N/A'); ?></span>
                                            </p>
                                        </div>
                                        <div class="col-md-6">
                                            <p class="mb-2"><strong>Status:</strong> 
                                                <span class="badge bg-soft-primary"><?php echo htmlspecialchars($lead['status']); ?></span>
                                            </p>
                                            <p class="mb-2"><strong>Budget:</strong> 
                                                <?php if ($lead['budget_min'] || $lead['budget_max']): ?>
                                                    KES <?php echo number_format($lead['budget_min'] ?? 0, 0); ?> - 
                                                    KES <?php echo number_format($lead['budget_max'] ?? 0, 0); ?>
                                                <?php else: ?>
                                                    N/A
                                                <?php endif; ?>
                                            </p>
                                            <p class="mb-2"><strong>Assigned Agent:</strong> 
                                                <?php if ($lead['agent_first']): ?>
                                                    <?php echo htmlspecialchars($lead['agent_first'] . ' ' . $lead['agent_last']); ?>
                                                <?php else: ?>
                                                    <span class="text-muted">Unassigned</span>
                                                <?php endif; ?>
                                            </p>
                                        </div>
                                    </div>
                                    <?php if ($lead['notes']): ?>
                                        <div class="mb-3">
                                            <h6 class="text-muted mb-2">Notes</h6>
                                            <p><?php echo nl2br(htmlspecialchars($lead['notes'])); ?></p>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>

    <?php include 'layouts/vendor-scripts.php'; ?>
</body>
</html>
