<?php

session_start();

include("../config/db.php");

if(!isset($_SESSION['user_id'])){

    header("Location:/login.php");
    exit;

}

$lead_id =
intval($_GET['id'] ?? 0);

$lead = $conn->query("
SELECT
    leads.*,
    campaigns.campaign_name
FROM leads

LEFT JOIN campaigns
ON campaigns.id = leads.campaign_id

WHERE leads.id = '$lead_id'

LIMIT 1
")->fetch_assoc();

if(!$lead){

    die("Lead no encontrado.");

}

?>

<!DOCTYPE html>
<html lang="es">

<head>

<meta charset="UTF-8">

<title>
Ver lead
</title>

<link rel="stylesheet" href="/assets/css/style.css">

<style>

.content-area{
    margin-left:260px;
    padding:40px;
}

.page-title{
    font-size:42px;
    font-weight:800;
    color:#02122c;
    margin-bottom:10px;
}

.page-subtitle{
    color:#64748b;
    margin-bottom:30px;
}

.white-card{
    background:#ffffff;
    border-radius:24px;
    padding:30px;
    margin-bottom:25px;
    box-shadow:0 10px 30px rgba(15,23,42,.06);
}

.info-grid{
    display:grid;
    grid-template-columns:
    repeat(auto-fit,minmax(280px,1fr));
    gap:20px;
}

.info-box{
    background:#f8fafc;
    border-radius:16px;
    padding:20px;
}

.info-label{
    font-size:13px;
    font-weight:700;
    color:#64748b;
    margin-bottom:10px;
    text-transform:uppercase;
}

.info-value{
    font-size:16px;
    color:#02122c;
    word-break:break-word;
}

.file-link{
    display:inline-flex;
    align-items:center;
    justify-content:center;
    height:42px;
    padding:0 18px;
    border-radius:12px;
    background:#dbeafe;
    color:#1d4ed8;
    text-decoration:none;
    font-weight:700;
}

.top-actions{
    margin-bottom:25px;
    display:flex;
    gap:14px;
}

.action-btn{
    display:inline-flex;
    align-items:center;
    justify-content:center;
    height:48px;
    padding:0 22px;
    border-radius:14px;
    text-decoration:none;
    font-weight:700;
}

.back-btn{
    background:#e2e8f0;
    color:#0f172a;
}

.contract-btn{
    background:linear-gradient(
        90deg,
        #5b4bff,
        #7b61ff
    );
    color:#fff;
}

</style>

</head>

<body>

<?php include("../includes/sidebar.php"); ?>

<div class="content-area">

    <div class="page-title">
        Lead #<?= $lead['id'] ?>
    </div>

    <div class="page-subtitle">
        Información completa del lead
    </div>

    <div class="top-actions">

        <a
            href="/pages/leads.php?campaign_id=<?= $lead['campaign_id'] ?>"
            class="action-btn back-btn"
        >
            Volver
        </a>

        <a
            href="/pages/contracts.php?lead_id=<?= $lead['id'] ?>"
            class="action-btn contract-btn"
        >
            Crear contrato
        </a>

    </div>

    <div class="white-card">

        <div class="info-grid">

            <div class="info-box">

                <div class="info-label">
                    Campaña
                </div>

                <div class="info-value">
                    <?= htmlspecialchars($lead['campaign_name'] ?? '') ?>
                </div>

            </div>

            <div class="info-box">

                <div class="info-label">
                    Nombre
                </div>

                <div class="info-value">
                    <?= htmlspecialchars($lead['full_name'] ?? '') ?>
                </div>

            </div>

            <div class="info-box">

                <div class="info-label">
                    Documento
                </div>

                <div class="info-value">
                    <?= htmlspecialchars($lead['document_number'] ?? '') ?>
                </div>

            </div>

            <div class="info-box">

                <div class="info-label">
                    Teléfono
                </div>

                <div class="info-value">
                    <?= htmlspecialchars($lead['phone'] ?? '') ?>
                </div>

            </div>

            <div class="info-box">

                <div class="info-label">
                    Email
                </div>

                <div class="info-value">
                    <?= htmlspecialchars($lead['email'] ?? '') ?>
                </div>

            </div>

        </div>

    </div>

    <div class="white-card">

        <div class="page-subtitle">
            Campos personalizados
        </div>

        <div class="info-grid">

<?php

$custom_values = $conn->query("
SELECT
    custom_fields.field_label,
    custom_fields.field_type,
    lead_custom_values.field_value

FROM lead_custom_values

INNER JOIN custom_fields
ON custom_fields.id = lead_custom_values.field_id

WHERE lead_custom_values.lead_id = '$lead_id'
");

while($field = $custom_values->fetch_assoc()):

?>

<div class="info-box">

    <div class="info-label">
        <?= htmlspecialchars($field['field_label']) ?>
    </div>

    <div class="info-value">

        <?php if($field['field_type'] == 'file'): ?>

            <a
                href="<?= htmlspecialchars($field['field_value']) ?>"
                target="_blank"
                class="file-link"
            >
                Ver archivo
            </a>

        <?php else: ?>

            <?= nl2br(htmlspecialchars($field['field_value'])) ?>

        <?php endif; ?>

    </div>

</div>

<?php endwhile; ?>

        </div>

    </div>

</div>

</body>
</html>