<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
include 'dbconfig.php';
include '../email.php';
if(isset($_SERVER['HTTP_ORIGIN'])) {
        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Max-Age: 86400');    // cache for 1 day
}
// Access-Control headers are received during OPTIONS requests
if($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
            header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
            header("Access-Control-Allow-Headers:        {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

        exit(0);
}

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$json = json_decode(file_get_contents('php://input'), true);
$name= $json['name'];
$mobile= $json['mobile'];
$email= $json['email'];
$password= sha1($json['password']);
$loggedForm  = "mobile";
$user_type_id=2;
$created_at = date('Y-m-d H:i:s');
$sql = "SELECT * FROM users WHERE user_name='$name'";
$result = $conn->query($sql);
$result1 = $conn->query("SELECT * FROM user_details WHERE email='$email'");
$result2 = $conn->query("SELECT * FROM user_details WHERE phone='$mobile'");
if(mysqli_num_rows($result)!=0){
$responseData = array('success'=>'0','message'=>"Name Already Exists");
$data = array('data' => $responseData );
echo json_encode( $data);   
}else if(mysqli_num_rows($result1)!=0){
$responseData = array('success'=>'0','message'=>"Email Id Already Exists");
$data = array('data' => $responseData );
echo json_encode( $data);   
}else if(mysqli_num_rows($result2)!=0){
$responseData = array('success'=>'0','message'=>"Mobile Number Already Exists");
$data = array('data' => $responseData );
echo json_encode( $data);   
}else{
$lower=strtolower($name);    
$ins="INSERT INTO users SET user_name='$lower',logged_from='$loggedForm',is_internal='1',password='$password',created_at='$created_at'";
$ins_result=$conn->query($ins);

$sql="SELECT user_id,sub_category_id,user_name FROM users where user_name='".$name."'";
	$result = $conn->query($sql);
	if (mysqli_num_rows($result) > 0) {
		// output data of each row
		while($row = mysqli_fetch_assoc($result)) {
			$user_id=$row["user_id"];
			$user_name=$row["user_name"];
		}
	}
	
$sqlUserDetail =$conn->query("INSERT INTO user_details (user_id,name,phone,email,created_at)VALUES ('".$user_id."','".$name."','".$mobile."','".$email."','".$created_at."')");
$sqlUserRole =$conn->query("INSERT INTO user_roles (user_id,role_id,created_at) VALUES ('".$user_id."','".$user_type_id."','".$created_at."')");

$customers_picture=$json['profile'];
if($customers_picture!=null){
$base_path=$_SERVER["DOCUMENT_ROOT"]; 
$image = substr($customers_picture, strpos($customers_picture, ",") + 1); 
$img = base64_decode($image);
$dir="workapp/console/image/profile/";
if (!file_exists($dir) and !is_dir($dir)) {
		mkdir($dir);
} 
$uploadfile = $dir."/pic_".time().".jpg";
$uploadfile1 = "pic_".time().".jpg";
file_put_contents($base_path.'/'.$uploadfile,$img);
$ins="UPDATE user_details SET image='$uploadfile1' WHERE user_id='$user_id'";
$ins_result=$conn->query($ins);
}	
$html=' <h1>Hi '.$user_name.',</h1> 
        <h3>Thanks you for joining with us!.</h3>';
sendemail($email,"Registration",$html);
$responseData = array('success'=>'1','message'=>"success");
$data = array('data' => $responseData );
echo json_encode( $data);      
}
?>