<?php
	session_start();
	include"../../connect/db.php"; 
 ?> 
<?php
	if( isset($_POST['fullname']) ){

		$fullname = mysqli_real_escape_string($con,$_POST['fullname']);
		$acct_type = mysqli_real_escape_string($con,$_POST['acct_type']);
		$d_o_b = mysqli_real_escape_string($con,$_POST['d_o_b']);
		$gender = mysqli_real_escape_string($con,$_POST['gender']);
		$country = mysqli_real_escape_string($con,$_POST['country']);
		$currency = mysqli_real_escape_string($con,$_POST['currency']);
		$city = mysqli_real_escape_string($con,$_POST['city']);
		$zipcode = mysqli_real_escape_string($con,$_POST['zipcode']);
		$ssn = mysqli_real_escape_string($con,$_POST['ssn']);
		$phone = mysqli_real_escape_string($con,$_POST['phone']);
		$email = mysqli_real_escape_string($con,$_POST['email']);
		$secquestion = mysqli_real_escape_string($con,$_POST['secquestion']);
		$secans = mysqli_real_escape_string($con,$_POST['sec_ans']);
		$pword = mysqli_real_escape_string($con,$_POST['pword']);
		$pin = mysqli_real_escape_string($con,$_POST['pin']);

		$query = mysqli_query($con,"SELECT email FROM users WHERE email = '$email' ");

			if( mysqli_num_rows($query) == 0 ){
				$target_dir2 = "../../e-banking/assets/images/checkuploads/".$email."/";

				$target_dir = "../../e-banking/assets/images/uploads/".$email."/";
				$img_loc = "assets/images/uploads/".$email."/";
				$target_file = $target_dir . basename($_FILES["profile-pic"]["name"]); 
				$img_loc_main = $img_loc . basename($_FILES["profile-pic"]["name"]);
				$uploadOk = 1;
				$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
				// Check if image file is a actual image or fake image
				if(isset($_POST["submit"])) {
				    $check = getimagesize($_FILES["profile-pic"]["tmp_name"]);
				    if($check !== false) {
				        $uploadOk = 1;
				    } else {
				        echo "File is not an image.";
				        exit();
				        $uploadOk = 0;
				    }
				}
				// Check file size
				if ($_FILES["profile-pic"]["size"] > 5000000) {
				    echo "Sorry, your file is too large.";
				    exit();
				    $uploadOk = 0;
				}
				// Allow certain file formats
				if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
				&& $imageFileType != "gif" ) {
				    echo "Not allowed";
					exit();
				    $uploadOk = 0;
				}
				// Check if $uploadOk is set to 0 by an error
				if ($uploadOk == 0) {
				    echo "Sorry, your file was not uploaded.";
				    exit();

				// if everything is ok, try to upload file
				} else {
			  		mkdir($target_dir);
					  mkdir($target_dir2);
				    if (move_uploaded_file($_FILES["profile-pic"]["tmp_name"], $target_file)) {

		                $seed = str_split('0123456789'); // and any other characters
                        shuffle($seed); // probably optional since array_is randomized; this may be redundant
                        $rand = '';
                        foreach (array_rand($seed, 10) as $k) $rand .= $seed[$k];								            
				        $new_acc_num =  $rand; 				    	

						$sql = "INSERT INTO users (fullname,acct_type,account_num,d_o_b,gender,country,currency,city,zipcode,ssn,phone,email,img,secquestion,sec_ans,pword,pin,status)
								VALUES ('$fullname','$acct_type','$new_acc_num','$d_o_b','$gender','$country','$currency','$city','$zipcode','$ssn','$phone','$email','$img_loc_main','$secquestion','$secans','$pword','$pin','inactive')";

						if(mysqli_query($con,$sql)){

							$_SESSION['chap'] = $email ;
							$cute = mysqli_query($con,"INSERT INTO code (user) VALUES ('$new_acc_num') ");

							if( $cute ){
						//	  include'../login/mail.php';
							  // include'alert2.php';
								echo "success";
							}
							else{
								echo "error";
							}
							    
						}
						else{
							echo mysqli_error($con);
						}
					}
					else {
				        echo "Signup error";
						exit();
				    }
				}								
			}
			else{
				echo "email error";
				exit();						
			}

	}
?>
