<?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']."Region.php");
$obj_region = new Region();

if(!isset($_GET['action']) && $_GET['id'] != ""){

    $O_id = $obj_crud->escape_string(check_input($_GET['id']));
    $data = $obj_user->get_data_detail($obj_crud, $O_id);

    $data_role = $obj_role->get_select_list($obj_crud);
    $data_region = $obj_region->get_data_select($obj_crud);

    $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_pic = $obj_access->check_access($obj_crud,$_SESSION['admin_id'],'edit_pic');
    
    if($acsa_change_role == 0){
        $disable_role = "disable-input2";
    } else {
        $disable_role = "";
    }
    if($acsa_edit_admin == 0){
        if($_SESSION['admin_id'] != $O_id){
            header("Location:".$path['admin']);
        }
    }

    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'] == "edit"){

        $N_admin_id = $obj_crud->escape_string(check_input($_POST['admin_id']));
        $N_old_email = $obj_crud->escape_string(check_input($_POST['old_email']));
        $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']));
        $img_link = "";
        $img_thmblink  = "";

        if($_FILES["image"]["name"]!=null){
            $ran = rand();
            $timestamp = time();
            $img_link = "uploads/admin/".$timestamp.$ran.cleanSpace($_FILES["image"]["name"]);
            $img_thmblink  = "uploads/admin-thmb/" .$timestamp.$ran.cleanSpace($_FILES["image"]["name"]);

            if ((($_FILES["image"]["type"] == "image/gif")|| ($_FILES["image"]["type"] == "image/jpeg")|| ($_FILES["image"]["type"] == "image/jpg")||($_FILES["image"]["type"] == "image/JPG")||($_FILES["image"]["type"] == "image/png")) && ($_FILES["image"]["size"] < 10048576) && ($_FILES["image"]["name"] != "")){
                require_once("../../packages/imageProcessor.php");
                if ($_FILES["image"]["error"] > 0){
                    //nothing
                } else {
                    $file = $_FILES["image"]["tmp_name"];

                    //Resizing images
                    if (true !== ($pic_error = @resampimage(1920,1028,"$file","../../$img_link",0))) {
                        echo $pic_error;
                        unlink($img_link);
                    }

                    if (true !== ($pic_error1 = @resampimagethmb("$file","../../$img_thmblink",0))) {
                        echo $pic_error1;
                        unlink($img_thmblink);
                    }
                }
            } else if($_FILES["image"]["name"] != ""){
                $message = "Something is wrong with your uploaded image, please make sure that the format is (jpg, png, gif) and the size is less than 9.5MB.<br />";
                $_SESSION['alert'] = "error";
            }
        }

        if($N_email == $N_old_email){

            $result = $obj_user->update_data($obj_crud, $N_admin_id, $N_role, $N_email, $N_username, $N_pic, $N_adminc, $N_region, $img_link, $img_thmblink, $global['root']);
            if($result == 1){
                $message = "Admin <i><b>'".correctDisplay($N_username)."'</b></i> has been succesfully edited.";
                $_SESSION['alert'] = "success";
            } else {
                $message = "There's no change in ".correctDisplay($N_username).".";
                $_SESSION['alert'] = "success";
            }

        } else {
            $check_email = $obj_user->check_email($obj_crud, $N_email);
            if($check_email == 0){
                $result = $obj_user->update_data($obj_crud, $N_admin_id, $N_role, $N_email, $N_username, $N_pic, $N_adminc, $N_region, $img_link, $img_thmblink, $global['root']);
                if($result == 1){
                    $message = "Admin <i><b>'".correctDisplay($N_username)."'</b></i> has been succesfully edited.";
                    $_SESSION['alert'] = "success";
                } else {
                    $message = "There's no change in ".correctDisplay($N_username).".";
                    $_SESSION['alert'] = "success";
                }
            } else {
                $message = "Update data Failed.<br/>Email already exist.";
                $_SESSION['alert'] = "error";
            }
        }

        if($_SESSION['admin_id'] == $N_admin_id){
            $_SESSION['admin_username'] = $N_username;
            $_SESSION['admin_email'] = $N_email;
            $_SESSION['admin_img'] = $img_link;
            $_SESSION['admin_img_thmb'] = $img_thmblink;
        }

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

    } 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'] == "password"){

        $N_admin_id = $obj_crud->escape_string(check_input($_POST['radmin_id']));
        //$N_opassword = $obj_crud->escape_string(check_input($_POST['opassword']));
        $N_password = $obj_crud->escape_string(check_input($_POST['password']));
        $N_repassword = $obj_crud->escape_string(check_input($_POST['repassword']));
        $data = $obj_user->get_data_detail($obj_crud, $N_admin_id);
        //$enc_opass = encrypt_key($N_opassword);
        $enc_npass = encrypt_key($N_password);
        $O_password = $data[0]['admin_password'];
        //echo $O_password.' - '.$enc_opass;

        //if($enc_opass == $O_password){
            $result = $obj_user->update_password($obj_crud, $N_admin_id, $enc_npass);
            if($result == 1){
                $message = "Success to Change Password to ".$N_password;
                $_SESSION['alert'] = "success";
            } else {
                $message = "Failed to change password.";
                $_SESSION['alert'] = "success";
            }
        // } else {
        //     $message = "Your old password is incorrect.";
        //     $_SESSION['alert'] = "error";
        // }

        $_SESSION['status'] = $message;
        header("Location:".$path['admin-edit'].$N_admin_id."/");
    } 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 deactivate the admin";
            $_SESSION['alert'] = "error";
        }else if($result == 1){
            $message = "Admin <b><i>'" . $O_title . "'</i></b> has been deactivate successfully.";
            $_SESSION['alert'] = "success";
        }

        $_SESSION['status'] = $message;
        header("Location:".$path['admin-edit'].$O_id."/");
    } 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 activate the admin";
            $_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-edit'].$O_id."/");
    }
}
?>