<?php

/*************** 
ERROR_CODE -> 0 - Success / 1 - Error
MESSAGE -> ""
 ***************/
require("../../config.php");
require("../classes/db_class.php");
require("../classes/comman_class.php");
require("../classes/user_class.php");
require("../classes/jwt_encode_decode.php");

$helpers = new Helper_class();
$mysqlClass = new Mysql_class();
$userClass = new user_class();
$jwtED = new jwt_encode_decode();


if ($_SERVER['HTTP_X_API_KEY'] == HTTP_X_API_KEY && $_SERVER['HTTP_DGFNAPASSKEY'] == DGFNAPASSKEY) {
} else {
    $helpers->errorResponse("Authorization Invalid !");
}

$post = $helpers->clearSlashes($_POST);

if (isset($post) && !empty($post)) {
    if (isset($post['token'])) {
        $res = $jwtED->decode_token($post['token']);
        if (isset($res->USER_ID) && $res->USER_ID > 0) {
            $USER_ID     = $res->USER_ID;
            $CREATOR_ID    = $res->CREATOR_ID;
            $WL_ID         = $res->WL_ID;
            $ADMIN_ID     = $res->ADMIN_ID;
        } else {
            $helpers->errorResponse("Token Expire");
        }
    } else {
        $helpers->errorResponse("Token not set!");
    }

    if(empty($post['cName'])){
        $helpers->errorResponse("Name is required.");
    }

   if(empty($post['cMobile'])){
        $helpers->errorResponse("Mobile number is required.");
    }elseif(!preg_match('/^[0-9]{10}+$/', $post['cMobile'])){
            $helpers->errorResponse("Please enter valid 10 digits and space is not allowed.");  
        }

   if(empty($post['loanType'])){
        $helpers->errorResponse("Loan type is required.");
    }

   if(empty($post['loanAmount'])){
        $helpers->errorResponse("Loan amount is required.");
    }elseif(!is_numeric($post['loanAmount'])){
        $helpers->errorResponse("Loan amount should be numeric."); 
    } 

    if($_FILES['address_proof']['name']=''){
     $helpers->errorResponse("Please upload address proof.");    
    }

    if($_FILES['occupation_proof']['name']=''){
     $helpers->errorResponse("Please upload occupation proof.");    
    }
  

   $addressProofIType  = $_FILES['address_proof']['type'];
   $occupationProofIType  = $_FILES['occupation_proof']['type'];

    ################ Upload File ####################################

    #### Attachedform-1 ####
    $upload_path = '../../uploads/loan/'; // set folder path to upload a picture
    $file_upload_1 = 0;
    $file_upload_2 = 0;
    //$valid_extensions = array('pdf'); 
    if($addressProofIType =="image/png" || $addressProofIType =="image/jpeg" ||  $addressProofIType =="image/jpg"){

    $file_name_1 = $_FILES['address_proof']['name'];
    $file1 = explode('/', $addressProofIType);
    $new_file1 = $file1[0] . time() . '1.' . $file1[1];
    $tmp_name_1 = $_FILES['address_proof']['tmp_name'];
    $size_1 = $_FILES['address_proof']['size'];

    #### Attachedform-2 #### 
    if ($size_1 <= 5000000) {
        move_uploaded_file($tmp_name_1, $upload_path . $new_file1);
        $file_upload_1 = 1;
    } else {
         $helpers->errorResponse("Address Image is too large. upload upto 5 MB.");
    }

    }else{
       $helpers->errorResponse("Address proof should be jpg, jpeg and png.");  
    }

if($occupationProofIType =="image/png" || $addressProofIType =="image/jpeg" ||  $addressProofIType =="image/jpg"){
    $file_name_2 = $_FILES['occupation_proof']['name'];
    $file2 = explode('/', $occupationProofIType);
    $new_file2 = $file2[0] . time() . '2.' . $file2[1];
    $tmp_name_2 = $_FILES['occupation_proof']['tmp_name'];
    $size_2 = $_FILES['occupation_proof']['size'];

    if ($size_2 <= 5000000) {
        move_uploaded_file($tmp_name_2, $upload_path . $new_file2);
        $file_upload_2 = 1;
    } else {
       $helpers->errorResponse("Occupation Image is too large. upload upto 5 MB.");
    }
   }else{
    $helpers->errorResponse("Occupation proof should be jpg, jpeg and png."); 
   } 

    ################### End File uploads ##############################

    if ($file_upload_2 == 1 &&  $file_upload_1 == 1) {

      $data = array(
                    "retailer_id"=> $USER_ID,
                    "cName"=>$post['cName'],
                    "cEmail"=>$post['cEmail'],
                    "cMobile"=>$post['cMobile'],
                    "loanType"=>$post['loanType'],
                    "loanSubType"=>$post['subLoanType'],
                    "loanAmount"=>$post['loanAmount'],
                    "cAddress"=>$post['cAddress'],
                    "pincode "=>$post['cPincode'],
                    "state"=>$post['cState'],
                    "district"=>$post['cDistrict'],
                    "addressProof"=>$new_file1,
                    "occupationProof"=>$new_file2,
                    "cOccupation "=>$post['cOccupation'],
                    "other"=>$post['otherOccupation'],
                    "uploadedAPFile "=>$post['document'],
                    "uploadedOpFile"=>$post['occupation'],
                    "created_at"=>date("Y-m-d G:i:s")
                );  
    $id =  $mysqlClass->insertData('loan_applications',$data);
    $prefix = "LA";
    $application_id =  sprintf("%s%05s", $prefix, $id);
   
    $uData = array("application_id"=>$application_id);
    $query = "UPDATE loan_applications SET application_id ='".$application_id."' WHERE id='".$id."'";
    $mysqlClass->mysqlQuery($query);

    $response['ERROR_CODE'] = 0;
    $response['APPLICATION_ID'] = $application_id;
    $response['MESSAGE'] = "Request has saved sucessfully.";  
       
    } else {
        $helpers->errorResponse("Problem in file upload.");
    }
    echo json_encode($response);
}
// }
