<?php
require_once($global['root-model']."Crud.php");
$obj_crud = new Crud();

require_once($global['root-model']."Role.php");
$obj_role = new Role();

require_once($global['root-model']."Access.php");
$obj_access = new Access();

if(!isset($_GET['action'])){

    $O_page = isset($_GET['page']) ? $_GET['page'] : 1;
    $datas = $obj_role->get_data($obj_crud, $O_page);
    $total_data = is_array($datas) ? $datas[0]['total_data_all'] : 0;
    $total_show = is_array($datas) ? $datas[0]['total_data'] : 0;
    $total_page = is_array($datas) ? $datas[0]['total_page'] : 0;

    $data_general = $obj_access->get_data_module($obj_crud, 'general');
    $data_dashboard = $obj_access->get_data_module($obj_crud, 'dashboard');
    $data_admin = $obj_access->get_data_module($obj_crud, 'admin');
    $data_po = $obj_access->get_data_module($obj_crud, 'po');
    $data_report = $obj_access->get_data_module($obj_crud, 'report');
    $data_client = $obj_access->get_data_module($obj_crud, 'client');
    $data_region = $obj_access->get_data_module($obj_crud, 'region');
    $data_unit = $obj_access->get_data_module($obj_crud, 'unit');
    $data_ppn = $obj_access->get_data_module($obj_crud, 'ppn');

    if(isset($_SESSION['status'])){
        $message = $_SESSION['status'];
        unset($_SESSION['status']);
    } else {
        $message = "";
    }

    if(isset($_SESSION['alert'])){
        $alert = $_SESSION['alert'];
        unset($_SESSION['alert']);
    } else {
        $alert = "";
    }


} else if(isset($_GET['action'])){

    if($_GET['action'] == "add"){

        $N_title = $obj_crud->escape_string(check_input($_POST['title']));
        $N_slug = strtolower(preg_replace('/\s+/', '_', $N_title));

        $result = $obj_role->insert_data($obj_crud, $N_title, $N_slug);
        if($result != 0){
            if(!empty($_POST['access'])) {
                foreach($_POST['access'] as $access) {
                    $res_access = $obj_access->insert_at($obj_crud, $result, $access);
                }
            }
            $message = "Admin Role <i><b>'" . $N_title . "'</b></i> Succesfully Created.";
            $_SESSION['alert'] = "success";
        }else{
            $message = "Admin Role <i><b>'" . $N_title . "'</b></i> Failed to Create.";
            $_SESSION['alert'] = "error";
        }


        $_SESSION['status'] = $message;
        header("Location:".$path['role']);

    } else if($_GET['action'] == "delete"){

        $O_id = $obj_crud->escape_string(check_input($_GET['id']));
        $O_title = $obj_crud->escape_string(check_input($_GET['title']));

        $result = $obj_role->delete_data($obj_crud, $O_id);
        if($result <= 0){
            $message = "Something is wrong while deleting the Data";
            $_SESSION['alert'] = "error";
        }else if($result == 1){
            $res_access = $obj_access->delete_at_by_role($obj_crud, $O_id);
            $message = "Admin Role <b><i>'" . $O_title . "'</i></b> has been deleted successfully.";
            $_SESSION['alert'] = "success";
        }

        $_SESSION['status'] = $message;
        header("Location:".$path['role']);
    }
}
?>