<?php ini_set('display_errors',1); ?>

<?php require_once 'inc/includes.php';
  
if(isset($_GET['logout'])){
	if(isset($_SESSION['user'])){
		unset($_SESSION['user']);
	}

	if(isset($_SESSION['token'])){
		unset($_SESSION['token']);
	}

	if(isset($_SESSION['token_uncrypted'])){
		unset($_SESSION['token_uncrypted']);
	}

	$_SESSION['authentificated'] = false;
	$_SESSION['message'] = "A bientôt, Vous êtes maintenant déconnecté.";
	header('location:index.php');
	exit();
}

if($_SERVER['REQUEST_METHOD'] == "POST"){
	if(!empty($_POST['password']) && !empty($_POST['email'])){
		$email =$_POST['email'];
		$password = User::hashPassword($_POST['password']);

		$data = array(
			'email'=>$email,
			'password'=>$password
			);

		$req = $DB->tquery('SELECT * FROM  users WHERE email=:email AND password=:password LIMIT 1',$data);
		if(!empty($req)){
			if($req[0]['active'] == 1){
				session_name('mon_site');
				$_SESSION['user'] = $req[0];
				 $_SESSION['user']['role'] = User::hashPassword($_SESSION['user']['role']);
				$_SESSION['authentificated'] = true;
				$_SESSION['token_uncrypted']=uniqid();
				$_SESSION['token'] = User::hashPassword($_SESSION['token_uncrypted']);

				$_SESSION['message'] = "Bienvenue,Vous pouvez vous connectez à votre espace admin.";

				$n= $DB->insert('UPDATE users SET last_login=NOW() WHERE email=:email',array('email'=>$email));

				header('location:index.php');
				exit();
			}else{
				$_SESSION['erreur'] = "Compte user non Actif,Vauillez vérifier votre méssagerie ...;";
			}
		}else{
			$_SESSION['erreur'] = "Votre email et/ou mot de passe sont invalides";
		}
	}else{
		if(empty($_POST['password'])){
			 $erreur_password = "&nbsp &nbsp &nbsp &nbsp &nbsp  Le mot de passe est requis.";
		}

		if(empty($_POST['email'])){
			 $erreur_email = "L'adresse email est requise !.";
		}
		$_SESSION['erreur'] = " Veuillez taper votre email et votre mot de passe ";
	}
}

 include 'inc/header.php';?>

<body style="margin: 0; padding: 0">

<!-- Login 2 start -->
<div class="login-2">
    <div class="container">
        <div class="row">
            <div class="col-lg-12">
                <div class="form-section">
                    <div class="logo-2">
                       
                    </div>
                    <h3>Connexion à votre espace</h3>
					
<?php if (isset($_SESSION['message'])): ?>
  <div class="message"> <?php echo $_SESSION['message']; ?></div>
  <?php unset($_SESSION['message']) ?>
<?php endif ?>
<?php if (isset($_SESSION['erreur'])): ?>
  <div class="errorMessage"> <?php echo $_SESSION['erreur']; ?></div>
  <?php unset($_SESSION['erreur']) ?>
<?php endif ?>
					
					
						
                    <form action="login.php" method="POST" id="login" >
                        <div class="form-group form-box">
                            <input type="text" name="email" class="input-text" value="<?php echo isset($_POST['email'])?$_POST['email']:''; ?>">
                        </div>
						
						<?php if(!empty($erreur_email)) :?>
							<div class="error"> <?php echo $erreur_email; ?></div>
						<?php endif ?>

                        <div class="form-group form-box">
                            <input type="password" name="password" class="input-text" placeholder="Mot de passe">
                        </div>
						
							<?php if(!empty($erreur_password)) :?>
							<div class="error"> <?php echo $erreur_password; ?></div>
						<?php endif ?>
						
						
						
						
						
                        <div class="form-group mb-0 clearfix">
                              <button type="submit" class="btn-md btn-theme float-left">Connexion</button></a>
                            <a href="forgot-password-2.html" class="forgot-password">Mot de passe oublié</a>
                        </div>
						 <div class="clearfix"></div>
                       <!--  <ul class="social-list">
                            <li><a href="#" class="facebook-color"><i class="fa fa"></i><span>Patients</span></a></li>
                            <li><a href="#" class="twitter-color"><i class="fa "></i><span>Docteurs</span></a></li>
                            <li><a href="#" class="google-color"><i class="fa fa"></i><span>Transporteurs</span></a></li>
                        </ul> -->
                        
                    </form>
                    <p>Pas de compte? <a href="inscription.html" class="thembo">Enregistrez vous ici</a></p>
                </div>
            </div>
        </div>
    </div>
