<?php
include("../../connection/Functions.php");
$operation = new Functions();



//add slider image
if(isset($_FILES['file_profile']) && isset($_POST['profile_name']) && !empty($_POST['profile_name']) ){
    
    $caption1 = addslashes($_POST['profile_name']);

    $images = $_FILES['file_profile']['name'];
    $image=strtolower(pathinfo($images,PATHINFO_EXTENSION));
    $filename=rand(1000, 1000000).".".$image;
    /* Location */
    $location = "../../work_profiles/".$filename;
    $uploadOk = 1;
    $imageFileType = pathinfo($location,PATHINFO_EXTENSION);
    /* Valid Extensions */
    $valid_extensions = array("pdf");
    /* Check file extension */
    if( !in_array(strtolower($imageFileType),$valid_extensions) ) {
       $uploadOk = 0;
    }
    
    
    if($_FILES['file_profile']['size'] > 30000000 ){
        echo json_encode(array("code"=>2,"msg"=>"✖ File must be less than 30mb!"));
        die();
    }
    
    
    if($uploadOk == 0){
         echo json_encode(array("code"=>2,"msg"=>"✖ File type not supported, try jpg, jpeg or png!"));
    }else{
       /* Upload file */
       if(move_uploaded_file($_FILES['file_profile']['tmp_name'],$location)){
            
               $table = "profiles";
               $data = [
                   'profile_name'=>"$caption1",
                   'file_url' => "$filename"  
               ];               
               if($operation->insertData($table,$data) == 1){
                   echo json_encode(array("code"=>1,"msg"=>"Success, refreshing, please wait!"));
               }else{
//                   echo 0;
                   echo json_encode(array("code"=>2,"msg"=>"✖ An error occured while saving, please try again later!"));
               }
               
           
          
       }else{
          echo json_encode(array("code"=>2,"msg"=>"✖ An error occured while saving the picture!"));
       }
    }
    //edit slider image
}elseif(isset($_POST['eprofile_name']) && isset($_POST['profile_id']) && !empty($_POST['eprofile_name']) && !empty($_POST['profile_id']) ){
    
    $ecaption2 = addslashes($_POST['eprofile_name']);
    
    $slider_id = addslashes($_POST['profile_id']);

    $table = "profiles";
    $where = "profile_id = '$slider_id'";
//check if picture was attached
    
    if(file_exists($_FILES['eimage']['tmp_name']) || is_uploaded_file($_FILES['eimage']['tmp_name'])){
        $images = $_FILES['eimage']['name'];
        
        $size =  $_FILES['eimage']['size'];
        if($size > 30000000){
             echo json_encode(array("code"=>2,"msg"=>"✖ File should be less than or equal to 30mb"));
            die();
        }else{
        $image=strtolower(pathinfo($images,PATHINFO_EXTENSION));
        $filename=rand(1000, 1000000).".".$image;
        /* Location */
        $location = "../../work_profiles/".$filename;
        $uploadOk = 1;
        $imageFileType = pathinfo($location,PATHINFO_EXTENSION);

        /* Valid Extensions */
        $valid_extensions = array("jpg","jpeg","png");
        /* Check file extension */
        if( !in_array(strtolower($imageFileType),$valid_extensions) ) {
           $uploadOk = 0;
        }
    

        if($uploadOk == 0){
             echo json_encode(array("code"=>2,"msg"=>"✖ File type not supported, try jpg, jpeg or png!"));
        }else{
            //get the image sAVED earlier
            $checkImg = $operation->retrieveSingle("SELECT *FROM profiles WHERE profile_id = '$slider_id'");
              $directory = "../../images/".$checkImg['img_url'];
            //delete previous picture
               if(unlink($directory)){
                            /* Upload file */
                    if(move_uploaded_file($_FILES['eimage']['tmp_name'],$location)){               
                       $data = [
                            'profile_name'=>"$ecaption2",
                           'file_url' => "$filename"  
                       ];  

                       if($operation->updateData($table,$data,$where) == 1){
        //                   echo $filename;
                           echo json_encode(array("code"=>1,"msg"=>"Success, redirecting, please wait!"));
                       }else{
        //                   echo 0;
                           echo json_encode(array("code"=>2,"msg"=>"✖ An error occured while saving, please try again later!"));
                       }
               

                    }else{
                         echo json_encode(array("code"=>2,"msg"=>"✖ An error occured while saving, please try again later!"));
                    }
               }else{
                   echo json_encode(array("code"=>2,"msg"=>"✖ An error occured while saving, please try again later!"));
               }
            }
        }
        

    }else{
        $data = [
           'profile_name'=>"$ecaption2",
       ]; 

       if($operation->updateData($table,$data,$where) == 1){
//                   echo $filename;
           echo json_encode(array("code"=>1,"msg"=>"Success, refreshing, please wait!"));
       }else{
//                   echo 0;
           echo json_encode(array("code"=>2,"msg"=>"✖ An error occured while saving, please try again later!"));
       }
    }
    //delete meal
}elseif(isset($_POST['id']) && !empty($_POST['id'])){
    $id = addslashes($_POST['id']);
    $where = "profile_id = '$id'";
    $table = "profiles";
    //get image name
    $checkImg = $operation->retrieveSingle("SELECT *FROM profiles WHERE profile_id = '$id'");
    
    $directory = "../../work_profiles/".$checkImg['file_url'];
   if(unlink($directory)){
        if($operation->deleteData($table,$where) == 1){
            echo json_encode(array("code"=>1,"msg"=>"Success, refreshing, please wait..."));
        }else{
            echo json_encode(array("code"=>2,"msg"=>"An error occured, please try again later"));
        }
   }else{
        echo json_encode(array("code"=>2,"msg"=>"✖ An error occured while saving the picture!"));
   }
    
    
    
}


?>