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

date_default_timezone_set("Asia/Riyadh");
$date = date("Y-m-d h:i:sa");

//print_r($_GET);

function extractLabel($input) {
    // Remove curly braces
    $clean = trim($input, "{}");

    // Split on colon
    $parts = explode(":", $clean);

    // Return the label part
    return $parts[0] ?? '';
}

function convert2english($string) {
    $newNumbers = range(0, 9);
    // 1. Persian HTML decimal
    $persianDecimal = array('&#1776;', '&#1777;', '&#1778;', '&#1779;', '&#1780;', '&#1781;', '&#1782;', '&#1783;', '&#1784;', '&#1785;');
    // 2. Arabic HTML decimal
    $arabicDecimal = array('&#1632;', '&#1633;', '&#1634;', '&#1635;', '&#1636;', '&#1637;', '&#1638;', '&#1639;', '&#1640;', '&#1641;');
    // 3. Arabic Numeric
    $arabic = array('٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩');
    // 4. Persian Numeric
    $persian = array('۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹');

    $string =  str_replace($persianDecimal, $newNumbers, $string);
    $string =  str_replace($arabicDecimal, $newNumbers, $string);
    $string =  str_replace($arabic, $newNumbers, $string);
    return str_replace($persian, $newNumbers, $string);
}

function getLookupValue($lookup_value,$type) {
    // Database connection parameters
    $servername = "localhost";
    $username = "balubaid_core3_leads";
    $password = "Vision@2050";
    $dbname = "balubaid_core3_leads";
    

    // 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 lookup_values   WHERE lookup_value = ? and type = ?");
    $stmt->bind_param("ss", $lookup_value,$type);  // "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 normalizeSaudiMobile($input)
{
    // Remove all non-numeric characters
    $number = preg_replace('/\D+/', '', $input);

    // If starts with country code 966
    if (strpos($number, '966') === 0) {
        $number = substr($number, 3);
    }

    // If starts with leading zero (05xxxxxxxx)
    if (strpos($number, '0') === 0) {
        $number = substr($number, 1);
    }

    // Final validation: must be 9 digits and start with 5
    if (preg_match('/^5\d{8}$/', $number)) {
        return $number;
    }

    return null; // Invalid number
}

$your_name = $_POST['fname']." ".$_POST['lname'];
$phonenumber = "0".normalizeSaudiMobile(convert2english($_POST['mobile']));
$your_car = getLookupValue($_POST['adid'],'model');
$your_city  = extractLabel($_POST['city']);
$your_email = $_POST['email'];
$source = 'Snapchat';
$gender = "NA";
$formid = $_POST['formid'];
$salary = extractLabel($_POST['salary']);
$paymethod = extractLabel($_POST['paymethod']);
$campaign = 'YearEndOffer2025';


$sql = "insert into xx_honda_leads 
(
name,
mobile,
car,
city,
request_date,
email_address,
source,
gender,
formid,
salary,
paymethod,
campaign
)
values
(
'$your_name',
'$phonenumber',
'$your_car',
'$your_city',
'$date',
'$your_email',
'$source',
'$gender',
'$formid',
'$salary',
'$paymethod',
'$campaign'
)";
     

echo $sql;
    

$dbhost = 'localhost';
$dbuser = 'balubaid_core3_leads';
$db = 'balubaid_core3_leads';
$dbpass = 'Vision@2050';
//$id = $_GET['id'];
$link = mysqli_connect($dbhost, $dbuser, $dbpass, $db);
$link->set_charset("utf8");
if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

$confirm = $_GET['confirm'];
$date = date("Y-m-d h:i:sa");

if($confirm == '@CRM1'){

$codequery = $sql;

if ($link->query($codequery) === TRUE) {

    
}
}
?>