<?php
session_start();
$should_send_lead = isset($_POST['promo']) && $_POST['promo'] === 'yes';
$pixel = isset($_POST['pixel']) ? $_POST['pixel'] : '';
$gclid = isset($_POST['gclid']) ? $_POST['gclid'] : '';
$wbraid = isset($_POST['wbraid']) ? $_POST['wbraid'] : '';
$gbraid = isset($_POST['gbraid']) ? $_POST['gbraid'] : '';
$tracking_param = '';
$tracking_value = '';
if (!empty($gclid)) {
    $tracking_param = 'gclid';
    $tracking_value = $gclid;
} else if (!empty($wbraid)) {
    $tracking_param = 'wbraid';
    $tracking_value = $wbraid;
} else if (!empty($gbraid)) {
    $tracking_param = 'gbraid';
    $tracking_value = $gbraid;
}

$redirect_url = "thankyou.php?pixel=" . urlencode($pixel);
if (!empty($tracking_param)) {
    $redirect_url .= "&" . $tracking_param . "=" . urlencode($tracking_value);
}

$lead_status = 'no_lead'; 

if ($should_send_lead && !empty($_POST['phone_number']) && !empty($_POST['email'])) {
    $url = 'https://aadmin.leadsilkroad.com/api/registration';
    $data = [
        "firstname"    => $_POST['firstname'] ?? '',
        "lastname"     => $_POST['lastname'] ?? '',
        "email"        => $_POST['email'] ?? '',
        "phone_number" => $_POST['phone_number'] ?? '',
        "phone_code"   => $_POST['phone_code'] ?? '',
        "country_iso"  => $_POST['country'] ?? '',
        "external_click_id" => $_POST['clickid'],
        "offer_token"  => '9Uagu4Nw4ekcfnUeGRmAWjwkS8GUkjSp',
        "sync"         => $_POST['sync'] ?? 1,
        "p1"           => $_POST['p1'] ?? '',
        "p2"           => $_POST['p2'] ?? '',
        "p3"           => $_POST['p3'] ?? '',
        "p4"           => $_POST['p4'] ?? '',
        "p5"           => $_POST['p5'] ?? '',
        "p6"           => $_POST['p6'] ?? '',
        "p7"           => $_POST['p7'] ?? '',
        "p8"           => $_POST['p8'] ?? '',
        "p9"           => $_POST['p9'] ?? '',
        "p10"          => $_POST['p10'] ?? '',
        "ip"           => (isset($_SERVER["HTTP_CF_CONNECTING_IP"])?$_SERVER["HTTP_CF_CONNECTING_IP"]:$_SERVER['REMOTE_ADDR']),
    ];
    
    $headers = [
        'authority'        => $_SERVER['HTTP_HOST'] ?? '',
        'accept'           => '*/*',
        'accept-language'  => $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? '',
        'content-type'     => 'application/json',
        'origin'           => $_SERVER['HTTP_ORIGIN'] ?? '',
        'referer'          => $_SERVER['HTTP_REFERER'] ?? '',
        'sec-ch-ua'        => $_SERVER['HTTP_SEC_CH_UA'] ?? '',
        'sec-ch-ua-mobile' => $_SERVER['HTTP_SEC_CH_UA_MOBILE'] ?? '',
        'sec-ch-ua-platform' => $_SERVER['HTTP_SEC_CH_UA_PLATFORM'] ?? '',
        'sec-fetch-dest'   => $_SERVER['HTTP_SEC_FETCH_DEST'] ?? '',
        'sec-fetch-mode'   => $_SERVER['HTTP_SEC_FETCH_MODE'] ?? '',
        'sec-fetch-site'   => $_SERVER['HTTP_SEC_FETCH_SITE'] ?? '',
        'sec-gpc'          => $_SERVER['HTTP_SEC_GPC'] ?? '',
        'user-agent'       => $_SERVER['HTTP_USER_AGENT'] ?? '',
        'x-language'       => 'en',
        'x-offer-token'    => '9Uagu4Nw4ekcfnUeGRmAWjwkS8GUkjSp'
    ];
    
    $curlHeaders = [];
    foreach ($headers as $key => $value) {
        if (!empty($value)) {
            $curlHeaders[] = "$key: $value";
        }
    }
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, $curlHeaders);
    $response = curl_exec($ch);
    $error = curl_error($ch);
    curl_close($ch);
    
    if ($error) {
        $lead_status = 'error_curl';
    } else {
        $response_data = json_decode($response, true);
        if (isset($response_data['redirect_url']) && !empty($response_data['redirect_url'])) {
            $_SESSION['auto_login_url'] = $response_data['redirect_url'];
            $lead_status = 'success';
        } else {
            $lead_status = 'error_no_url';
        }
    }
}

$_SESSION['lead_status'] = $lead_status;

header("Location: " . $redirect_url);
exit;
?>