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

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_access->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;

    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_module = $obj_crud->escape_string(check_input($_POST['module']));
        $N_slug = $obj_crud->escape_string(check_input($_POST['slug']));
        //$N_slug = strtolower(preg_replace('/\s+/', '_', $N_title));


        $result = $obj_access->insert_data($obj_crud, $N_title, $N_module, $N_slug);
        if($result == 1){
            $message = "Role Access <i><b>'" . $N_title . "'</b></i> Succesfully Created.";
            $_SESSION['alert'] = "success";
        }else{
            $message = "Failed to Create Role Access <i><b>'" . $N_title . "'</b></i>.";
            $_SESSION['alert'] = "error";
        }


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

    } 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_access->delete_data($obj_crud, $O_id, $global['root']);
        if($result <= 0){
            $message = "Something is wrong while deleting the Data";
            $_SESSION['alert'] = "error";
        }else if($result == 1){
            $message = "Role Access <b><i>'" . $O_title . "'</i></b> has been deleted successfully.";
            $_SESSION['alert'] = "success";
        }

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