</div>

  <!-- jQuery -->
<script src="https://extremelab.tech/plugins/jquery/jquery.min.js"></script>
<!-- jQuery UI 1.11.4 -->
<script src="https://extremelab.tech/plugins/jquery-ui/jquery-ui.min.js"></script>
<!-- Bootstrap 4 -->
<script src="https://extremelab.tech/plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- ChartJS -->
<script src="https://extremelab.tech/plugins/chart.js/Chart.min.js"></script>
<!-- Sparkline -->
<script src="https://extremelab.tech/plugins/sparklines/sparkline.js"></script>
<!-- jQuery Knob Chart -->
<script src="https://extremelab.tech/plugins/jquery-knob/jquery.knob.min.js"></script>
<!-- daterangepicker -->
<script src="https://extremelab.tech/plugins/moment/moment.min.js"></script>
<script src="https://extremelab.tech/plugins/daterangepicker/daterangepicker.js"></script>
<!-- Tempusdominus Bootstrap 4 -->
<script src="https://extremelab.tech/plugins/tempusdominus-bootstrap-4/js/tempusdominus-bootstrap-4.min.js"></script>
<!-- Summernote -->
<script src="https://extremelab.tech/plugins/summernote/summernote-bs4.min.js"></script>
<!-- overlayScrollbars -->
<script src="https://extremelab.tech/plugins/overlayScrollbars/js/jquery.overlayScrollbars.min.js"></script>
<!-- AdminLTE App -->
<script src="https://extremelab.tech/dist/js/adminlte.js"></script>
<!-- DataTables -->
<script src="https://extremelab.tech/plugins/datatables/jquery.dataTables.min.js"></script>
<script src="https://extremelab.tech/plugins/datatables-bs4/js/dataTables.bootstrap4.min.js"></script>
<script src="https://extremelab.tech/plugins/datatables-responsive/js/dataTables.responsive.min.js"></script>
<script src="https://extremelab.tech/plugins/datatables-responsive/js/responsive.bootstrap4.min.js"></script>
<script src="https://extremelab.tech/plugins/datatables-fixedheader/js/dataTables.fixedHeader.min.js" type="text/javascript"></script>
<script src="https://extremelab.tech/plugins/datatables-buttons/js/dataTables.buttons.min.js"></script>
<script src="https://extremelab.tech/plugins/datatables-buttons/js/buttons.bootstrap4.min.js"></script>
<script src="https://extremelab.tech/plugins/jszip/jszip.min.js"></script>
<script src="https://extremelab.tech/plugins/pdfmake/pdfmake.min.js"></script>
<script src="https://extremelab.tech/plugins/pdfmake/vfs_fonts.js"></script>
<script src="https://extremelab.tech/plugins/datatables-buttons/js/buttons.html5.min.js"></script>
<script src="https://extremelab.tech/plugins/datatables-buttons/js/buttons.print.min.js"></script>
<script src="https://extremelab.tech/plugins/datatables-buttons/js/buttons.colVis.min.js"></script>
<!-- Toastr-->
<script src="https://extremelab.tech/js/toastr.min.js"></script>
<!-- Validate -->
<script src="https://extremelab.tech/plugins/jquery-validation/jquery.validate.min.js"></script>
<script src="https://extremelab.tech/plugins/print/jQuery.print.min.js"></script>
<script src="https://extremelab.tech/js/jquery.classyqr.min.js"></script>
<script src="https://extremelab.tech/js/select2.js"></script>
<script src="https://extremelab.tech/plugins/sweet-alert/sweetalert.min.js"></script>
<!-- Scripts Translation -->

<!-- Main dashboard -->
  <script src="https://extremelab.tech/js/admin/main.js"></script>
<!-- Flash messages -->
<script>
    </script>

    <!-- Switch -->
  <script src="https://extremelab.tech/plugins/swtich-netliva/js/netliva_switch.js"></script>
  <script src="https://extremelab.tech/js/admin/dashboard.js"></script>
  
  <script src="//code.tidio.co/wm6kjlncgktzimpuaad5qetmtv9h1cui.js" async></script>

</body>
</html>