
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
<?php 

//print_r($_POST);

?>

<?php
date_default_timezone_set('Asia/Riyadh'); // Set PHP timezone to Riyadh

// Database connection
$servername = "localhost";  // Change this if necessary
$username = "balubaid_autozoneoffers";         // Your database username
$password = "Vision@2050";             // Your database password
$database = "balubaid_autozoneoffers";  // Your database name


$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Sanitize and retrieve form data
$mobile = isset($_POST['mobile']) ? $conn->real_escape_string($_POST['mobile']) : '';

if (empty($mobile)) {
    die("Mobile number is required.");
}

// Check if the mobile number has submitted a lead in the last 15 days
$checkQuery = "SELECT id FROM leads WHERE mobile = '$mobile' AND created_at >= DATE_SUB(NOW(), INTERVAL 15 DAY)";
$result = $conn->query($checkQuery);

if ($result->num_rows > 0) {
    $is_duplicate = '1';
   // echo "Duplicate Lead";
    

// If no recent lead exists, insert the new lead
$utm_source = isset($_POST['utm_source']) ? $conn->real_escape_string($_POST['utm_source']) : '';
$utm_campaign = isset($_POST['utm_campaign']) ? $conn->real_escape_string($_POST['utm_campaign']) : '';
$formtype = isset($_POST['formtype']) ? $conn->real_escape_string($_POST['formtype']) : '';
$offer_id = isset($_POST['offer_id']) ? (int)$_POST['offer_id'] : 0;
$fullName = isset($_POST['fullName']) ? $conn->real_escape_string($_POST['fullName']) : '';
$email = isset($_POST['email']) ? $conn->real_escape_string($_POST['email']) : '';
$salary = isset($_POST['salary']) ? (int)$_POST['salary'] : 0;
$model = isset($_POST['model']) ? $conn->real_escape_string($_POST['model']) : '';
$nationality = isset($_POST['nationality']) ? $conn->real_escape_string($_POST['nationality']) : '';
$bank = isset($_POST['bank']) ? $conn->real_escape_string($_POST['bank']) : '';
$gender = isset($_POST['gender']) ? $conn->real_escape_string($_POST['gender']) : '';
$city = isset($_POST['city']) ? $conn->real_escape_string($_POST['city']) : '';
$branch = isset($_POST['branch']) ? $conn->real_escape_string($_POST['branch']) : '';
$obligation = isset($_POST['obligation']) ? $conn->real_escape_string($_POST['obligation']) : '';
$realestateLoan = isset($_POST['realestateLoan']) ? $conn->real_escape_string($_POST['realestateLoan']) : '';
$obligationAmount = isset($_POST['obligationAmount']) ? (int)$_POST['obligationAmount'] : 0;
$purchaseTime = isset($_POST['purchaseTime']) ? $conn->real_escape_string($_POST['purchaseTime']) : '';
$terms = isset($_POST['terms']) ? $conn->real_escape_string($_POST['terms']) : '';
$otp = isset($_POST['otp']) ? $conn->real_escape_string($_POST['otp']) : '';
$otpVerified = isset($_POST['otpVerified']) ? $conn->real_escape_string($_POST['otpVerified']) : '';

$created_at = date('Y-m-d H:i:s'); // Current timestamp in Riyadh timezone

$sql = "INSERT INTO duplicate_leads (utm_source, utm_campaign, formtype, offer_id, fullName, email, mobile, salary, model, nationality, bank, gender, city, branch, obligation, realestateLoan, obligationAmount, purchaseTime, terms, created_at,otp,otpVerified)
VALUES ('$utm_source', '$utm_campaign', '$formtype', $offer_id, '$fullName', '$email', '$mobile', $salary, '$model', '$nationality', '$bank', '$gender', '$city', '$branch', '$obligation', '$realestateLoan', $obligationAmount, '$purchaseTime', '$terms', '$created_at','$otp','$otpVerified')";

if ($conn->query($sql) === TRUE) {
    //echo "Data successfully inserted!";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}    
    
    
    
}else{

// If no recent lead exists, insert the new lead
$utm_source = isset($_POST['utm_source']) ? $conn->real_escape_string($_POST['utm_source']) : '';
$utm_campaign = isset($_POST['utm_campaign']) ? $conn->real_escape_string($_POST['utm_campaign']) : '';
$formtype = isset($_POST['formtype']) ? $conn->real_escape_string($_POST['formtype']) : '';
$offer_id = isset($_POST['offer_id']) ? (int)$_POST['offer_id'] : 0;
$fullName = isset($_POST['fullName']) ? $conn->real_escape_string($_POST['fullName']) : '';
$email = isset($_POST['email']) ? $conn->real_escape_string($_POST['email']) : '';
$salary = isset($_POST['salary']) ? (int)$_POST['salary'] : 0;
$model = isset($_POST['model']) ? $conn->real_escape_string($_POST['model']) : '';
$nationality = isset($_POST['nationality']) ? $conn->real_escape_string($_POST['nationality']) : '';
$bank = isset($_POST['bank']) ? $conn->real_escape_string($_POST['bank']) : '';
$gender = isset($_POST['gender']) ? $conn->real_escape_string($_POST['gender']) : '';
$city = isset($_POST['city']) ? $conn->real_escape_string($_POST['city']) : '';
$branch = isset($_POST['branch']) ? $conn->real_escape_string($_POST['branch']) : '';
$obligation = isset($_POST['obligation']) ? $conn->real_escape_string($_POST['obligation']) : '';
$realestateLoan = isset($_POST['realestateLoan']) ? $conn->real_escape_string($_POST['realestateLoan']) : '';
$obligationAmount = isset($_POST['obligationAmount']) ? (int)$_POST['obligationAmount'] : 0;
$purchaseTime = isset($_POST['purchaseTime']) ? $conn->real_escape_string($_POST['purchaseTime']) : '';
$terms = isset($_POST['terms']) ? $conn->real_escape_string($_POST['terms']) : '';
$otp = isset($_POST['otp']) ? $conn->real_escape_string($_POST['otp']) : '';
$otpVerified = isset($_POST['otpVerified']) ? $conn->real_escape_string($_POST['otpVerified']) : '';

$created_at = date('Y-m-d H:i:s'); // Current timestamp in Riyadh timezone

$sql = "INSERT INTO leads (utm_source, utm_campaign, formtype, offer_id, fullName, email, mobile, salary, model, nationality, bank, gender, city, branch, obligation, realestateLoan, obligationAmount, purchaseTime, terms, created_at,otp,otpVerified)
VALUES ('$utm_source', '$utm_campaign', '$formtype', $offer_id, '$fullName', '$email', '$mobile', $salary, '$model', '$nationality', '$bank', '$gender', '$city', '$branch', '$obligation', '$realestateLoan', $obligationAmount, '$purchaseTime', '$terms', '$created_at','$otp','$otpVerified')";

if ($conn->query($sql) === TRUE) {
    //echo "Data successfully inserted!";
} else {
   // echo "Error: " . $sql . "<br>" . $conn->error;
}


function getbranchValue($lookup_value) {
    // Database connection parameters
    $servername = "localhost";
    $username = "balubaid_autozoneoffers";
    $password = "Vision@2050";
    $dbname = "balubaid_autozoneoffers";
    

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    // Prepare SQL query to prevent SQL injection
    $stmt = $conn->prepare("SELECT result_value FROM branch_lookup WHERE lookup_value = ?");
    $stmt->bind_param("s", $lookup_value);  // "s" means string type

    // Execute the query
    $stmt->execute();
    $stmt->bind_result($result_value);

    // Fetch the result
    if ($stmt->fetch()) {
        $stmt->close();
        $conn->close();
        return $result_value;
    } else {
        $stmt->close();
        $conn->close();
        return null; // Return null if no match is found
    }
}



function getcityValue($lookup_value) {
    // Database connection parameters
    $servername = "localhost";
    $username = "balubaid_autozoneoffers";
    $password = "Vision@2050";
    $dbname = "balubaid_autozoneoffers";
    

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    // Prepare SQL query to prevent SQL injection
    $stmt = $conn->prepare("SELECT result_value FROM city_lookup WHERE lookup_value = ?");
    $stmt->bind_param("s", $lookup_value);  // "s" means string type

    // Execute the query
    $stmt->execute();
    $stmt->bind_result($result_value);

    // Fetch the result
    if ($stmt->fetch()) {
        $stmt->close();
        $conn->close();
        return $result_value;
    } else {
        $stmt->close();
        $conn->close();
        return null; // Return null if no match is found
    }
}


function getbankValue($lookup_value) {
    // Database connection parameters
    $servername = "localhost";
    $username = "balubaid_autozoneoffers";
    $password = "Vision@2050";
    $dbname = "balubaid_autozoneoffers";
    

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    // Prepare SQL query to prevent SQL injection
    $stmt = $conn->prepare("SELECT result_value FROM bank_lookup WHERE lookup_value = ?");
    $stmt->bind_param("s", $lookup_value);  // "s" means string type

    // Execute the query
    $stmt->execute();
    $stmt->bind_result($result_value);

    // Fetch the result
    if ($stmt->fetch()) {
        $stmt->close();
        $conn->close();
        return $result_value;
    } else {
        $stmt->close();
        $conn->close();
        return null; // Return null if no match is found
    }
}


if($salary > 10000){
    
$vsalary = 'أكثر من ١٠٠٠٠ - Above 10000';    
    
}elseif($salary > 5000){

$vsalary = 'من ٥٠٠٠ إلى ١٠٠٠٠ - From 5000 to 10000';    
    
}elseif($salary > 4000){

$vsalary = 'أكثر من ١٠٠٠٠ - Above 10000';
    
}

if($gender == 'Male'){

$vgender = 'Male - ذكر';    
    
}

if($gender == 'Female'){

$vgender = 'Female - أنثى';    
    
}

$vbank = getbankValue($bank);
$vbranch = getbranchValue($branch);
$vcity = getcityValue($city);


$data = array('your-name' => $fullName,'phonenumber' => $mobile,'your-email' => $email,'your-car' => $model,'source' => $utm_source,'campaign' => $utm_campaign, 'your-city' => $vcity,'gender' => $vgender,'salary' => $vsalary,'your-branch' => $vbranch,'your-bank' => $vbank,'formid' => $offer_id,'form-type' => $formtype);	   
      $ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"https://lp.autozone.com.sa/service_request/get_service.php?confirm=@CRM1");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);

