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



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

    $images = $_FILES['picture']['name'];
    $image=strtolower(pathinfo($images,PATHINFO_EXTENSION));
    $filename=rand(1000, 1000000).".".$image;
    /* Location */
    $location = "../../images/".$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($_FILES['picture']['size'] > 3000000 ){
        echo json_encode(array("code"=>2,"msg"=>"✖ File must be less than 3mb!"));
        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['picture']['tmp_name'],$location)){
            
               $table = "corousel";
               $data = [
                   'caption1'=>"$caption1",
                   'caption2'=>"$caption2",
                   'img_url' => "$filename"  
               ];               
               if($operation->insertData($table,$data) == 1){
                   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 the picture!"));
       }
    }
    //edit slider image
}elseif(isset($_POST['ecaption1']) && isset($_POST['ecaption2']) && isset($_POST['slider_id']) && !empty($_POST['ecaption1']) && !empty($_POST['ecaption2']) && !empty($_POST['slider_id']) ){
    
    $ecaption2 = addslashes($_POST['ecaption2']);
    $ecaption1 = addslashes($_POST['ecaption1']);
    
    $slider_id = addslashes($_POST['slider_id']);

    $table = "corousel";
    $where = "carousel_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 > 3000000){
             echo json_encode(array("code"=>2,"msg"=>"✖ File should be less than or equal to 3mb"));
            die();
        }else{
        $image=strtolower(pathinfo($images,PATHINFO_EXTENSION));
        $filename=rand(1000, 1000000).".".$image;
        /* Location */
        $location = "../../images/".$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 corousel WHERE carousel_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 = [
                           'caption1'=>"$ecaption1",
                            'caption2'=>"$ecaption2",
                           'img_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 = [
           'caption1'=>"$ecaption1",
           'caption2'=>"$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 = "carousel_id = '$id'";
    $table = "corousel";
    //get image name
    $checkImg = $operation->retrieveSingle("SELECT *FROM corousel WHERE carousel_id = '$id'");
    
    $directory = "../../images/".$checkImg['img_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!"));
   }
    
    
    
}


?>