<?php
session_start();

require_once '../Config/Database.php';
require_once '../Model/ItemLevel.php';
$item_level = new ItemLevel();

$item_levels = $item_level->read_all_item_levels();

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

if(isset($_POST['add_item_level'])){
    $item_level_name = $_POST['item_level_name'];
    $data = array(
        "item_level_name"=>$item_level_name
    );
    $success = $item_level->add_item_level($data);
    if($success){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}

if(isset($_POST['update_item_level'])){
    $item_level_id = $_POST['item_level_id'];
    $item_level_name = $_POST['item_level_name'];
    $data = array(
        "item_level_name"=>$item_level_name
    );
    $key = array(
        "item_level_id"=>$item_level_id
    );
    $success = $item_level->update_item_level($key,$data);
    if($success){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}


if(isset($_POST['remove_item_level'])){
    $item_level_id = $_POST['item_level_id'];
    $data = $item_level->remove_item_level($item_level_id);
    if(!$data){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}