// In real life you should use something like:
// curl_setopt($ch, CURLOPT_POSTFIELDS, 
//          http_build_query(array('postvar1' => 'value1')));

// Receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec($ch);

curl_close ($ch);

//print_r($server_output);
//print_r(json_decode(trim($server_output),true));

$oppdetail = json_decode(trim($server_output),true);


//echo $oppdetail['Agent Name'];
//echo "<br>";
//echo $oppdetail['Branch Name'];
//echo "<br>";
//echo $oppdetail['address'];
//echo "<br>";
//echo $oppdetail['agentnumber'];
//echo "<br>";
//echo $oppdetail['branchurl'];


}

$conn->close();




?>

<?php
// Confirmation page for Autozone loan application submission
// Sample data for agent details (to be replaced with dynamic data from database)

if($formtype=='cash' || $formtype=='highscore'){

$autoassigned = 1;    
    
}

$agent_name = $oppdetail['Agent Name'];
$agent_mobile = $oppdetail['agentnumber'];
$agent_mobile2 = "966".substr($oppdetail['agentnumber'],1);

$text = rawurlencode("Hello " . $oppdetail['Agent Name'] . ", I'm " . $fullName . " and ready to finalize my new " . $model . " purchase. Please guide me on the next steps. Thank you!");

