 <?php
include('../../../function/__data_app_db.php');
 $_name_in_jsnj = $_referral_in_jsnj = $__go_to = $errData = '';
 $__data_ction = 'false';
if(isset($_POST["tkn"]) && $_POST["tkn"] == $_SESSION['signup_token']){
if(isset($_POST["name"])){

 
  if (empty($_POST["name"])) {
    $errData .= "| Name is required |";
  } else {
    $_name_in_jsnj = validate_jsnj_data($_POST['name']);
  }

	
if (empty($_POST["email"])) {
    $_email_in_jsnj = "| Email is required |";
  } else {
   $_email_in_jsnj = validate_jsnj_data($_POST['email']);
    if (!filter_var($_email_in_jsnj, FILTER_VALIDATE_EMAIL)) {
      $errData .= "| Invalid email format |"; 
    }
  }
  if (empty($_POST["phone"])) {
    $errData .= "| Phone is required |";
  } else {
    $_phone_in_jsnj = validate_jsnj_data($_POST['phone']);
  }
if($_POST["password"] == $_POST["confirmpassword"])	{
  if (empty($_POST["password"])) {
    $errData .= "| Password is required |";
  } else {
    $_password_in_jsnj = validate_jsnj_data(md5($_POST['password']));
    $_password_salt_in_jsnj = validate_jsnj_data($_POST['password']);
  }
} else {
	$errData .= "| The password and its confirm are not the same |";
}
  $_ip_jsnj = validate_jsnj_data($ip);
	
$query_check = mysqli_query($_conn_db,"select * from users where email = '$_email_in_jsnj' or mobile = '$_phone_in_jsnj'");
	if(mysqli_num_rows($query_check)>0){
		$errData .= '| Email or Mobile phone already exist |';
	}	
		
  if (empty($_POST["referral_code"])) {
    $errData .= "";
  } else {
	 $_referral_in_jsnj = validate_jsnj_data($_POST['referral_code']);
	$query_check = mysqli_query($_conn_db,"select * from users where referral_code = '$_referral_in_jsnj' limit 1");
	if(mysqli_num_rows($query_check)>0){
		 while($data_ref = mysqli_fetch_array($query_check)){
			 $senderid = $data_ref['login_id'];
		 };
		$errData .= "";
	} else {
		$errData .= '| Referral Code not found please check it once |';
	}
  }

include_once('../function_pin_generate.php');
  $db_query = "insert into users (full_name, password,  password_salt, email, mobile, referral_code, ip_address) values('$_name_in_jsnj', '$_password_in_jsnj', '$_password_salt_in_jsnj', '$_email_in_jsnj', '$_phone_in_jsnj', '$_generated_pin', '$_ip_jsnj')";
  
 if($errData=='')
 {
  if(mysqli_query($_conn_db, $db_query))
  {
    $__json_res_out = 'Congratulations!';
	$__notif_disc = 'Your account created successfully.';
	$__notif_type = 'success';
	$__go_to = 'dashboard';
	$__data_ction = 'true';
	$newuserid=$_conn_db->insert_id; 
if (!empty($_POST["referral_code"])) {
   $ref_query = "insert into referral (sendinguserid, newuserid, ip_address) values('$senderid', '$newuserid',  '$_ip_jsnj')";
   mysqli_query($_conn_db, $ref_query);
};
	  $_SESSION['user_num'] = show_jsnj_data($newuserid);
	  $_SESSION['user_login'] = 'true';
	  $_SESSION['name'] = show_jsnj_data($_name_in_jsnj);
	  $_SESSION['password'] = show_jsnj_data($_password_salt_in_jsnj);
	  $_SESSION['email'] = show_jsnj_data($_email_in_jsnj);
	  $_SESSION['mobile'] = show_jsnj_data($_phone_in_jsnj);
	  $_SESSION['user_t'] = '1';
	  $_SESSION['referral_code'] = show_jsnj_data($_generated_pin);
	  
   unset ($_SESSION['referral_code']);
   unset ($_SESSION['signup_token']);
	  
  }
  else
  {
	$__json_res_out = 'Oops!';
	$__notif_disc = 'Something want wrong please reload this page';
	$__notif_type = 'danger';
  }
 }
 else
 {
   $__json_res_out = 'Oops!';
   $__notif_disc = $errData;
   $__notif_type = 'warning';
 } 
 } else {
	$__json_res_out = 'Oops!';
	$__notif_disc = 'Something Want Wrong';
	$__notif_type = 'danger';
}


$dataResult = array('page' => 'signup', 'res' => $__data_ction, 'notif_title' => $__json_res_out, 'notif_desc' => $__notif_disc,  'notif_type' => $__notif_type, 'goTo' => $__go_to);
echo json_encode($dataResult);
	} else {
   echo 'lol';
};
?>