<?php

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


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

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


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

    $type = $_GET['type'];
    if ($debug && $type != "retrieve_specific_category_sub_categories" && $type != "retrieve_specific_category_child_categories") {
        return ;
    }
    if($type=="delete_category"){

        $id = $_GET['id'];

        $query="DELETE FROM Categories WHERE id = '$id'";
        $result = queryRunner($query);
        if($result!=null){
            echo $success_code;
        }
        else{
            echo $error_code;
        }


    }
    else if($type=="update_category_positions"){

        $id = $_GET['position_item_value'];
        $replaced_position_value = $_GET['replaced_position_value'];

        $query="UPDATE Categories SET position = '$replaced_position_value' WHERE id='$id' ";
        $result = queryRunner($query);
        if($result!=null){
            echo $success_code;
        }
        else{
            echo $error_code;
        }


    }
    else if($type=="update_specific_category"){

        ///var_dump($_POST);

        $id = $_POST['id'];
        $category_name = $_POST['category_name'];
        $category_picture = $_POST['category_picture'];
        $category_status = $_POST['category_status'];



        ////echo "Id = ".$id." Name = ".$category_name." Picture = ".$category_picture;

        $query="UPDATE Categories SET name = '$category_name' , enable = '$category_status' %s  WHERE id='$id' ";
        ///echo "Query 01 ".$query;

        if (!pathinfo($category_picture, PATHINFO_EXTENSION)) {

            $category_image_name=convert_save_image_from_base64("ic_category_picture".rand().".webp",$category_picture,$category_images);
            $query = sprintf($query," , image ='$category_image_name' ");

        }
        else{

            $query = sprintf($query," ");

        }


        //echo " Query 02 ".$query;

        $result = queryRunner($query);
        if($result!=null){
            echo $success_code;
        }
        else{
            echo $error_code;
        }


    }
    else if($type=="add_new_category"){




        $position = $_POST['position'];
        $category_name = $_POST['category_name'];
        $category_picture = $_POST['category_picture'];

        $category_image_name=convert_save_image_from_base64("ic_category_picture".rand().".webp",$category_picture,$category_images);
        $query="INSERT INTO Categories(name,image,position) VALUES('$category_name','$category_image_name','$position')";

        $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;
        }


    }

    /*Functionaality of Category End */

    /*Functionaality of subCategory Start */


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


        $category_id = $_POST['category_id'];
        $subcategory_name = $_POST['subcategory_name'];



        $query="INSERT INTO SubCategories(name,category_id) VALUES('$subcategory_name','$category_id')";

        $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=="delete_sub_category"){

        $id = $_GET['id'];

        $query="DELETE FROM SubCategories WHERE id = '$id'";
        $result = queryRunner($query);
        if($result!=null){
            echo $success_code;
        }
        else{
            echo $error_code;
        }


    }
    else if($type=="update_specific_subcategory"){

        ///var_dump($_POST);

        $id = $_POST['id'];
        $subcategory_name = $_POST['subcategory_name'];
        $subcategory_status = $_POST['subcategory_status'];



        ////echo "Id = ".$id." Name = ".$category_name." Picture = ".$category_picture;

        $query="UPDATE SubCategories SET name = '$subcategory_name' , enable = '$subcategory_status'  WHERE id='$id' ";


        $result = queryRunner($query);
        if($result!=null){
            echo $success_code;
        }
        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;
        }


    }

    /*Functionaality of subCategory End */

    /*Functionaality of Child Category Start */

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




        $position = $_POST['child_id'];
        $childcategory_name = $_POST['childcategory_name'];
        $category_picture = $_POST['childcategory_picture'];

        $category_image_name=convert_save_image_from_base64("ic_category_picture".rand().".webp",$category_picture,$category_images);
        $query="INSERT INTO ChildSubCategories(name,image,sub_category_id) VALUES('$childcategory_name','$category_image_name','$position')";

        $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=="delete_child_category"){

        $id = $_GET['id'];

        $query="DELETE FROM ChildSubCategories WHERE id = '$id'";
        $result = queryRunner($query);
        if($result!=null){
            echo $success_code;
        }
        else{
            echo $error_code;
        }


    }
    else if($type=="update_child_specific_category"){

        ///var_dump($_POST);

        $id = $_POST['id'];
        $category_name = $_POST['child_name'];
        $category_picture = $_POST['child_picture'];
        $category_status = $_POST['child_status'];



        ////echo "Id = ".$id." Name = ".$category_name." Picture = ".$category_picture;

        $query="UPDATE ChildSubCategories SET name = '$category_name' , enable = '$category_status' %s  WHERE id='$id' ";
        ///echo "Query 01 ".$query;

        if (!pathinfo($category_picture, PATHINFO_EXTENSION)) {

            $category_image_name=convert_save_image_from_base64("ic_category_picture".rand().".webp",$category_picture,$category_images);
            $query = sprintf($query," , image ='$category_image_name' ");

        }
        else{

            $query = sprintf($query," ");

        }


        //echo " Query 02 ".$query;

        $result = queryRunner($query);
        if($result!=null){
            echo $success_code;
        }
        else{
            echo $error_code;
        }


    }

    /*Functionaality of Child Category End */

    /*Functionaality of Brand Category Start */


    // else if($type== "retrieve_specific_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;
    //     }
    // }
    /*Functionaality of Brand Category End */
}
else{
    echo $unknown_code;
}




?>