<?php

require_once'inserts.php';
    
    //user 
$msg_loc = '../kyc/';

        echo ('all fields are required ');


// Check if image file is a actual image or fake image
if(isset($_POST['_token'])) {
    if(!empty($_FILES["kyc_file_front"]["tmp_name"]) && !empty($_FILES["kyc_file_back"]["tmp_name"])){

$target_dir = "upload/";
$target_file_front = $target_dir . basename($_FILES["kyc_file_front"]["name"]);
$target_file_back = $target_dir . basename($_FILES["kyc_file_back"]["name"]);
$uploadOk = 1;

$imageFileType_front = strtolower(pathinfo($target_file_front,PATHINFO_EXTENSION));
$imageFileType_back = strtolower(pathinfo($target_file_back,PATHINFO_EXTENSION));

  $check_front = getimagesize($_FILES["kyc_file_front"]["tmp_name"]);
  $check_back = getimagesize($_FILES["kyc_file_back"]["tmp_name"]);
  
  if(($check_front !== false) && ($check_back !== false) ) {
    // echo ("File is an image");
    $uploadOk = 1;
  } else {
    // echo ("File is not an image.");
    $uploadOk = 0;
  }

if (file_exists($target_file_front)) {
     note_msg('Sorry, file already exists',$msg_loc,2);

  $uploadOk = 0;
}else{
    echo 'aaa';
}

if (($_FILES["kyc_file_front"]["size"] > 1500000) && ($_FILES["kyc_file_back"]["size"] > 1500000) ) {

  note_msg('Sorry, your file is too large',$msg_loc,2);
 
  $uploadOk = 0;
}

if(($imageFileType_front != "jpg" && $imageFileType_front != "png" && $imageFileType_front != "jpeg") && ($imageFileType_back != "jpg" && $imageFileType_back != "png" && $imageFileType_back != "jpeg"
) ) {
  note_msg('Sorry, only JPG, JPEG, PNG  files are allowed',$msg_loc,2);

  $uploadOk = 0;
}
$file_f_name  = $infos['fname'].'_front'.time().'.'.$imageFileType_front;
$file_b_name  = $infos['fname'].'_back'.time().'.'.$imageFileType_back;

$target_file_front = $target_dir.$file_f_name;
$target_file_back = $target_dir.$file_b_name;

if ($uploadOk == 0) {
          note_msg('Sorry, your file was not uploaded',$msg_loc,2);


} else {
  if (move_uploaded_file($_FILES["kyc_file_front"]["tmp_name"], $target_file_front) && move_uploaded_file($_FILES["kyc_file_back"]["tmp_name"], $target_file_back) ) {
    
            $imgData = ($_FILES['kyc_file_front']['tmp_name']);
            $imgData2 = ($_FILES['kyc_file_back']['tmp_name']);
 
    $current_user_email=$mail;

    $front_file =$imgData;
    $front_file_name = $file_f_name;

    $back_file =$imgData2;
    $back_file_name = $file_b_name;
    
    $one=0;
    if($stmt_front -> execute()){
        $one = 1;
      }else{
          note_msg('An error occurred please try again',$msg_loc,2);
      }
    
      if($stmt_back -> execute()){
        $one = 1;
      }else{
          note_msg('An error occurred please try again',$msg_loc,2);
      }  
        if($one ==1){
        $new_kyc_no =1;
        $stmt_kyc -> execute();
                    note_msg("The ID successfully upload",$msg_loc,1);
        //<!--send mail to  user  -->
            // kyc_mail($mail);
        }
    
    
  } else {
  note_msg('Sorry, there was an error uploading your file.',$msg_loc,2);

    
  }
}


}else{
    // echo ('all fields are required ');
      note_msg('Sorry, all fields are required.',$msg_loc,2);

}
}else{
        // echo ('all fields are required ');

      note_msg('Sorry, all fields are required.',$msg_loc,2);
}
?>
    

