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

		$title = mysqli_real_escape_string($con,$_POST['title']);
		$acc_num = mysqli_real_escape_string($con,$_POST['acc_num']);
		$f_name = mysqli_real_escape_string($con,$_POST['f_name']);
		$m_name = mysqli_real_escape_string($con,$_POST['m_name']);
		$l_name = mysqli_real_escape_string($con,$_POST['l_name']);
		$email = mysqli_real_escape_string($con,$_POST['email']);
		$phone = mysqli_real_escape_string($con,$_POST['phone']);
		$relationship = mysqli_real_escape_string($con,$_POST['relationship']);
		$d_o_b = mysqli_real_escape_string($con,$_POST['d_o_b']);
		$gender = mysqli_real_escape_string($con,$_POST['gender']);
		$address = mysqli_real_escape_string($con,$_POST['address']);
		$city = mysqli_real_escape_string($con,$_POST['city']);
		$state = mysqli_real_escape_string($con,$_POST['state']);
		$zip_code = mysqli_real_escape_string($con,$_POST['zip_code']);

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

			if( mysqli_num_rows($query) == 0 ){
			       
			     $pussy = mysqli_query($con,"SELECT email From beneficiary WHERE email = '$email' ") ; 
			    
                if( mysqli_num_rows($pussy) == 0 ){
                    
    				$target_dir = "../../e-banking/assets/images/beneficiary/".$email."/";
    				$img_loc = "assets/images/beneficiary/".$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"] > 500000) {
    				    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);
    				    if (move_uploaded_file($_FILES["profile-pic"]["tmp_name"], $target_file)) {
    
    
    						$sql = "INSERT INTO beneficiary (acc_num,title,f_name,m_name,l_name,email,relationship,phone,d_o_b,gender,address,city,state,zip_code,img)
    								VALUES ('$acc_num','$title','$f_name','$m_name','$l_name','$email','$relationship','$phone','$d_o_b','$gender','$address','$city','$state','$zip_code','$img_loc_main')";
    
    						if(mysqli_query($con,$sql)){
    							echo "success";
    						}
    						else{
    							echo mysqli_error($con);
    						}
    					}
    					else {
    				        echo "Signup error";
    						exit();
    				    }
    				}
                }
                else{
    				echo "email error";
    				exit();	
                }
			}
			else{
				echo "email error";
				exit();						
			}

	}
?>
