<?php
session_start();
if (!isset($gc_pdo)) {
  $gc_host='localhost';
  $gc_db='assurance_gestion';
  $gc_user='root';
  $gc_pass='';
  try { $gc_pdo=new PDO("mysql:host=$gc_host;dbname=$gc_db;charset=utf8",$gc_user,$gc_pass,[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION,PDO::ATTR_DEFAULT_FETCH_MODE=>PDO::FETCH_ASSOC]); }
  catch(Exception $e){ die('Erreur connexion centrale: '.$e->getMessage()); }
}

if (empty($_SESSION['gestion_reset_email']) || empty($_SESSION['gestion_code_verified'])) {
    header('Location: forgot_password.php');
    exit;
}

$email = $_SESSION['gestion_reset_email'];
$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $pass = trim($_POST['password'] ?? '');
    $confirm = trim($_POST['confirm_password'] ?? '');
    if (empty($pass) || empty($confirm)) {
        $error = 'Tous les champs sont requis.';
    } elseif ($pass !== $confirm) {
        $error = 'Les mots de passe ne correspondent pas.';
    } elseif (strlen($pass) < 6 || strlen($pass) > 50) {
        $error = 'Le mot de passe doit contenir entre 6 et 50 caractères.';
    } else {
        $hash = password_hash($pass, PASSWORD_BCRYPT);
  $stmt = $gc_pdo->prepare("UPDATE users_table SET user_pass = :pass, recup_pass = NULL, recup_expiry = NULL WHERE user_email = :email");
  if ($stmt->execute([':pass'=>$hash, ':email'=>$email])) {
            $success = 'Mot de passe réinitialisé avec succès.';
            unset($_SESSION['gestion_code_verified']);
            unset($_SESSION['gestion_reset_email']);
        } else {
            $error = 'Erreur lors de la mise à jour.';
        }
  $stmt = null;
    }
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <link href="./data/123456/logo.png" rel="icon">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="../vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/qrious/4.0.2/qrious.min.js"></script>
    <link href="../vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="../css/ruang-admin.min.css" rel="stylesheet">
    <link href="../vendor/datatables/dataTables.bootstrap4.min.css" rel="stylesheet">
  <title>Réinitialisation mot de passe - Gestion Compagnies</title>
</head>
<body class="bg-gradient-login">
<div class="container-login">
  <div class="row justify-content-center">
    <div class="col-xl-6 col-lg-6 col-md-9">
      <div class="card shadow-sm my-5">
        <div class="card-body p-0">
          <div class="row"><div class="col-lg-12"><div class="login-form">
            <div class="text-center"><h1 class="h4 text-gray-900 mb-4">Nouveau mot de passe</h1></div>
            <?php if ($error): ?><div class="alert alert-danger"><?= $error ?></div><?php endif; ?>
            <?php if ($success): ?><div class="alert alert-success"><?= $success ?><br><a href="login.php" class="btn btn-sm btn-primary mt-2">Se connecter</a></div><?php endif; ?>
            <?php if (!$success): ?>
            <form method="POST" action="">
              <div class="form-group">
                <label>Nouveau mot de passe</label>
                <input type="password" class="form-control" name="password" required>
              </div>
              <div class="form-group">
                <label>Confirmer le mot de passe</label>
                <input type="password" class="form-control" name="confirm_password" required>
              </div>
              <div class="form-group"><button type="submit" class="btn btn-primary btn-block">Réinitialiser</button></div>
            </form>
            <?php endif; ?>
            <hr>
            <div class="text-center"><a class="small" href="login.php">Retour à la connexion</a></div>
          </div></div></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>
