<?php
session_start();
require_once __DIR__.'/include/config.php';
require_once __DIR__.'/include/functions.php';
require_once __DIR__.'/include/Page.Class.php';

$title="Login";

$Page = new Page();
$Page->Title = $title;
$MSG = '';


if($_POST['superadmin_embody']){

global $User;
$User = UserInit();

}



if(request_ifset('logout')) {
        session_unset();
        session_destroy();
        session_start();
        unset($_COOKIE['user_session']);
        unset($_COOKIE['user_superadmin']);
        setcookie('user_session', '', time() - 3600, '/'); // empty value and old timestamp
        setcookie('user_superadmin', '', time() - 3600, '/'); // empty value and old timestamp
        header("Location: {$Page->BASE_URL_HTTPS}/");
}
if(request_ifset('email') && request_ifset('pswd')) {

    $res = check_password_hash(request_ifset('email'),request_ifset('pswd'));

    if($res) {
        #print_r($res);
        $can_login = 1;
        if($can_login == 1){
            
            $_SESSION['user_id'] = $res->id;
            $_SESSION['email'] = $res->email;

            setcookie("user_session",$res->id,time()+ (365 * 24 * 60 * 60),"/", false);
            
            if($res->is_superadmin == 1){
                setcookie("user_superadmin",md5($res->email),time()+ (365 * 24 * 60 * 60),"/", false);
                
                
            } else {
                unset($_COOKIE['user_superadmin']);
                setcookie('user_superadmin', '', time() - 3600, '/'); // empty value and old timestamp
            }

            
            header("Location: {$Page->BASE_URL_HTTPS}{$_REQUEST['redirect_to']}");
        } else {
            $MSG =  <<<EOD
    
            <div class="alert alert-danger" role="alert">
                Il tuo account non è abilitato all'accesso.
            </div>
        
        EOD;
        }
    } else {
    
        $MSG =  <<<EOD
    
    <div class="alert alert-danger" role="alert">
        Credenziali non valide. Non sei autorizzato all'accesso.
    </div>

EOD;
    }
} 

$Body = <<<EOD


<body>
	<!--wrapper-->
	<div class="wrapper">
		<div class="authentication-header"></div>
		<div class="section-authentication-signin d-flex align-items-center justify-content-center my-5 my-lg-0">
			<div class="container">
				<div class="row row-cols-1 row-cols-lg-2 row-cols-xl-3">
					<div class="col mx-auto">
						<div class="mb-4 text-center">
							<img src="assets/images/logo-img.png" width="180" alt="" />
						</div>
						<div class="card rounded-4">
							<div class="card-body">
								<div class="p-4 rounded">
									<div class="text-center">
										<h3 class="">Sign in</h3>
										<p>Don't have an account yet? <a href="authentication-signup.html">Sign up here</a>
										</p>
									</div>
									
									<div class="login-separater text-center mb-4"> <span> SIGN IN WITH EMAIL</span>
										<hr/>
									</div>
									<div class="form-body">
										<form method="POST" action="" class="row g-3">
											<div class="col-12">
												<label for="inputEmailAddress" class="form-label">Email Address</label>
												<input type="email" class="form-control" id="inputEmailAddress" name="email" placeholder="Email Address">
											</div>
											<div class="col-12">
												<label for="inputChoosePassword" class="form-label">Enter Password</label>
												<div class="input-group" id="show_hide_password">
													<input type="password" class="form-control border-end-0" id="inputChoosePassword" name="pswd" value="12345678" placeholder="Enter Password"> <a href="javascript:;" class="input-group-text bg-transparent"><i class='bx bx-hide'></i></a>
												</div>
											</div>
											<div class="col-md-6">
												<div class="form-check form-switch">
													<input class="form-check-input" type="checkbox" id="flexSwitchCheckChecked" checked>
													<label class="form-check-label" for="flexSwitchCheckChecked">Remember Me</label>
												</div>
											</div>
											<div class="col-md-6 text-end">	<a href="authentication-forgot-password.html">Forgot Password ?</a>
											</div>
											<div class="col-12">
												<div class="d-grid">
                                                {$MSG}

													<button type="submit" class="btn btn-primary"><i class="bx bxs-lock-open"></i>Sign in</button>
												</div>
											</div>
										</form>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
				<!--end row-->
			</div>
		</div>
	</div>
	<!--end wrapper-->
	<!-- Bootstrap JS -->
	<script src="assets/js/bootstrap.bundle.min.js"></script>
	<!--plugins-->
	<script src="assets/js/jquery.min.js"></script>
	<script src="assets/plugins/simplebar/js/simplebar.min.js"></script>
	<script src="assets/plugins/metismenu/js/metisMenu.min.js"></script>
	<script src="assets/plugins/perfect-scrollbar/js/perfect-scrollbar.js"></script>
	<!--Password show & hide js -->
	<script>
		$(document).ready(function () {
			$("#show_hide_password a").on('click', function (event) {
				event.preventDefault();
				if ($('#show_hide_password input').attr("type") == "text") {
					$('#show_hide_password input').attr('type', 'password');
					$('#show_hide_password i').addClass("bx-hide");
					$('#show_hide_password i').removeClass("bx-show");
				} else if ($('#show_hide_password input').attr("type") == "password") {
					$('#show_hide_password input').attr('type', 'text');
					$('#show_hide_password i').removeClass("bx-hide");
					$('#show_hide_password i').addClass("bx-show");
				}
			});
		});
	</script>
	<!--app JS-->
	<script src="assets/js/app.js"></script>
</body>
EOD;



echo $Page->GetMeta();
echo $Body;
