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

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

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_region->get_data_list($obj_crud, $O_page, "20");
    $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;

    $acsreg_create = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'create_region');
    $acsreg_edit = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'edit_region');
    $acsreg_delete = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'delete_region');

    if($acsg_region != 1){
        header("Location:".$path['dashboard']);
    }

    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']));
        $title = strtolower($N_title);

        $result = $obj_region->insert_data($obj_crud, $N_title);
        if($result != 0){
            $message = "Region <i><b>'" . $N_title . "'</b></i> Succesfully Created.";
            $_SESSION['alert'] = "success";
        }else{
            $message = "Region <i><b>'" . $N_title . "'</b></i> Failed to Create.";
            $_SESSION['alert'] = "error";
        }

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

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

        $_SESSION['status'] = $message;
        header("Location:".$path['region']);
    } else if($_GET['action'] == "edit"){

        $N_item_id = $obj_crud->escape_string(check_input($_POST['item_id']));
        $N_title = $obj_crud->escape_string(check_input($_POST['title']));
        $title = strtolower($N_title);

        $result = $obj_region->update_data($obj_crud, $N_item_id, $N_title);
        if($result != 0){
            $message = "Region <i><b>'" . $N_title . "'</b></i> Succesfully Edited.";
            $_SESSION['alert'] = "success";
        }else{
            $message = "Region <i><b>'" . $N_title . "'</b></i> Failed to Edited.";
            $_SESSION['alert'] = "error";
        }

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

    }
}
?>