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

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

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

    $acsun_create = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'create_unit');
    $acsun_edit = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'edit_unit');
    $acsun_delete = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'delete_unit');

    if($acsg_unit != 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_unit->insert_data($obj_crud, $title);
        if($result != 0){
            $message = "Unit <i><b>'" . $N_title . "'</b></i> Succesfully Created.";
            $_SESSION['alert'] = "success";
        }else{
            $message = "Unit <i><b>'" . $N_title . "'</b></i> Failed to Create.";
            $_SESSION['alert'] = "error";
        }

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

    } 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_unit->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 = "Unit <b><i>'" . $O_title . "'</i></b> has been deleted successfully.";
            $_SESSION['alert'] = "success";
        }

        $_SESSION['status'] = $message;
        header("Location:".$path['unit']);
    } 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_unit->update_data($obj_crud, $N_item_id, $title);
        if($result != 0){
            $message = "Unit <i><b>'" . $N_title . "'</b></i> Succesfully Edited.";
            $_SESSION['alert'] = "success";
        }else{
            $message = "Unit <i><b>'" . $N_title . "'</b></i> Failed to Edited.";
            $_SESSION['alert'] = "error";
        }

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

    }
}
?>