<?php
require_once __DIR__ . '/config/koneksi.php';
require_once __DIR__ . '/config/security.php';
require_once __DIR__ . '/config/functions.php';

session_start();
$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = cleanInput($_POST['username']);
    $password = cleanInput($_POST['password']);

    $stmt = $pdo->prepare("SELECT * FROM users WHERE username = ? AND is_active = 1 LIMIT 1");
    $stmt->execute([$username]);
    $user = $stmt->fetch();

    if ($user && verifyPassword($password, $user['password_hash'])) {
        $_SESSION['user_id'] = $user['id'];
        $_SESSION['username'] = $user['username'];
        $_SESSION['full_name'] = $user['full_name'];
        $_SESSION['last_activity'] = time();
        header("Location: admin/index.php");
        exit;
    } else {
        $error = 'Username atau password salah.';
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Login LPM UNIRU</title>

  <!-- Bootstrap 5 (CDN) -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <!-- Font Awesome -->
  <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css" rel="stylesheet">
  <!-- Google Fonts -->
  <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap" rel="stylesheet">

  <style>
    :root {
      --hijau: #084D36;
      --coklat: #CC9933;
      --cream: #FFFF99;
    }

    body {
      min-height: 100vh;
      display: flex;
      align-items: center;
      justify-content: center;
      background: linear-gradient(135deg, var(--hijau), var(--coklat));
      font-family: "Poppins", sans-serif;
      overflow: hidden;
    }

    .login-card {
      background: rgba(63, 61, 61, 0.15);
      backdrop-filter: blur(10px);
      border-radius: 15px;
      box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
      padding: 2.5rem;
      width: 100%;
      max-width: 420px;
      text-align: center;
      animation: fadeIn 1s ease forwards;
    }

    .login-card img {
      width: 80px;
      margin-bottom: 1rem;
      filter: drop-shadow(0 2px 4px rgba(0,0,0,0.3));
    }

    .login-card h4 {
      color: white;
      font-weight: 700;
      margin-bottom: 1rem;
    }

    .login-card small {
      color: #f0f0f0;
      display: block;
      margin-bottom: 1.5rem;
    }

    .form-control {
      background: rgba(255, 255, 255, 0.2);
      border: 1px solid transparent;
      color: #fff;
      border-radius: 8px;
      transition: 0.3s;
    }

    .form-control:focus {
      background: rgba(255, 255, 255, 0.3);
      border-color: var(--coklat);
      box-shadow: none;
      color: #fff;
    }

    .form-control::placeholder { color: rgba(255, 255, 255, 0.8); }

    .btn-login {
      background-color: var(--coklat);
      color: #fff;
      border: none;
      border-radius: 8px;
      padding: 10px 0;
      font-weight: 600;
      transition: all 0.3s ease;
    }

    .btn-login:hover {
      background-color: #b17f2a;
      transform: translateY(-2px);
    }

    .alert-danger {
      background-color: rgba(255, 99, 99, 0.85);
      border: none;
      color: #fff;
      border-radius: 8px;
    }

    .footer-text {
      color: var(--cream);
      margin-top: 1.5rem;
      font-size: 0.9rem;
    }

    .footer-text a {
      color: #fff;
      text-decoration: underline;
    }

    @keyframes fadeIn {
      from { opacity: 0; transform: translateY(25px); }
      to { opacity: 1; transform: translateY(0); }
    }

    @media (max-width: 576px) {
      .login-card {
        padding: 2rem;
        margin: 1rem;
      }
    }
  </style>
</head>
<body>
  <div class="login-card">
    <img src="assets/img/logo-uniru.png" alt="Logo LPM UNIRU"> <!-- Ganti path sesuai logo -->
    <h4>Login LPM UNIRU</h4>

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

    <form method="POST" novalidate>
      <div class="mb-3 text-start">
        <label class="form-label fw-semibold text-white">Username</label>
        <input type="text" name="username" class="form-control" placeholder="Masukkan username..." required autofocus>
      </div>

      <div class="mb-3 text-start">
        <label class="form-label fw-semibold text-white">Password</label>
        <div class="input-group">
          <input type="password" name="password" id="password" class="form-control" placeholder="Masukkan password..." required>
          <button class="btn btn-outline-light" type="button" id="togglePassword">
            <i class="fa-solid fa-eye"></i>
          </button>
        </div>
      </div>

      <button type="submit" class="btn btn-login w-100 mt-3">
        <i class="fa-solid fa-right-to-bracket me-1"></i> Masuk
      </button>
    </form>

    <div class="footer-text">
      <small>&copy; <?= date('Y') ?> LPM Universitas Riyadlul Ulum</small>
    </div>
  </div>

  <!-- Bootstrap JS -->
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
  <script>
    const toggleBtn = document.getElementById('togglePassword');
    const pwdField = document.getElementById('password');
    toggleBtn.addEventListener('click', () => {
      const type = pwdField.type === 'password' ? 'text' : 'password';
      pwdField.type = type;
      toggleBtn.innerHTML = type === 'password'
        ? '<i class="fa-solid fa-eye"></i>'
        : '<i class="fa-solid fa-eye-slash"></i>';
    });
  </script>
</body>
</html>
