<?php


// error_reporting(E_ALL);
// ini_set("display_errors", 1);


define("ADMIN", true);
require_once("../common/lib.php");
require_once("../common/define.php");
define("TITLE_ELEMENT", $texts['DASHBOARD']." - ".$texts['LOGIN']);

$action = (isset($_GET['action'])) ? $_GET['action'] : "";

if($action == "logout" && isset($_SESSION['user'])) unset($_SESSION['user']);

if(isset($_SESSION['user'])){
    if($_SESSION['user']['type'] != "registered"){
        header("Location: index.php");
        exit();
    }else
        unset($_SESSION['user']);
}

$captcha = $_POST["g-recaptcha-response"];

// if($captcha <> "" ){
// $message = "Erreur 1";
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $user = htmlentities($_POST['user'], ENT_COMPAT, "UTF-8");
    $password = $_POST['password'];
	
	// $message = "Erreur 2";
	
	$recaptcha_secret = '6LfLma8cAAAAAEdp4g3wZzNQko4q7jPpOUTyVrP1';
    $recaptcha_response = $_POST['g-recaptcha-response'];
 
    // Envoyer une requête POST à l'API de reCAPTCHA
    $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$recaptcha_secret&response=$recaptcha_response");
    $response_data = json_decode($response);

    // Vérifier si la vérification reCAPTCHA a réussi
    if ($response_data->success) {
		 $message = "Le formulaire a été soumis avec succès !";
    // if(check_token("/".ADMIN_FOLDER."/login.php", "login", "post")){
        // echo "SELECT * FROM pm_user WHERE login = ".$db->quote($user)." AND pass = '".md5($password)."' AND checked = 1";
        $result_user = $db->query("SELECT * FROM pm_user WHERE login = ".$db->quote($user)." AND pass = '".md5($password)."' AND checked = 1");
        if($result_user !== false && $db->last_row_count() > 0){
            $row = $result_user->fetch();
            $_SESSION['user']['id'] = $row['id'];
            $_SESSION['user']['login'] = $user;
            $_SESSION['user']['email'] = $row['email'];
            $_SESSION['user']['type'] = $row['type'];
            header("Location: index.php");
            exit();
        }else
            $_SESSION['msg_error'][] = $texts['LOGIN_FAILED'];
    
	} else {
        // La validation du reCAPTCHA a échoué
         $message = "Erreur reCAPTCHA. Veuillez réessayer.";
    }
	
	// }else
        // $_SESSION['msg_error'][] = $texts['BAD_TOKEN2'];
}

// }else{
	// $_SESSION['msg_error'][] = $texts['INVALID_CAPTCHA_CODE'];
// }


$csrf_token = get_token("login"); ?>
<!DOCTYPE html>
<html>
<head>
	<script src="https://www.google.com/recaptcha/api.js" async defer></script>
	<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
	<title>Orientalsnacks</title>
</head>
<body>
	<div class="container" id="container">
		<div class="form-container log-in-container">
		

		
		
			<form id="form" class="form-horizontal" role="form" action="login.php" method="post">
            <input type="hidden" name="csrf_token" value="<?php echo $csrf_token; ?>">
				<h1>Login</h1>
				<div class="social-container">

				</div>
				<input type="text" name="user" placeholder="Email" />
				<input type="password" name="password" placeholder="Password" />
				

				<div class="alert-container">
                        <div class="alert alert-success alert-dismissable"></div>
                        <div class="alert alert-warning alert-dismissable"></div>
                        <div class="alert alert-danger alert-dismissable"></div>
                </div>

		   
		   <button class="g-recaptcha" 
          data-sitekey="6LfLma8cAAAAAAJNYLXMlQ9kGxh8pzSjuCxkwhA6" 
          data-callback="onSubmit" 
          data-action="submit" name="login">Envoyer</button>
		  
			</form>
		</div>
		<div class="overlay-container">
			<div class="overlay">
				<div class="overlay-panel overlay-right">
					<img src="images/logoN.png" style="width:200px;" />
				
				</div>
			</div>
		</div>
	</div>
</body>
</html>

<?php
$_SESSION['msg_error'] = array();
$_SESSION['msg_success'] = array();
$_SESSION['msg_notice'] = array(); ?>

<script>
  function onSubmit(token) {
    document.querySelector('form').submit();
  }
</script>


<style>
@import url('https://fonts.googleapis.com/css?family=Montserrat:400,800');

* {
	box-sizing: border-box;
}

body {
	background: #2e2e2;
	display: flex;
	justify-content: center;
	align-items: center;
	flex-direction: column;
	font-family: 'Montserrat', sans-serif;
	height: 100vh;
	margin: -20px 0 50px;
}

h1 {
	font-weight: bold;
	margin: 0;
}

p {
	font-size: 14px;
	font-weight: 100;
	line-height: 20px;
	letter-spacing: 0.5px;
	margin: 20px 0 30px;
}

span {
	font-size: 12px;
}

a {
	color: #333;
	font-size: 14px;
	text-decoration: none;
	margin: 15px 0;
}

form {
	background-color: #FFFFFF;
	display: flex;
	align-items: center;
	justify-content: center;
	flex-direction: column;
	padding: 0 50px;
	height: 100%;
	text-align: center;
}

input {
	background-color: #eee;
	border: none;
	padding: 12px 15px;
	margin: 8px 0;
	width: 100%;
}

.container {
	background-color: #fff;
	border-radius: 10px;
  	box-shadow: 0 14px 28px rgba(0,0,0,0.25), 0 10px 10px rgba(0,0,0,0.22);
	position: relative;
	overflow: hidden;
	width: 768px;
	max-width: 100%;
	min-height: 480px;
}

.form-container {
	position: absolute;
	top: 0;
	height: 100%;
}

.log-in-container {
	left: 0;
	width: 50%;
	z-index: 2;
}


.overlay-container {
	position: absolute;
	top: 0;
	left: 50%;
	width: 50%;
	height: 100%;
}



.overlay_trade {
	background: #0c0c0c;
	background: -webkit-linear-gradient(to right, #FF4B2B, #0c0c0c);
	background: linear-gradient(to right, #FF4B2B, #0c0c0c);
	background-repeat: no-repeat;
	background-size: cover;
	background-position: 0 0;
	color: #FFFFFF;
	position: relative;
	left: -100%;
	height: 100%;
	width: 200%;
}

.overlay-panel {
	position: absolute;
	display: flex;
	align-items: center;
	justify-content: center;
	flex-direction: column;
	padding: 0 40px;
	text-align: center;
	top: 0;
	height: 100%;
	width: 50%;
}


.overlay-right {
	right: 0;
}


.social-container {
	margin: 50px 0;
}

.social-container a {
	border: 1px solid #DDDDDD;
	border-radius: 50%;
	display: inline-flex;
	justify-content: center;
	align-items: center;
	margin: 0 5px;
	height: 40px;
	width: 40px;
}

</style>


<style>
.overlay {
	background: #0c0c0c;
	background: -webkit-linear-gradient(to right, #000000, #E4032F);
	background: linear-gradient(to right, #000000, #E4032F);
	background-repeat: no-repeat;
	background-size: cover;
	background-position: 0 0;
	color: #FFFFFF;
	position: relative;
	left: -100%;
	height: 100%;
	width: 200%;
}

button {
	border-radius: 20px;
	border: 1px solid #d1032b;
	background-color: #d1032b;
	color: #FFFFFF;
	font-size: 12px;
	font-weight: bold;
	padding: 12px 45px;
	letter-spacing: 1px;
	text-transform: uppercase;
	transition: transform 80ms ease-in;
}

</style>
