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

require_once($global['root']."model/Client.php");
$obj_client = new Client();

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']) && $_GET['id'] != ""){

    $acsc_edit = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'edit_client');
    $acsc_password = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'change_password');
    $acsc_active = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'change_stat_active');

    $O_id = $obj_crud->escape_string(check_input($_GET['id']));
    if($acsc_edit == 1){
        if($O_id != ""){
            $data = $obj_client->get_data_detail($obj_crud, $O_id);
            $data_region = $obj_region->get_data_select($obj_crud);
        } else {
            header("Location:".$path['dashboard']);
        }
    } else {
        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'] == "edit"){

        $N_client_id = $obj_crud->escape_string(check_input($_POST['client_id']));
        $N_old_email = $obj_crud->escape_string(check_input($_POST['old_email']));
        $N_name = $obj_crud->escape_string(check_input($_POST['name']));
        $N_email = $obj_crud->escape_string(check_input($_POST['email']));
        $N_phone = $obj_crud->escape_string(check_input($_POST['phone']));
        $N_region = $obj_crud->escape_string(check_input($_POST['region']));
        $N_address = $obj_crud->escape_string(check_input($_POST['address']));

        if($N_email == $N_old_email){

            $result = $obj_client->update_data($obj_crud, $N_client_id, $N_name, $N_phone, $N_email, $N_address, $N_region);
            if($result == 1){
                $message = "Client <i><b>'".correctDisplay($N_name)."'</b></i> has been succesfully edited.";
                $_SESSION['alert'] = "success";
            } else {
                $message = "There's no change in ".correctDisplay($N_name).".";
                $_SESSION['alert'] = "success";
            }

        } else {
            $check_email = $obj_client->check_email($obj_crud, $N_email);
            if($check_email == 0){
                $result = $obj_client->update_data($obj_crud, $N_client_id, $N_name, $N_phone, $N_email, $N_address, $N_region);
                if($result == 1){
                    $message = "Client <i><b>'".correctDisplay($N_name)."'</b></i> has been succesfully edited.";
                    $_SESSION['alert'] = "success";
                } else {
                    $message = "There's no change in ".correctDisplay($N_name).".";
                    $_SESSION['alert'] = "success";
                }
            } else {
                $message = "Update data Failed.<br/>Email already exist.";
                $_SESSION['alert'] = "error";
            }
        }

        $_SESSION['status'] = $message;
        header("Location:".$path['client-edit'].$N_client_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_client->change_active($obj_crud, $O_id, "no");
        if($result <= 0){
            $message = "Something is wrong while deactivate the client";
            $_SESSION['alert'] = "error";
        }else if($result == 1){
            $message = "Client <b><i>'" . $O_title . "'</i></b> has been deactivate successfully.";
            $_SESSION['alert'] = "success";
        }

        $_SESSION['status'] = $message;
        header("Location:".$path['client-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_client->change_active($obj_crud, $O_id, "yes");
        if($result <= 0){
            $message = "Something is wrong while activate the client";
            $_SESSION['alert'] = "error";
        }else if($result == 1){
            $message = "Client <b><i>'" . $O_title . "'</i></b> has been activate successfully.";
            $_SESSION['alert'] = "success";
        }

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

        $N_client_id = $obj_crud->escape_string(check_input($_POST['rclient_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_client->get_data_detail($obj_crud, $N_client_id);
        //$enc_opass = encrypt_key($N_opassword);
        $enc_npass = encrypt_key($N_password);
        $O_password = $data[0]['client_password'];
        //echo $O_password.' - '.$enc_opass;

        //if($enc_opass == $O_password){
            $result = $obj_client->update_password($obj_crud, $N_client_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['client-edit'].$N_client_id."/");
    }
}
?>