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

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

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

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

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

$acsa_create_admin = $obj_access->check_access($obj_crud,$_SESSION['admin_id'],'create_new_admin');
$acsa_activate_admin = $obj_access->check_access($obj_crud,$_SESSION['admin_id'],'deactivate_other_admin');
$acsa_edit_admin = $obj_access->check_access($obj_crud,$_SESSION['admin_id'],'edit_other_admin');
$acsa_change_role = $obj_access->check_access($obj_crud,$_SESSION['admin_id'],'change_role_admin');
$acsa_mail_admin_register = $obj_access->check_access($obj_crud,$_SESSION['admin_id'],'mail_new_admin_register');
$acsa_pic = $obj_access->check_access($obj_crud,$_SESSION['admin_id'],'edit_pic');

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

    if($acsg_admin == 0){
        header("Location:".$path['dashboard']);
    }

    $O_page = isset($_GET['page']) ? $_GET['page'] : 1;
    $datas = $obj_user->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_region = $obj_region->get_data_select($obj_crud);

    $tokens = $obj_user->generate_token();

    $data_role = $obj_role->get_select_list($obj_crud);

    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_email = $obj_crud->escape_string(check_input($_POST['email']));
        $N_username = $obj_crud->escape_string(check_input($_POST['username']));
        $N_role = $obj_crud->escape_string(check_input($_POST['role']));
        $N_pic = $obj_crud->escape_string(check_input($_POST['pic']));
        $N_adminc = $obj_crud->escape_string(check_input($_POST['adminc']));
        $N_region = $obj_crud->escape_string(check_input($_POST['region']));
        $tokens = $obj_user->generate_token();

        $check_email = $obj_user->check_email($obj_crud, $N_email);
        if($check_email == 0){
            $result = $obj_user->create_data_admin($obj_crud, $N_email, $N_username, $N_role, $tokens, $N_pic, $N_adminc, $N_region);
            if($result != 0){
                //send mail with token
                $activation_url = $path['login-activate']."?token=".$tokens;
                $body_message = $obj_mail->mail_create_user($global['logo-url'], $N_username, $activation_url);
                $subject_message = "Ruas Management System Account Register";
                $sent_customer = smtp_mail($N_email, $smtp['url'], $smtp['from'], $smtp['password'], $smtp['from_name'], $subject_message, $body_message, $smtp['reply-cs']);

                //send email new admin
                if($acsa_mail_admin_register == 1){
                    $subject_message_reg = "New Admin Register - Ruas Management System";
                    $body_message_reg = $obj_mail->new_admin_registered($global['logo-url'], $N_username, $N_email);

                    $data_admins = $obj_user->get_list_email_v2($obj_crud, 'mail_new_admin_register');
                    if(is_array($data_admins)){
                        $emails = explode(",", $data_admins[0]['emails']);
                        $sent_customer = smtp_mail($emails, $smtp['url'], $smtp['from'], $smtp['password'], $smtp['from_name'], $subject_message_reg, $body_message_reg, $smtp['reply-cs']);
                    }
                }

                $message = "Admin <i><b>'" . $N_username . "'</b></i> succesfully created.";
                $_SESSION['alert'] = "success";
            } else {
                $message = "Admin <i><b>'" . $N_username . "'</b></i> registration failed.";
                $_SESSION['alert'] = "error";
            }
        } else {
            $message = "Email already exist in system.";
            $_SESSION['alert'] = "error";
        }

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

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

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

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

        $result = $obj_user->change_active($obj_crud, $O_id, 'inactive');
        if($result <= 0){
            $message = "Something is wrong while deactive the account";
            $_SESSION['alert'] = "error";
        }else if($result == 1){
            $message = "Admin <b><i>'" . $O_title . "'</i></b> has been deactive successfully.";
            $_SESSION['alert'] = "success";
        }

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

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

        $result = $obj_user->change_active($obj_crud, $O_id, 'active');
        if($result <= 0){
            $message = "Something is wrong while activating the account";
            $_SESSION['alert'] = "error";
        }else if($result == 1){
            $message = "Admin <b><i>'" . $O_title . "'</i></b> has been activate successfully.";
            $_SESSION['alert'] = "success";
        }

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

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

        $detail = $obj_user->get_data_detail($obj_crud, $O_id);
        if(is_array($detail)){
            $N_email = $detail[0]['admin_email'];
            $result = $obj_user->update_token($obj_crud, $O_id, $tokens);
            //send mail with token
            $activation_url = $path['login-activate']."?token=".$tokens;
            $body_message = $obj_mail->mail_create_user($global['logo-url'], $O_title, $activation_url);
            $subject_message = "Ruas Management System Account Register";
            $sent_customer = smtp_mail($N_email, $smtp['url'], $smtp['from'], $smtp['password'], $smtp['from_name'], $subject_message, $body_message, $smtp['reply-cs']);  
            $message = "Successfully send activating mail.";
            $_SESSION['alert'] = "success";

        } else {
            $message = "Something is wrong while resend activating mail";
            $_SESSION['alert'] = "error";
        }

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