$whatsapp_link = "https://wa.me/".$agent_mobile2."?text=".$text;
$showroom_location = $oppdetail['branchurl'];
?>
<!DOCTYPE html>
<html lang="en">
<head>

<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-5QL8T48Q');</script>
<!-- End Google Tag Manager -->
    
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Your Dream Car Awaits! - <?php if($is_duplicate == '1'){echo "Duplicate Submission";}else{echo "Thank-You Submission";} ?></title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            text-align: center;
            margin: 0;
            padding: 0;
        }
        .container {
            background: #fff;
            max-width: 500px;
            margin: 50px auto;
            padding: 30px;
            border-radius: 15px;
            box-shadow: 0px 4px 15px rgba(0, 0, 0, 0.1);
        }
        .logo img {
            max-width: 160px;
            margin-bottom: 20px;
        }
        .checkmark {
            font-size: 70px;
            color: #FEC327;
        }
        h2 {
            color: #222;
            font-size: 26px;
            margin-bottom: 15px;
        }
        p {
            color: #555;
            font-size: 18px;
            margin-bottom: 15px;
        }
        .contact, .agent-info {
            margin-top: 20px;
            padding: 15px;
            background: #f9f9f9;
            border-radius: 10px;
            box-shadow: 0px 2px 10px rgba(0, 0, 0, 0.05);
        }
        .contact a, .save-contact, .save-vcf {
            color: #FEC327;
            text-decoration: none;
            font-weight: bold;
        }
        .social-icons {
            margin-top: 20px;
        }
        .social-icons a {
            margin: 0 10px;
            font-size: 24px;
            color: #444;
            text-decoration: none;
        }
        .button {
            display: inline-flex;
            align-items: center;
            padding: 12px 20px;
            background: #FEC327;
            color: #222;
            border-radius: 8px;
            text-decoration: none;
            font-size: 18px;
            font-weight: bold;
            margin-top: 10px;
            transition: background 0.3s ease;
            transform: skew(-6deg, 0deg);
        }
        .button i {
            margin-right: 10px;
        }
        .button:hover {
            background: #E0A800;
        }
    </style>
