<?php
session_start();
require_once "inc/config.php";


/*

try {
    $nom = "Marah Abderrahman";
    $email = "contact@mondialisa.com";
    $password = "12341234";
    $countries = "all";
    $cities = "all";
    $role = "superadmin";
    $actif = 1;

    // Hachage sécurisé du mot de passe
    $hashedPassword = password_hash($password, PASSWORD_DEFAULT);

    $sql = "INSERT INTO mondialisa_admins (nom, email, password, countries, cities, actif, role, created_at)
            VALUES (:nom, :email, :pass, :countries, :cities, :actif, :role, NOW())";
    $stmt = $dbh->prepare($sql);

    $stmt->bindParam(':nom', $nom);
    $stmt->bindParam(':email', $email);
    $stmt->bindParam(':pass', $hashedPassword);
    $stmt->bindParam(':countries', $countries);
    $stmt->bindParam(':cities', $cities);
    $stmt->bindParam(':actif', $actif, PDO::PARAM_INT);
    $stmt->bindParam(':role', $role);

    $stmt->execute();

    echo "✅ Super admin inséré avec succès.";
} catch (Exception $e) {
    echo "❌ Erreur : " . $e->getMessage();
}

*/

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = trim($_POST['email']);
    $password = trim($_POST['password']);

    $stmt = $dbh->prepare("SELECT * FROM mondialisa_admins WHERE email = ? AND actif = 1 LIMIT 1");
    $stmt->execute([$email]);
    $admin = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($admin && password_verify($password, $admin['password'])) {
         $_SESSION['admin']['connected'] = true;
        $_SESSION['admin'] = [
            'id' => $admin['id'],
            'nom' => $admin['nom'],
            'email' => $admin['email'],
            'role' => $admin['role'],
            'countries' => $admin['countries'],
            'cities' => $admin['cities']
        ];
        header('Location: index.php');
        exit;
    } else {
        $error = "Identifiants incorrects.";
    }
}
?>

<!DOCTYPE html>
<html lang="fr">
<head>
  <meta charset="UTF-8">
  <title>Admin - Mondialisa</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light d-flex align-items-center justify-content-center" style="height:100vh;">
  <div class="card shadow p-4" style="width:400px;">
    <h4 class="text-center mb-3">🔐 Administrator</h4>

    <?php if (!empty($error)): ?>
      <div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
    <?php endif; ?>

    <form method="POST">
      <div class="mb-3">
        <label>Email</label>
        <input type="email" class="form-control" name="email" required>
      </div>
      <div class="mb-3 position-relative">
        <label>Password</label>
        <input type="password" class="form-control" name="password" id="password" required>
        <span class="position-absolute end-0 top-0 mt-3 me-3" style="cursor:pointer" onclick="togglePassword()">
          👁️
        </span>
      </div>
      <button class="btn btn-primary w-100">Sign in</button>
    </form>
  </div>

  <script>
    function togglePassword() {
      let p = document.getElementById('password');
      p.type = (p.type === 'password') ? 'text' : 'password';
    }
  </script>
</body>
</html>
