<?php
date_default_timezone_set('Asia/Kolkata');

// Initialize URL to the variable
$ref = $_SERVER['HTTP_REFERER'];


// contains its various components
$url_components = parse_url($ref);
 
// Use parse_str() function to parse the
// string passed via URL
parse_str($url_components['query'], $params);
     
// Display result
$source=strtoupper($params['utm_source']);

 

 if($_POST['usermobile'] != "") {
// $servername = "103.50.163.40";
// $username = "imacempc_leads";
// $password = "leads@123";
// $dbname = "imacempc_leads";


    $servername = "148.66.133.154";
    $username = "investmango_InvestMango-Leads";
    $password = "Invest@9654";
    $dbname = "investmango_imacempc_leads";

//$source=$_GET['utm_source'];

  
$conn = new mysqli($servername, $username, $password, $dbname);
  $message_regex ="/^[0-9a-zA-Z ]+$/";
$name_regex ="/^[a-zA-Z ]*$/";
$phone_regex ="/^[0-9]*$/";


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

$projectName="Kolte Patil";
$interstedIn=$_POST['intersted_in'];
$email=$_POST['useremail'];
$name=$_POST['username'];
$phone=$_POST['usermobile'];
$country_code = $_POST['dial_code'];
$query_info=$_POST['usermsg'];


 if(!preg_match($name_regex,$name)){
    echo "<script>
            alert('Only alphabets are allowed in name');
            window.location.assign('".$ref."');
        </script>
    ";
    exit();
}else if(!preg_match($phone_regex,$phone)){
    echo "<script>
            alert('Only number are allowed in phone');
            window.location.assign('".$ref."');
        </script>
    ";
    exit();
}
//  $checkDuplicateQuery = "SELECT COUNT(*) as count FROM `leads` WHERE phone = '$phone'";
//     $duplicateResult = $conn->query($checkDuplicateQuery);
//     $duplicateData = $duplicateResult->fetch_assoc();

//     if ($duplicateData['count'] > 0) {
//         // Record with the same phone number already exists, you may choose to handle it accordingly
//         echo "<script>
//                 alert('A record with the same phone number already exists. No new record has been added.');
//                 window.location.assign('" . $ref . "');
//               </script>";
//         exit();
//     }
// $projectName="Ace Divino";
// $email="";
// $name="test";
// $phone="9643624962";
// $country_code = "91";
// $query_info="testing";


if($email !== ''){
    $email=$email;
}else{
    $email="guest@investmango.com";
}

if($name !== ''){
    $name=$name;
}else{
    $name="Guest";
}

if($country_code !== ''){
    $phone=$phone;    
    //$phone= "+".$country_code . $phone;
}else{
    $phone=$_POST['phone'];      
}


if($query_info !== ''){
    $query_info=$query_info." ".$interstedIn;
    // echo "is not mt";
}else{
    $query_info="Empty ".$interstedIn;
    // echo "mt";
}

if($source !== ''){
    $source=$source;
}else{
   $source	="ORGANIC";
}




// echo $projectName;
// echo "<br>";
// echo $email;
// echo "<br>";
// echo $name;
// echo "<br>";
// echo $phone;
// echo "<br>";
// echo $country_code;
// echo "<br>";
// echo "-".$query_info."-";
// echo "<br>";
// echo $source;
// echo "<br>";
// echo $ref;

// die();


$received_on=strtotime(date('d F Y, h:i:s A'));
//$source	="GOOGLE";
$storing_date=date('d F Y, h:i:s A');
$assigned_date='';

$sql = "INSERT INTO `leads`(`cmpct_labl`, `email`, `name`, `phone`, `query_info`, `received_on`, `source`, `storing_date`, `assigned_date`) VALUES ('$projectName','$email','$name','$phone','$query_info','$received_on','$source','$storing_date','$assigned_date')";

if ($conn->query($sql) === TRUE) {
 // echo "New record created successfully";
  //header('Location: thankyou.php');
} else {
  //echo "Error: " . $sql . "<br>" . $conn->error;
 // header('Location: thankyou.php');
}

$conn->close();

$message_regex ="/^[0-9a-zA-Z ]+$/";
$name_regex ="/^[a-zA-Z ]*$/";
$phone_regex ="/^[0-9]*$/";
 if(!preg_match($name_regex,$name)){
    echo "<script>
            alert('Only alphabets are allowed in name');
            window.location.assign('".$ref."');
        </script>
    ";
    exit();
}else if(!preg_match($phone_regex,$phone)){
    echo "<script>
            alert('Only number are allowed in phone');
            window.location.assign('".$ref."');
        </script>
    ";
    exit();
}

    $url_new="http://148.66.133.154:8181/add/google/leads";
    //$url_new="http://192.168.1.20:8000/add/google/leads";
    $data_new = array(
                            'name' => $name,
                            'email' => $email,
                            'countryCode' => $country_code,
                            'phone' => $phone,
                            'projectName' =>$projectName,
                            'queryInfo' => $query_info,
                            'source'    => $source
                    );
    $options_new= array(
         'http' => array(
             'header'  => "Content-Type: application/json\r\n",
             'method'  => 'POST',
             'content' => json_encode($data_new)
         )
     );
    $context_new = stream_context_create($options_new);
    $result_new = file_get_contents($url_new, false, $context_new);
  //  debug_to_console($result_new);
    if ($result_new === FALSE) { echo "<script>alert('Check your detail please.');</script>"; }
    
    $resultData_new = json_decode($result_new, TRUE);
    
    // echo "
    // <script>alert('Your inquiry has been sent successfully.');
    // window.location.href='thankyou.php';</script>";
    
		header('Location: thankyou.php');
    }
    else{
		header('Location:404.php');
    }

?>
