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



//add team member
if(isset($_FILES['picture']) && isset($_POST['fullname']) && isset($_POST['position']) && isset($_POST['background']) && !empty($_POST['fullname']) && !empty($_POST['position']) && !empty($_POST['background'])){
    
    $fullname = addslashes($_POST['fullname']);
    $position = addslashes($_POST['position']);
    $background = addslashes($_POST['background']);

    $images = $_FILES['picture']['name'];
    $image=strtolower(pathinfo($images,PATHINFO_EXTENSION));
    $filename=rand(1000, 1000000).".".$image;
    /* Location */
    $location = "../../images/team/".$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'] > 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['picture']['tmp_name'],$location)){
           
           //check if portfolio was attached
           $table = "team";
            if($_FILES['portfolio']['size'] != 0){
                $images2 = $_FILES['portfolio']['name'];
                $image2=strtolower(pathinfo($images2,PATHINFO_EXTENSION));
                $filename2=rand(1000, 1000000).".".$image2;
                /* Location */
                $location2 = "../../images/team/".$filename2;
                $uploadOk2 = 1;
                $imageFileType2 = pathinfo($location2,PATHINFO_EXTENSION);
                /* Valid Extensions */
                $valid_extensions2 = array("pdf");
                /* Check file extension */
                if( !in_array(strtolower($imageFileType2),$valid_extensions2) ) {
                   $uploadOk2 = 0;
                }

                if($_FILES['portfolio']['size'] > 50000000 ){
                    echo json_encode(array("code"=>2,"msg"=>"✖ File must be less than 50mb!"));
                    die();
                }
                
                if($uploadOk2 == 0){
                     echo json_encode(array("code"=>2,"msg"=>"✖ File type not supported, try pdf for portfolio"));
                }else{
                    /* Upload file */
                    if(move_uploaded_file($_FILES['portfolio']['tmp_name'],$location2)){
                        $data = [
                           'fullname'=>"$fullname",
                           'position'=>"$position",
                           'portfolio'=>"$filename2",
                           'background'=>"$background",
                           '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{
                $data = [
                   'fullname'=>"$fullname",
                   'position'=>"$position",
                   'background'=>"$background",
                   '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 !"));
       }
    }
    //edit team member
}elseif(isset($_POST['eposition']) && isset($_POST['efullname']) && isset($_POST['eteam_id']) && isset($_POST['ebackground']) && !empty($_POST['eposition']) && !empty($_POST['efullname']) && !empty($_POST['eteam_id']) && !empty($_POST['ebackground'])){
    
    $fullname = addslashes($_POST['efullname']);
    $position = addslashes($_POST['eposition']);
    $background = addslashes($_POST['ebackground']);
    
    $team_id = addslashes($_POST['eteam_id']);

    $table = "team";
    $where = "team_id = '$team_id'";
    
    $data = [
      'fullname'=>"$fullname",
        'position'=>"$position",
        'background'=>"$background"
    ];
    
    if($operation->updateData($table,$data,$where) == 1){
         echo json_encode(array("code"=>1,"msg"=>"Success, refreshing, please wait..."));
    }else{
         echo json_encode(array("code"=>2,"msg"=>"Updating failed, try again later!"));
    }

    //delete member
}elseif(isset($_POST['id']) && !empty($_POST['id'])){
    $id = addslashes($_POST['id']);
    $where = "team_id = '$id'";
    $table = "team";
    //get image name
    $checkImg = $operation->retrieveSingle("SELECT *FROM team WHERE team_id = '$id'");
    
    $directory = "../../images/team/".$checkImg['img_url'];
   if(unlink($directory)){
       
         //check if portfolio was uploaded
            if($checkImg['portfolio'] != "" && $checkImg['portfolio'] != NULL){
                $directory2 = "../../images/team/".$checkImg['portfolio'];
                if(unlink($directory2)){
                    
                }
            }
       
        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!"));
   }
    
    
    //change team picture
}elseif(isset($_POST['pic_id']) && !empty($_POST['pic_id'])){
    $id = addslashes($_POST['pic_id']);
    $where = "team_id = '$id'";
    $table = "team";
    //get image name
    $checkImg = $operation->retrieveSingle("SELECT *FROM team WHERE team_id = '$id'");
    
    $directory = "../../images/team/".$checkImg['img_url'];
   if(unlink($directory)){
       
       $images = $_FILES['epicture']['name'];
        $image=strtolower(pathinfo($images,PATHINFO_EXTENSION));
        $filename=rand(1000, 1000000).".".$image;
        /* Location */
        $location = "../../images/team/".$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['epicture']['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{
        $data = [
            'img_url'=>"$filename"
        ];
       /* Upload file */
       if(move_uploaded_file($_FILES['epicture']['tmp_name'],$location)){
            if($operation->updateData($table,$data,$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"));
            }
       }
       
       
       
       
       
       
   }
    
    
    
}
    //change portfolio
}elseif(isset($_POST['port_teamid']) && !empty($_POST['port_teamid'])){
    $id = addslashes($_POST['port_teamid']);
    $where = "team_id = '$id'";
    $table = "team";
    //get image name
    $checkImg = $operation->retrieveSingle("SELECT *FROM team WHERE team_id = '$id'");
    
    //check previous photo
    if($checkImg['portfolio'] != "" && $checkImg['portfolio'] != NULL){
        $directory = "../../images/team/".$checkImg['portfolio'];
       if(unlink($directory)){
           
           //check if uploaded file now
           if($_FILES['eportfolio']['size'] == 0 ){
               $dd = "";
               $data = [
                   'portfolio'=>"$dd"
               ];
               if($operation->updateData($table,$data,$where)==1){
                   echo json_encode(array("code"=>1,"msg"=>"File removed!"));
                   die();
               }else{
                   echo json_encode(array("code"=>2,"msg"=>"File not removed!"));
                   die();
               }
                
           }
           
           $images = $_FILES['eportfolio']['name'];
            $image=strtolower(pathinfo($images,PATHINFO_EXTENSION));
            $filename=rand(1000, 1000000).".".$image;
            /* Location */
            $location = "../../images/team/".$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['eportfolio']['size'] > 50000000 ){
            echo json_encode(array("code"=>2,"msg"=>"✖ File must be less than 50mb!"));
            die();
        }


        if($uploadOk == 0){
             echo json_encode(array("code"=>2,"msg"=>"✖ File type not supported, try pdf!"));
        }else{
            $data = [
                'portfolio'=>"$filename"
            ];
           /* Upload file */
           if(move_uploaded_file($_FILES['eportfolio']['tmp_name'],$location)){
                if($operation->updateData($table,$data,$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{
                   //check if uploaded file now
           if($_FILES['eportfolio']['size'] == 0 ){
                echo json_encode(array("code"=>2,"msg"=>"You did not upload portfolio earlier, now attach one!"));
               die();
           }
           
           $images = $_FILES['eportfolio']['name'];
            $image=strtolower(pathinfo($images,PATHINFO_EXTENSION));
            $filename=rand(1000, 1000000).".".$image;
            /* Location */
            $location = "../../images/team/".$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['eportfolio']['size'] > 50000000 ){
            echo json_encode(array("code"=>2,"msg"=>"✖ File must be less than 50mb!"));
            die();
        }


        if($uploadOk == 0){
             echo json_encode(array("code"=>2,"msg"=>"✖ File type not supported, try pdf!"));
        }else{
            $data = [
                'portfolio'=>"$filename"
            ];
           /* Upload file */
           if(move_uploaded_file($_FILES['eportfolio']['tmp_name'],$location)){
                if($operation->updateData($table,$data,$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"));
                }
           }






       }

    }
    
    
    
}


?>