<?php

session_start();
include("connectdb.php");

if ( isset( $_POST[ 'btn_login' ] ) ) {

  if (empty($_POST['email']) || empty($_POST['password'])) {
    $error_username="mot de passe ou email non renseigné";
  } elseif (isset($_POST['email']) && isset($_POST['password'])) {
    $login = $pdo->prepare('SELECT * FROM tbl_user WHERE useremail=? AND password=?');
    $login->execute(array($_POST['email'], $_POST['password']));
    $count = $login->rowCount();
    if ($count == 0) {
      $error_username="mot de passe ou email incorrect";
    } else {
      while ( $row = $login->fetch() ) {

          $_SESSION["userid"]=$row["userid"];
          $_SESSION["username"]=$row["username"];
          $_SESSION["useremail"]=$row["useremail"];
          $_SESSION["role"]=$row["role"];
          
          header( 'location:dashboard.php' );


        }
    }
    

  }
}

       
    


?>



<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>E-stock | Log in</title>

  <!-- Google Font: Source Sans Pro -->
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="plugins/fontawesome-free/css/all.min.css">
  <!-- icheck bootstrap -->
  <link rel="stylesheet" href="plugins/icheck-bootstrap/icheck-bootstrap.min.css">
  <link rel="stylesheet" href="plugins/sweetalert2/sweetalert2.css">
  <link rel="stylesheet" href="plugins/sweetalert2/sweetalert2.min.css">

  <!-- Theme style -->
  <link rel="stylesheet" href="dist/css/adminlte.min.css">
</head>
<body class="hold-transition login-page">
<div class="login-box">
  <div class="login-logo">
    <a href="index.php"><b>E-Stock</b></a>
  </div>
  <!-- /.login-logo -->
  <div class="card">
    <div class="card-body login-card-body">
      <p class="login-box-msg">Connectez-vous à votre compte</p>

      <form method="post">
        <div class="input-group mb-3">
          <input type="email" class="form-control" placeholder="Email" name="email" required>
          <div class="input-group-append">
            <div class="input-group-text">
              <span class="fas fa-envelope"></span>
            </div>
            
          </div>
        </div>
        <div class="input-group mb-3">
          <input type="password" class="form-control" placeholder="Mot de passe" name="password" required>
          <div class="input-group-append">
            <div class="input-group-text">
              <span class="fas fa-lock"></span>
            </div>
 
          </div>
        </div>
        <div class="row">
          <div class="col-6">
            
          </div>
          <!-- /.col -->
          <div class="col-6">
            <button type="submit" class="btn btn-primary btn-block" name="btn_login">Se connecter</button>
          </div>
          <!-- /.col -->
        </div>
      </form>
      <?php
                         if(!empty($error_username)){
                            echo '<h5 style="color:red;">'.$error_username.'</h5>';
                         }
                    ?>

      
      <!-- /.social-auth-links -->

      
      
    </div>
    <!-- /.login-card-body -->
  </div>
</div>
<!-- /.login-box -->


</body>
</html>
