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

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

if(!isset($_GET['action'])){
    $alert = "";
    $message = "";

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

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

    //echo  $_COOKIE['mailTnp'];

    if(isset($_COOKIE['mailTnp'])) {
        setcookie("mailTnp", "", time() - 3600);
        setcookie("passTnp", "", time() - 3600);
    }

    if(isset($_GET['notLogin'])){
        $alert = "error";
        $message = "Please login first to access ruas management system!";
    }

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

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

        $O_email = $obj_crud->escape_string(check_input($_POST['email']));
        $O_password = $obj_crud->escape_string(check_input($_POST['password']));
        $enc_pass = encrypt_key($O_password);

        $result = $obj_user->login($obj_crud, $O_email, $enc_pass);
        if($result == 1){
            if(isset($_POST['remember'])){
                setcookie('mailTnp', $O_email, time() + (86400 * 30), "/"); // 86400 = 1 day
                setcookie('passTnp', $O_password, time() + (86400 * 30), "/"); // 86400 = 1 day
            }
            header("Location:".$path['dashboard']);
        }else{
            $_SESSION['message_log'] = "Incorrect Email or Password!";
            $_SESSION['alert_log'] = "error";
            header("Location:".$path['login']);
        }
    }
}
?>