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

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

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

    $N_token = isset($_GET['token']) ? $_GET['token'] : "";
    if($N_token == ""){
        $_SESSION['message_log'] = "Reset Token Not Found.";
        $_SESSION['alert_log'] = "error";
        header("Location:".$path['login']);
    } else {
        $detail = $obj_user->get_data_reset($obj_crud, $N_token);
        if(is_array($detail)){
            //nothing
        } else {
            $_SESSION['message_log'] = "Reset Token Not Found.";
            $_SESSION['alert_log'] = "error";
            header("Location:".$path['login']);
        }
    }

    if(isset($_SESSION['message_front'])){
        $message = $_SESSION['message_front'];
        unset($_SESSION['message_front']);
    } else {
        $message = "";
    }

    if(isset($_SESSION['alert_front'])){
        $alert = $_SESSION['alert_front'];
        unset($_SESSION['alert_front']);
    } else {
        $alert = "";
    }

} else if(isset($_GET['action'])){

    if($_GET['action'] == 'reset'){

        $O_email = $obj_crud->escape_string(check_input($_POST['email']));
        $O_name = $obj_crud->escape_string(check_input($_POST['name']));
        $O_password = $obj_crud->escape_string(check_input($_POST['password']));
        $O_repassword = $obj_crud->escape_string(check_input($_POST['repassword']));
        $O_token = $obj_crud->escape_string(check_input($_POST['token']));

        $enc_pass = encrypt_key($O_password);

        if($O_password == $O_repassword){
            $result = $obj_user->set_new_password($obj_crud, $O_email, $O_token, $enc_pass);
            if($result != 0){
                $_SESSION['message_log'] = "Success reset password, Please check your email address for new password.";
                $_SESSION['alert_log'] = "success";

                //send mail
                $body_message = $obj_mail->reset_password_mail($global['logo-url'], $O_name, $O_password);
                $subject_message = "Warehouse System Reset Password";
                $sent_customer = smtp_mail($O_email, $smtp['url'], $smtp['from'], $smtp['password'], $smtp['from_name'], $subject_message, $body_message, $smtp['reply-cs']);
                header("Location:".$path['login']);

            }else{
                $_SESSION['message_front'] = "Failed reset password. Something wrong with your input!";
                $_SESSION['alert_front'] = "error";
                header("Location:".$path['reset-password']."?token=".$O_token);
            }
        } else {
            $_SESSION['message_front'] = "Failed reset password. Re-Type password is not match!";
            $_SESSION['alert_front'] = "error";
            header("Location:".$path['reset-password']."?token=".$O_token);
        }
    }
}
?>