• WhatsApp +34 600 96 26 23
<?php
$apiBase = 'https://crm.vitaelignum.com/api';
$webmaster_id_fijo   = '0001';
$affiliate_key_fijo  = 'afiliado1';

$result = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit_lead'])) {
    $data = [
        'ip'            => $_POST['ip'],
        'msisdn'        => $_POST['msisdn'],
        'affiliate_key' => $affiliate_key_fijo,
        'webmaster_id'  => $webmaster_id_fijo,
        'name'          => $_POST['name'],
        'goods_id'      => $_POST['goods_id'],
    ];
    if (!empty($_POST['domain'])) $data['domain'] = $_POST['domain'];
    if (!empty($_POST['url_params'])) {
        $json = json_decode($_POST['url_params'], true);
        if (is_array($json)) $data['url_params'] = $json;
    }

    $ch = curl_init($apiBase . '/wbmaster/new-lead');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $api_response = curl_exec($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    $result = "<b>Respuesta API (HTTP $http_code):</b><br><pre>" . htmlspecialchars($api_response) . "</pre>";
    $lead_id = '';
    $json = json_decode($api_response, true);
    if (isset($json['lead']['lead_id'])) {
        $lead_id = $json['lead']['lead_id'];
        $result .= "<b>Lead ID generado:</b> $lead_id";
    }
}

$estado_result = '';
if (isset($_GET['check_status'])) {
    $check_lead_id = $_GET['estado_lead_id'] ?? '';
    $check_webmaster_id = $_GET['estado_webmaster_id'] ?? '';
    if ($check_lead_id && $check_webmaster_id) {
        $url = $apiBase . "/wbmaster/lead-status/" . urlencode($check_lead_id) . "?webmaster_id=" . urlencode($check_webmaster_id);
        $opts = [ "http" => [ "ignore_errors" => true ] ];
        $context = stream_context_create($opts);
        $estado_response = @file_get_contents($url, false, $context);
        $http_code = '??';
        if (isset($http_response_header[0])) {
            if (preg_match('#HTTP/\d+\.\d+\s+(\d+)#', $http_response_header[0], $m)) {
                $http_code = $m[1];
            }
        }
        $estado_result = "<b>Respuesta de estado (HTTP $http_code):</b><br><pre>" . htmlspecialchars($estado_response) . "</pre>";
    } else {
        $estado_result = "<b>Por favor, rellena ambos campos para comprobar el estado.</b>";
    }
}
?>

<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>Formulario Lead - Vitae Lignum</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
        body {
            background: linear-gradient(135deg, #e0eafc, #cfdef3);
            font-family: 'Segoe UI', sans-serif;
            margin: 0;
            padding: 20px;
        }
        .container {
            max-width: 700px;
            margin: auto;
        }
        .card {
            background: white;
            padding: 25px;
            border-radius: 16px;
            box-shadow: 0 6px 20px rgba(0, 0, 0, 0.1);
            margin-bottom: 40px;
        }
        h2 {
            color: #2c3e50;
        }
        label {
            font-weight: 600;
            display: block;
            margin-top: 15px;
        }
        input[type=text],
        input[type=number],
        textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 8px;
            margin-top: 6px;
            font-size: 1rem;
        }
        textarea {
            min-height: 50px;
        }
        button {
            margin-top: 20px;
            background-color: #007bff;
            color: white;
            border: none;
            padding: 12px 20px;
            font-size: 1rem;
            border-radius: 10px;
            cursor: pointer;
            transition: background 0.3s;
        }
        button:hover {
            background-color: #0056b3;
        }
        pre {
            background: #f4f4f4;
            padding: 12px;
            border-radius: 10px;
            overflow-x: auto;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="card">
        <h2>Enviar Lead</h2>
        <form method="POST">
            <label>IP:</label>
            <input type="text" name="ip" value="<?php echo isset($_POST['ip']) ? htmlspecialchars($_POST['ip']) : $_SERVER['REMOTE_ADDR'] ?>" required>

            <label>Teléfono (msisdn):</label>
            <input type="text" name="msisdn" required value="<?php echo isset($_POST['msisdn']) ? htmlspecialchars($_POST['msisdn']) : '' ?>">

            <label>Nombre:</label>
            <input type="text" name="name" required value="<?php echo isset($_POST['name']) ? htmlspecialchars($_POST['name']) : '' ?>">

            <label>Producto ID (goods_id):</label>
            <input type="number" name="goods_id" required value="<?php echo isset($_POST['goods_id']) ? htmlspecialchars($_POST['goods_id']) : '' ?>">

            <label>Dominio/URL Referencia (opcional):</label>
            <input type="text" name="domain" value="<?php echo isset($_POST['domain']) ? htmlspecialchars($_POST['domain']) : '' ?>">

            <label>url_params (JSON opcional):</label>
            <textarea name="url_params"><?php echo isset($_POST['url_params']) ? htmlspecialchars($_POST['url_params']) : '' ?></textarea>

            <input type="hidden" name="affiliate_key" value="<?php echo $affiliate_key_fijo ?>">
            <input type="hidden" name="webmaster_id" value="<?php echo $webmaster_id_fijo ?>">

            <button type="submit" name="submit_lead" value="1">Enviar Lead</button>
        </form>
        <?php if ($result) echo "<hr>$result"; ?>
    </div>

    <div class="card">
        <h2>Consultar Estado de un Lead</h2>
        <form method="GET">
            <label>ID del Lead:</label>
            <input type="text" name="estado_lead_id" required value="<?php echo isset($_GET['estado_lead_id']) ? htmlspecialchars($_GET['estado_lead_id']) : '' ?>">

            <label>Webmaster ID:</label>
            <input type="text" name="estado_webmaster_id" required value="<?php echo isset($_GET['estado_webmaster_id']) ? htmlspecialchars($_GET['estado_webmaster_id']) : $webmaster_id_fijo ?>">

            <button type="submit" name="check_status" value="1">Consultar Estado</button>
        </form>
        <?php if ($estado_result) echo "<hr>$estado_result"; ?>
    </div>
</div>
</body>
</html>