<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require_once 'inserts.php';
$str1 = "1234567890";
$str2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$str3 = strtolower($str2);
$str0 = $str1.$str2.$str3;
	$panel_msg="";
	function alert($x){
		echo "<script> alert('$x') </script>";
	}
	function show_notice($notice,$type=1){
		if($type==1){
			$panel_msg = "<div class='alert alert-success alert-dismissible fade show' role='alert'>
                                                                    <strong>$notice</strong>
                                                                <button type='button' class='btn-close' data-bs-dismiss='alert' aria-label='Close'></button>
                            </div>";
		}else{
			$panel_msg = "<div class='alert alert-warning alert-dismissible fade show' role='alert'>
                                                                    <strong>$notice</strong>
                                                                <button type='button' class='btn-close' data-bs-dismiss='alert' aria-label='Close'></button>
                            </div>";
		}
	}
	
	
function gen_token(){
	global $str0;
	$token_len=10;
$token = "";
while($token_len >0){
	$token .= $str0[rand(0,strlen($str0)-1)];
	$token_len--;
}
return $token;
}

// while($ll0 < 9){
    
// }

	function cls($x){
			global $conn;
			return mysqli_real_escape_string($conn,strtolower(trim($x)));
	}
		if(
		isset($_POST['first_name']) &&
		isset($_POST['last_name']) &&
		isset($_POST['email']) &&
		isset($_POST['username']) &&
		isset($_POST['country']) &&
		isset($_POST['phone']) &&
		isset($_POST['password']) &&
		isset($_POST['password_confirmation']) 
		){
			$fname = cls($_POST['first_name']);
			$lname = cls($_POST['last_name']);
			$email = cls($_POST['email']);
			$uname = cls($_POST['username']);
			$country = cls($_POST['country']);
			$phone = cls($_POST['phone']);
			$pass = cls($_POST['password']);
			$cpass = cls($_POST['password_confirmation']);
	if(
	!empty($fname)&&
	!empty($lname)&&
	!empty($email)&&
	!empty($uname)&&
	!empty($country)&&
	!empty($phone)&&
	!empty($pass)&&
	!empty($cpass)
	){
	    if(isset($_SESSION['reff'])){
	        $ref = $_SESSION['reff'];
	    }else{
	        $ref ='';
	    }

$id=9;
$select_query = "SELECT * FROM info WHERE email=?"; // SQL with parameters
$select_stmt = $conn->prepare($select_query); 
$select_stmt->bind_param("s", $email);
$select_stmt->execute();
$result = $select_stmt->get_result(); // get the mysqli result
$infos = $result->fetch_assoc(); // fetch data

$select_query = "SELECT * FROM info WHERE uname=?"; // SQL with parameters
$select_stmt = $conn->prepare($select_query); 
$select_stmt->bind_param("s", $uname);
$select_stmt->execute();
$result = $select_stmt->get_result(); // get the mysqli result
$infos_uname = $result->fetch_assoc(); // fetch data

	// print_R($infos);

        $id =null;
		$fname = $fname;
		$lname = $lname;
		$uname = $uname;
		$email = $email;
		$pass = $pass;
		$country=explode(':',$country);
		$country=$country[0];
			$remember_token  =  gen_token();
			$gender ="";
			$state = "";
			$city ="";
			$zip ="";
			$address ="";
			$dob='';
			$phone = $phone;
			$kyc =0;
			$avatar=null;
			$id_front=null;
			$id_back = null;
			$id_front_name="";
			$id_back_name = "";
			$level=1; 
			$bal=0;
			$bal2=0;
			$profit=0;
			 $ip=1;
			 $last_login;
			 $get_bonus=0;
			 $lock_account=0;
			$email_verified=0;
			$email_verified_at=null;
			// $created_at = '';
			// $updated_at = ;
		$db_email = (isset($infos['email'])) ? $infos['email'] : '';		
		$db_uname = (isset($infos['uname'])) ? $infos['uname'] : '';			
	if($db_email != $email){
	if($db_uname != $uname){
		if($country != 'zimbabwe'){
			if($stmt->execute()){

		   ($_SESSION['user_registered']=true);
			   include_once('../include/mail.php'); 
			   ($_SESSION['user_registered']=true);
			   			header('location:../register.php');

			}else{
			    			alert('db error');

			}
	}else{
	     ($_SESSION['user_registered']=true);
			 //  include_once('../include/mail.php'); 
			   ($_SESSION['user_registered']=true);
			   			header('location:../register.php');
	}
// 			show_notice("registered");
	}else{
		alert('Username already exist');
		
	}
	}else{
		alert('E-mail already exist');
	    
	}
		}
	}

?>