<?php
session_start();

require_once '../Config/Database.php';
require_once '../Model/ItemCategory.php';
$item_category = new ItemCategory();

$item_categories = $item_category->read_all_item_categories();

if(isset($_GET['all_item_categories'])){
    echo json_encode(
        array("data"=>$item_categories)
    ); 
}

if(isset($_POST['add_item_category'])){
    $item_category_name = $_POST['item_category_name'];
    $data = array(
        "item_category_name"=>$item_category_name
    );
    $success = $item_category->add_item_category($data);
    if($success){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}

if(isset($_POST['update_item_category'])){
    $item_category_id = $_POST['item_category_id'];
    $item_category_name = $_POST['item_category_name'];
    $data = array(
        "item_category_name"=>$item_category_name
    );
    $key = array(
        "item_category_id"=>$item_category_id
    );
    $success = $item_category->update_item_category($key,$data);
    if($success){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}


if(isset($_POST['remove_item_category'])){
    $item_category_id = $_POST['item_category_id'];
    $data = $item_category->remove_item_category($item_category_id);
    if(!$data){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}