
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
<?php
date_default_timezone_set('Asia/Riyadh');

// DB credentials
$servername = "localhost";
$username = "balubaid_autozoneoffers";
$password = "Vision@2050";
$database = "balubaid_autozoneoffers";

$conn = new mysqli($servername, $username, $password, $database);
if ($conn->connect_error) die("Connection failed: " . $conn->connect_error);

// Fetch unsent leads
$sql = "SELECT * FROM leads WHERE id between '44708' and '44708'"; // Batch of 50
$result = $conn->query($sql);

// Lookup functions
function lookupValue($conn, $table, $lookup) {
    $stmt = $conn->prepare("SELECT result_value FROM $table WHERE lookup_value = ?");
    $stmt->bind_param("s", $lookup);
    $stmt->execute();
    $stmt->bind_result($result);
    $stmt->fetch();
    $stmt->close();
    return $result ?: null;
}

while ($row = $result->fetch_assoc()) {
    $vsalary = ($row['salary'] > 10000) ? 'أكثر من ١٠٠٠٠ - Above 10000' :
               (($row['salary'] > 5000) ? 'من ٥٠٠٠ إلى ١٠٠٠٠ - From 5000 to 10000' :
               (($row['salary'] > 4000) ? 'أكثر من ١٠٠٠٠ - Above 10000' : ''));
    $vgender = ($row['gender'] == 'Male') ? 'Male - ذكر' : (($row['gender'] == 'Female') ? 'Female - أنثى' : '');

    $vbank   = lookupValue($conn, "bank_lookup", $row['bank']);
    $vcity   = lookupValue($conn, "city_lookup", $row['city']);
    $vbranch = lookupValue($conn, "branch_lookup", $row['branch']);

    $data =  array(
        'your-name'     => $row['fullName'],
        'phonenumber'   => $row['mobile'],
        'your-email'    => $row['email'],
        'your-car'      => $row['model'],
        'source'        => $row['utm_source'],
        'campaign'      => $row['utm_campaign'],
        'your-city'     => $vcity,
        'gender'        => $vgender,
        'salary'        => $vsalary,
        'your-branch'   => $vbranch,
        'your-bank'     => $vbank,
        'formid'        => $row['offer_id'],
        'form-type'     => $row['formtype']
    );

    // Send to API
    $ch = curl_init("https://lp.autozone.com.sa/service_request/get_service_sync.php?confirm=@CRM1");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); // <== important: encode it
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        "Content-Type: application/x-www-form-urlencoded"
    ]);
    
    //curl_setopt($ch, CURLOPT_POST, true);
    $response = curl_exec($ch);
echo "<br>";
echo $response;

//print_r($data);

    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    // If successful, mark as sent
    if ($httpCode == 200 && strpos($response, 'Agent Name') !== false) {
       // $conn->query("UPDATE leads SET api_sent = 1 WHERE id = " . $row['id']);
    } else {
        error_log("Failed to resend lead ID {$row['id']}: $response");
    }
}

$conn->close();
?>