<?php
require 'config.php';

if (session_status() === PHP_SESSION_NONE) {
  session_start();
}

$errors = [];
$showForgot = false;

if (isset($_GET['registered'])) {
    $msg = "Registrasi sukses, silakan login.";
}

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

    if ($login === '' || $password === '') {
        $errors[] = "Masukkan Username/Email dan password.";
    } else {
        $stmt = $pdo->prepare("SELECT * FROM users WHERE username = ? OR email = ? LIMIT 1");
        $stmt->execute([$login, $login]);
        $user = $stmt->fetch(PDO::FETCH_ASSOC);

        if ($user && password_verify($password, $user['password'])) {
            session_regenerate_id(true);
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['username'] = $user['username'];
            $_SESSION['role'] = $user['role'];
            $_SESSION['photo'] = $user['photo'] ?? '';
            $_SESSION['fullname'] = $user['fullname'] ?? '';

            if ($user['role'] === 'admin') {
                header("Location: ../admin/dsadmin.php");
                exit;
            } else {
                header("Location: ../user/books.php");
                exit;
            }
        } else {
            $errors[] = "Login gagal. Username/Email atau Password salah.";
            $showForgot = true;
        }
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Login - Pustakaia</title>
  <style>
    html, body { height: 100%; margin: 0; display: flex; flex-direction: column; font-family: "Poppins", Arial, sans-serif; background: linear-gradient(135deg, #f9f9f9, #e9f5ec); color: #222; }
    a { text-decoration: none; color: inherit; }
    header { display: flex; justify-content: space-between; align-items: center; padding: 20px 40px; background: #fff; border-bottom: 1px solid #ddd; }
    header .logo { font-size: 22px; font-weight: bold; color: #069797; }
    main { flex: 1; display: flex; justify-content: center; align-items: center; padding: 40px 20px; }
    .login-container { max-width: 400px; width: 100%; background: #fff; padding: 30px; border-radius: 8px; box-shadow: 0 4px 12px rgba(0,0,0,0.08); }
    .login-container h2 { text-align: center; margin-bottom: 20px; color: #069797; }
    .msg { font-size: 14px; margin-bottom: 10px; }
    .msg.success { color: green; }
    .msg.error { color: red; }
    .input-group { margin-bottom: 15px; }
    .input-group label { font-size: 14px; display: block; margin-bottom: 5px; }
    .input-group input { width: 100%; padding: 10px; border: 1px solid #ddd; border-radius: 6px; font-size: 14px; }
    button { width: 100%; padding: 12px; background: #069797; border: none; border-radius: 6px; color: #fff; font-size: 16px; cursor: pointer; transition: 0.3s; }
    button:hover { background: #048080; }
    .register-link { text-align: center; margin-top: 15px; font-size: 14px; }
    .register-link a { color: #069797; font-weight: bold; }
    footer { background: #fff; padding: 20px 40px; font-size: 14px; color: #222; border-top: 1px solid #ddd; }
  </style>
</head>
<body>
  <header>
    <div class="logo">📚 Pustakaia</div>
  </header>

  <main>
    <div class="login-container">
      <h2>Login</h2>

      <?php if (!empty($msg)): ?>
        <p class="msg success"><?= htmlspecialchars($msg) ?></p>
      <?php endif; ?>
      <?php foreach ($errors as $e): ?>
        <p class="msg error"><?= htmlspecialchars($e) ?></p>
      <?php endforeach; ?>

      <form method="post">
        <div class="input-group">
          <label>Username atau Email</label>
          <input type="text" name="login" required>
        </div>
        <div class="input-group">
          <label>Password</label>
          <input type="password" name="password" required>

          <?php if($showForgot): ?>
          <div style="text-align:right;">
            <a href="../pengaturan/forgot.php" style="font-size:12px;color:#069797">Lupa password?</a>
          </div>
          <?php endif; ?>

        </div>
        <button type="submit">Login</button>
      </form>

      <div class="register-link">
        Belum punya akun? <a href="register.php">Daftar</a>
      </div>
    </div>
  </main>

  <footer>
    <div class="footer-bottom">
      <p>© 2025, Pustakaia | Website developed by Grup 3</p>
    </div>
  </footer>
</body>
</html>
