<?php
ob_start();
session_start();
include("fresh_panel/inc/config.php");
    // Getting all language variables into array as global variable
    $i=1;
    $statement = $pdo->prepare("SELECT * FROM tbl_language");
    $statement->execute();
    $result = $statement->fetchAll(PDO::FETCH_ASSOC);
    foreach ($result as $row) {
        define('LANG_VALUE_'.$i,$row['lang_value']);
        $i++;
    }
    
    $status = 0;
    
?>
<?php

    if($_POST['mobile_number'] !='' && $_POST['otp']=='' && $_POST['cust_name']=='' && $_POST['cust_email']=='' && $_POST['reg_password']=='' && $_POST['reg_confirm_password']==''){
        $_SESSION['type']=2;
        
        $mob = $_POST['mobile_number'];                           
        
        $sql = "SELECT count(*) FROM `tbl_customer` WHERE cust_phone = ?"; 
        $result = $pdo->prepare($sql); 
        $result->execute([$mob]); 
        $number_of_rows = $result->fetchColumn(); 
        
        
        if($number_of_rows>0){
            $error_message =  'This Mobile is allready Exits. Please Login..';
        }
        else
        {
            $otp=mt_rand(100000,999999);
            $_SESSION['otp']=$otp;
            $success_message = $otp;
            $status=1;
            $sender = SMS_SENDER;
            $username = SMS_USER;
            $password2 = SMS_PASS; 
            $number=$mob;
            $message="Verification for FRESH MART AJ  OTP  is . $otp";
            $url="http://buzz.azmarq.com/http-api.php?username=".urlencode($username)."&password=".urlencode($password2)."&senderid=".urlencode($sender)."&route=1&number=".urlencode($number)."&message=".urlencode($message);
            file_get_contents($url);
        } 
    }

    if($_POST['mobile_number'] !='' && $_POST['otp']!='' && $_POST['cust_name']=='' && $_POST['cust_email']=='' && $_POST['reg_password']=='' && $_POST['reg_confirm_password']==''){
        $_SESSION['type']=3;
        $mob = $_POST['mobile_number'];
        $otp = $_POST['otp'];
        
        
        if($_SESSION['otp'] == $otp){
            $status=1;
            $success_message =  'Otp Verified Successfully.';
        }
        else
        {
            $error_message='Otp Not Matched.';
            $status=0;
        } 
    }
    
    

    if($_POST['mobile_number'] !='' && $_POST['otp']!='' && $_POST['cust_name']!='' && $_POST['cust_email']!='' && $_POST['reg_password']!='' && $_POST['reg_confirm_password']!=''){
        
        $mob = $_POST['mobile_number'];
        $otp = $_POST['otp'];
        $cust_name =  $_POST['cust_name'];
        $cust_email =  $_POST['cust_email'];
        $reg_password =  $_POST['reg_password'];
        $reg_confirm_password =  $_POST['reg_confirm_password'];
        
        
        if($_POST['reg_password'] == $_POST['reg_confirm_password']){
            $token = md5(time());
$cust_datetime = date('Y-m-d h:i:s');
$cust_timestamp = time();

// saving into the database
$statement = $pdo->prepare("INSERT INTO tbl_customer (
cust_name,
cust_email,
cust_phone,
cust_password,
cust_token,
cust_datetime,
cust_timestamp,
cust_status,
code,
pass
) VALUES (?,?,?,?,?,?,?,?,?,?)");
$statement->execute(array(
strip_tags($_POST['cust_name']),
strip_tags($_POST['cust_email']),
strip_tags($_POST['mobile_number']),
base64_encode($reg_password),
$token,
$cust_datetime,
$cust_timestamp,
'1',
$otp,
$reg_password
));


 $statement2 = $pdo->prepare("SELECT * FROM tbl_customer WHERE cust_phone=?");
$statement2->execute(array($mob));
$result2 = $statement2->fetchAll(PDO::FETCH_ASSOC);
foreach ($result2 as $row2) {

$code1= $row2['code'];
$pass=$row2['pass'];
$user=$row2['cust_phone'];
$cust_email = $row2['cust_email'];
$name = $cust_name;


$to = $cust_email;
$subject = "Welcome Message !!";
$message = "<h3><strong>Dear $name .</strong>Your Registration is successful.  User Id: $user And Password: $pass !! Thank You For Visiting FRESH MART AJ.</h3>";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$header = "From:".BASE_MALE."\r\n";
$header .= "Cc:".$meta_title_home."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);



             $sender = SMS_SENDER;
             $username = SMS_USER;
             $password2 = SMS_PASS; 
            $number=$mob;
            $message="Dear $name ,Your Welcome. User Id: $user And Password: $pass !! Thank You For Visiting FRESH MART AJ.";
            $url="http://buzz.azmarq.com/http-api.php?username=".urlencode($username)."&password=".urlencode($password2)."&senderid=".urlencode($sender)."&route=1&number=".urlencode($number)."&message=".urlencode($message);
            file_get_contents($url);


$_SESSION['customer'] = $row2;
}




            $status=1;
            $success_message =  "Account Created Successfully. <script>window.location.href='index.php'</script>";
            unset($_SESSION['type']);
        }
        else
        {
            $error_message='Password AND Confirm Password Do Not Matched.';
            $status=0;
        } 
    }
    
    
      // response for user
    $myObj->succ = $success_message;
    $myObj->error = $error_message;
    $myObj->log = $status;
    $myObj->form = $_SESSION['type'];
    
    $myJSON = json_encode($myObj);
    
    echo $myJSON;
    
?>  