<?php
session_start();

require_once '../Config/Database.php';
require_once '../Model/Role.php';
$role = new Role();

$roles = $role->read_all_roles();

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

if(isset($_POST['add_role'])){
    $role_name = $_POST['role_name'];
    $data = array(
        "role_name"=>$role_name
    );
    $success = $role->add_role($data);
    if($success){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}

if(isset($_POST['update_role'])){
    $role_id = $_POST['role_id'];
    $role_name = $_POST['role_name'];
    $data = array(
        "role_name"=>$role_name
    );
    $key = array(
        "role_id"=>$role_id
    );
    $success = $role->update_role($key,$data);
    if($success){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}


if(isset($_POST['remove_role'])){
    $role_id = $_POST['role_id'];
    $data = $role->remove_role($role_id);
    if(!$data){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}