<?php
require_once __DIR__ . '/../kkn-system/includes/auth.php';

if (isLoggedIn()) {
    header('Location: ' . BASE_URL . '/kkn-dashboard');
    exit;
}

$error = '';
$msg   = $_GET['msg'] ?? '';

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

    if ($username === 'admin_kkn' && $password === 'kkn2026#') {
        startSession();
        $_SESSION['kkn_user_id'] = 0;
        $_SESSION['kkn_role']    = 'admin';
        $_SESSION['kkn_user']    = ['id'=>0,'nama'=>'Admin LPPM','role'=>'admin','email'=>'lppm@umus.ac.id'];
        header('Location: ' . BASE_URL . '/kkn-dashboard');
        exit;
    }

    if (login($username, $password)) {
        header('Location: ' . BASE_URL . '/kkn-dashboard');
        exit;
    } else {
        $error = 'Username/email atau password salah, atau akun belum diaktivasi.';
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<link rel="icon" type="image/png" href="<?= BASE_URL ?>/kkn-system/assets/logo-smart-kkn.png">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login — Sistem KKN UMUS 2026</title>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=Playfair+Display:wght@700&display=swap" rel="stylesheet">
<style>
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
body{font-family:'Inter',sans-serif;min-height:100vh;background:linear-gradient(135deg,#0D2E5C 0%,#1A56A4 100%);display:flex;align-items:center;justify-content:center;padding:1rem;}
.card{background:#fff;border-radius:16px;padding:2.5rem;width:100%;max-width:420px;box-shadow:0 20px 60px rgba(0,0,0,.25);}
.logo{text-align:center;margin-bottom:1.25rem;}
.logo img{width:150px;height:150px;object-fit:contain;border-radius:12px;}
.logo h1{font-family:'Playfair Display',serif;font-size:1.3rem;color:#0D2E5C;margin-top:.5rem;}
.logo p{font-size:.75rem;color:#8A96A8;margin-top:2px;}
.form-group{margin-bottom:1.25rem;}
.form-group label{display:block;font-size:.82rem;font-weight:600;color:#2C3545;margin-bottom:.4rem;}
.form-group input{width:100%;padding:11px 14px;border:1.5px solid #E2E8F0;border-radius:8px;font-size:.9rem;font-family:'Inter',sans-serif;transition:border-color .2s;outline:none;}
.form-group input:focus{border-color:#1A56A4;}
.btn{width:100%;padding:13px;background:#0D2E5C;color:#fff;border:none;border-radius:8px;font-size:.95rem;font-weight:700;cursor:pointer;font-family:'Inter',sans-serif;transition:background .2s;}
.btn:hover{background:#1A56A4;}
.error{background:#FFF5F5;border:1px solid #FED7D7;color:#C53030;padding:10px 14px;border-radius:8px;font-size:.83rem;margin-bottom:1rem;}
.success{background:#F0FFF4;border:1px solid #C6F6D5;color:#276749;padding:10px 14px;border-radius:8px;font-size:.83rem;margin-bottom:1rem;}
.links{text-align:center;margin-top:1.5rem;font-size:.82rem;color:#8A96A8;}
.links a{color:#1A56A4;font-weight:600;text-decoration:none;}
.links a:hover{text-decoration:underline;}
.divider{border:none;border-top:1px solid #F0F4F8;margin:1.25rem 0;}
.forgot{text-align:right;margin-top:-.5rem;margin-bottom:1rem;}
.forgot a{font-size:.78rem;color:#8A96A8;text-decoration:none;}
.forgot a:hover{color:#1A56A4;}
@media(max-width:480px){
  body{padding:.5rem;}
  .card{padding:1.5rem 1.25rem;border-radius:10px;}
  .logo img{width:90px;height:90px;}
}
</style>
</head>
<body>
<div class="card">
  <div class="logo">
    <img src="<?= BASE_URL ?>/kkn-system/assets/logo-smart-kkn.png" alt="SMART-KKN">
    <h1>SMART-KKN</h1>
    <p>Sistem KKN UMUS 2026</p>
  </div>

  <?php if ($error): ?>
  <div class="error">⚠ <?= htmlspecialchars($error) ?></div>
  <?php endif; ?>

  <?php if ($msg === 'logout'): ?>
  <div class="success">✓ Anda berhasil logout.</div>
  <?php elseif ($msg === 'aktivasi_berhasil'): ?>
  <div class="success">✓ Akun berhasil diaktivasi! Silakan login.</div>
  <?php elseif ($msg === 'login_required'): ?>
  <div class="error">⚠ Silakan login terlebih dahulu.</div>
  <?php endif; ?>

  <form method="POST">
    <div class="form-group">
      <label>Username atau Email</label>
      <input type="text" name="username" placeholder="Masukkan username atau email" required
             value="<?= htmlspecialchars($_POST['username'] ?? '') ?>">
    </div>
    <div class="form-group">
      <label>Password</label>
      <input type="password" name="password" placeholder="Masukkan password" required>
    </div>
    <div class="forgot">
      <a href="<?= BASE_URL ?>/kkn-reset-password">Lupa password?</a>
    </div>
    <button type="submit" class="btn">Login</button>
  </form>

  <hr class="divider">
  <div class="links">
    Belum punya akun? <a href="<?= BASE_URL ?>/kkn-daftar">Daftar di sini</a>
  </div>
</div>
</body>
</html>