</head>
<body>

<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-5QL8T48Q"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->    
    
    <div class="container">
        <div class="logo">
            <img style="height:auto;width:52%;" src="https://autozone.com.sa/wp-content/uploads/2025/01/AZ_Original_EN.png" alt="Autozone Logo">
        </div>
        <p>You're one step closer to driving your dream car!</p>
        <div class="checkmark"><i class="fas fa-check-circle"></i></div>
        <h2>Congratulations!</h2>
        <p>Your application has been successfully submitted.</p>
<?php if($autoassigned == 1 && $is_duplicate == '' && $agent_name != ''){ ?>        
        <div class="agent-info">
            <h3>Your Dedicated Sales Consultant</h3>
            <p><strong>Name:</strong> <?php echo $agent_name; ?></p>
            <p><strong>Mobile:</strong> <?php echo $agent_mobile; ?></p>
            
            <a href="<?php echo $whatsapp_link; ?>" class="button">
                <i class="fab fa-whatsapp"></i> Chat with Your Agent
            </a>
            
            <br>
            
            <a href="<?php echo $showroom_location; ?>" class="button">
                <i class="fa-solid fa-store"></i> Showroom Location
            </a>
            
            
            <br>
            <br>
            <br>
            <a href="https://specialoffers.autozone.com.sa/generate-vcf/?name=<?php echo urlencode($agent_name); ?>&phone=<?php echo urlencode($agent_mobile); ?>&location=<?php echo urlencode($showroom_location); ?>" class="button">
                <i class="fas fa-save"></i> Save Contact
            </a>
            
             </div>
<?php  } ?>        
        <p>Stay connected with us!</p>
        <div class="social-icons">
            <a href="https://x.com/autozoneksa/"><i class="fab fa-twitter"></i></a>
            <a href="https://facebook.com/autozoneksa/"><i class="fab fa-facebook"></i></a>
            <a href="https://instagram.com/autozoneksa/"><i class="fab fa-instagram"></i></a>
        </div>
        <div class="contact">
            <p>If you have any questions, feel free to reach out!</p>
            <p><a href="mailto:customercare@autozone.com.sa">customercare@autozone.com.sa</a></p>
            <p>Call us at <strong>8007606000</strong></p>
        </div>
    </div>

<script>

var gtm_mobile = "<?php echo $mobile; ?>";

</script>

<script>

var gtm_email = "<?php echo $email; ?>";

</script>

<script>

var gtm_source = "<?php echo $utm_source; ?>";

</script>
    
</body>
</html>