<?php
session_start();

require_once '../Config/Database.php';
require_once '../Model/SystemGroup.php';
$system_group = new SystemGroup();

$system_groups = $system_group->read_all_system_groups();

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

if(isset($_POST['add_system_group'])){
    $system_group_name = $_POST['system_group_name'];
    $system_group_icon = $_POST['system_group_icon'];
    $data = array(
        "system_group_name"=>$system_group_name,
        "system_group_icon"=>$system_group_icon
    );
    $success = $system_group->add_system_group($data);
    if($success){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}

if(isset($_POST['update_system_group'])){
    $system_group_id = $_POST['system_group_id'];
    $system_group_name = $_POST['system_group_name'];
    $system_group_icon = $_POST['system_group_icon'];
    $data = array(
        "system_group_name"=>$system_group_name,
        "system_group_icon"=>$system_group_icon
    );
    $key = array(
        "system_group_id"=>$system_group_id
    );
    $success = $system_group->update_system_group($key,$data);
    if($success){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}


if(isset($_POST['remove_system_group'])){
    $system_group_id = $_POST['system_group_id'];
    $data = $system_group->remove_system_group($system_group_id);
    if(!$data){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}