<?php
session_start();
require_once __DIR__.'/config/connexion.php';

$err='';
if($_SERVER['REQUEST_METHOD']==='POST'){
  $id=trim($_POST['identifiant']??'');
  $pw=$_POST['password']??'';
  if($id===''||$pw==='') { $err='Champs requis'; }
  else {
    // Récupérer aussi le rôle pour stocker en session
    $st=$gc_pdo->prepare('SELECT id,user_pass,first_name,user_role,status FROM users_table WHERE user_email=:id OR phone_num=:id LIMIT 1');
    $st->execute([':id'=>$id]);
    $row=$st->fetch();
    if(!$row || !password_verify($pw,$row['user_pass'])){
      $err='Identifiants invalides';
    } elseif($row['status']!=='active') {
      $err='Compte désactivé';
    } else {
      // Session sécurisée + rôle
      session_regenerate_id(true);
      $_SESSION['gc_user_id']=$row['id'];
      $_SESSION['user_role']=$row['user_role'];
      $_SESSION['first_name']=$row['first_name'];
      header('Location: index.php'); exit;
    }
  }
}
?><!DOCTYPE html><html lang="fr">
  <head><meta charset="utf-8"><title>Connexion Backups</title><link rel="stylesheet" href="../vendor/bootstrap/css/bootstrap.min.css"><link rel="stylesheet" href="../vendor/fontawesome-free/css/all.min.css"><link rel="stylesheet" href="../css/ruang-admin.min.css"></head><body class="bg-gradient-login">
<div class="container-login">
  <div class="row justify-content-center">
    <div class="col-xl-5 col-lg-6 col-md-8">
      <div class="card shadow-sm my-5">
        <div class="card-body p-4">
          <h5 class="text-center mb-4"><i class="fas fa-database text-primary"></i> Espace Backups</h5>
          <?php if($err): ?><div class="alert alert-danger py-2"><?php echo htmlspecialchars($err); ?></div><?php endif; ?>
          <form method="post" novalidate>
            <div class="form-group"><label>Email ou Téléphone</label><input name="identifiant" class="form-control" value="<?php echo htmlspecialchars($_POST['identifiant']??''); ?>" autofocus></div>
            <div class="form-group"><label>Mot de passe</label><input name="password" type="password" class="form-control"></div>
            <button class="btn btn-primary btn-block">Connexion</button>
          </form>
          <hr>
                  <div class="text-center">
                    <a class="font-weight-bold small" href="forgot_password.php">Mot de passe oublié ?</a>
                  </div>
        </div>
      </div>
    </div>
  </div>
</div>
<script src="../vendor/jquery/jquery.min.js"></script><script src="../vendor/bootstrap/js/bootstrap.bundle.min.js"></script><script src="../vendor/jquery-easing/jquery.easing.min.js"></script><script src="../js/ruang-admin.min.js"></script>
</body></html>
