<?php

session_start();
if(empty($_SESSION['uid'])) {
    header("location:login.php");
}


include('../../config.php');

$resp=new Response();
$debug = $GLOBALS['debugging'];
$list_of_data= [];

if(isset($_GET['type'])) {

    $_POST = json_decode(file_get_contents('php://input'), true);

    $type = $_GET['type'];

    if($type=="retrieve_child_category") {

        $list_of_data = [];
        $childcategory_id = $_POST['childcategory_id'];
        $query="SELECT * FROM ChildSubCategories WHERE id IN ($childcategory_id)";
        // echo $query;
        $result = queryRunner($query);
        while ($row = rowRetriever($result)) {
        //  $pic_path = image_resize(, 40, 40);
        //  echo $pic_path;
               array_push($list_of_data, [
                    'id' => $row['id'] , 
                    'name' => $row['name'], 
                    'image'=> $category_images.$row['image'],
                    'enable' => $row['enable']
                ]);
        }
        if(count($list_of_data)>0){
            $resp -> code = $success_code;
            $resp -> message = "get data  ";
            $resp -> response=$list_of_data;
        }
        else{
            $resp -> code = $error_code;
            $resp -> message = "Error Insert Data ";
        }
        echo json_encode($resp);
      
    }

    else if($type=="update_specific_category"){
        
        ///var_dump($_POST);
        
        $id = $_POST['id'];
        $category_name = $_POST['category_name'];
        $category_status = $_POST['category_status'];

        
        
        ////echo "Id = ".$id." Name = ".$category_name." Picture = ".$category_picture;
        
        $query="UPDATE HomeFeaturedCategories SET name = '$category_name' , enable = '$category_status'  WHERE id='$id' ";
       
        
        $result = queryRunner($query);
        if($result!=null) {
            echo $success_code;
        }
        else{
            echo $error_code;
        }
        
        
    }

    else if($type=="delete_category"){
        
        $id = $_POST['id'];
        
        $query="DELETE FROM HomeFeaturedCategories WHERE id = '$id'";
        $result = queryRunner($query);
        if($result!=null){
            echo $success_code;
        }
        else{
            echo $error_code;
        }
        
        
    }

    else if($type=="add_new_category"){
      
        $category_name = $_POST['category_name'];
       
        $query="INSERT INTO HomeFeaturedCategories (name) VALUES('$category_name')";
        
        $result = queryRunner($query);
        if($result!=null){
            
            $resp -> code = $success_code;
            $resp -> message = "succesfully Insert Data ";
           array_push( $resp -> response,
            ['last_id' => findLastInsertId()  ]);
        }
        else{
            // echo $error_code;
            $resp -> code = $error_code;
            $resp -> message = "Error Insert Data ";

        }
        
        echo json_encode($resp);
    }

    else if($type=="retrieve_specific_category_sub_categories"){

        $list_of_data = [];
        $category_id = $_POST['category_id'];
        
        $query="SELECT * FROM SubCategories WHERE category_id='$category_id' ";
        $result = queryRunner($query);
        while ($row = rowRetriever($result)) {
               array_push($list_of_data, [
                    'id' => $row['id'] , 
                    'name' => $row['name'], 
                    'enable' => $row['enable']
                ]);
        }
        if(count($list_of_data)>0){
            echo json_encode($list_of_data);
        }
        else{
            echo $error_code;
        }
        
        
    }

    else if($type=="retrieve_specific_category_child_categories"){

        $list_of_data = [];
        $subcategory_id = $_POST['subcategory_id'];
        
        $query="SELECT * FROM ChildSubCategories WHERE sub_category_id='$subcategory_id' ";
        $result = queryRunner($query);
        while ($row = rowRetriever($result)) {
        //  $pic_path = image_resize(, 40, 40);
        //  echo $pic_path;
               array_push($list_of_data, [
                    'id' => $row['id'] , 
                    'name' => $row['name'], 
                    'image'=> $category_images.$row['image'],
                    'enable' => $row['enable']
                ]);
        }
        if(count($list_of_data)>0){
            echo json_encode($list_of_data);
        }
        else{
            echo $error_code;
        }
        
        
    }

    else if($type=="retrieve_child_category_id"){

        $list_of_data = [];
        $childcategory_id = $_POST['childcategory_id'];
        
        $query="SELECT * FROM ChildSubCategories WHERE id='$childcategory_id' ";
        $result = queryRunner($query);
        while ($row = rowRetriever($result)) {
        //  $pic_path = image_resize(, 40, 40);
        //  echo $pic_path;
               array_push($list_of_data, [
                    'id' => $row['id'] , 
                    'name' => $row['name'], 
                    'image'=> $category_images.$row['image'],
                    'enable' => $row['enable']
                ]);
        }
        if(count($list_of_data)>0){
            echo json_encode($list_of_data);
        }
        else{
            echo $error_code;
        }
        
        
    }

    else if($type=="addsub_child_category"){
        
        ///var_dump($_POST);
        
        $id = $_POST['childcategory_id'];
        $subcategory_name = $_POST['child_sub_category'];
        

        
        
        ////echo "Id = ".$id." Name = ".$category_name." Picture = ".$category_picture;
        
        $query="UPDATE HomeFeaturedCategories SET child_sub_categories = '$subcategory_name'   WHERE id='$id' ";
       
        
        $result = queryRunner($query);
        if($result!=null) {
            echo $success_code;
        }
        else{
            echo $error_code;
        }
        
        
    }
  

}
else {

}