<?php
/**
 * SISPENRA - Halaman Login
 */
session_start();
require_once 'config/database.php';
require_once 'includes/functions.php';

// Redirect jika sudah login
if (isLoggedIn()) {
    $role = $_SESSION['role'];
    $dest = $role === 'admin' ? 'admin.php' : ($role === 'siswa' ? 'siswa.php' : 'guru.php');
    header('Location: ' . url($dest));
    exit;
}

$error = '';

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

    if (empty($username) || empty($password)) {
        $error = 'Username dan password wajib diisi.';
    } else {
        $user = login($username, $password);
        if ($user) {
            $dest = $user['role'] === 'admin' ? 'admin.php' : ($user['role'] === 'siswa' ? 'siswa.php' : 'guru.php');
            header('Location: ' . url($dest));
            exit;
        } else {
            $error = 'Username atau password salah.';
        }
    }
}

$sekolah = getPengaturan();
$base = BASE_URL;
?>
<!DOCTYPE html>
<html lang="id">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Login - SISPEN-SNESAKA</title>
  <link rel="stylesheet" href="<?= $base ?>/style.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
</head>
<body>
<div class="login-wrapper">
  <div class="login-card">

    <div class="login-logo">
      <?php if (!empty($sekolah['logo_sekolah']) && file_exists(__DIR__ . '/' . $sekolah['logo_sekolah'])): ?>
        <img src="<?= $base ?>/<?= htmlspecialchars($sekolah['logo_sekolah']) ?>?v=<?= filemtime(__DIR__ . '/' . $sekolah['logo_sekolah']) ?>"
             alt="Logo <?= htmlspecialchars($sekolah['nama_sekolah'] ?? 'Sekolah') ?>"
             style="width:80px;height:80px;object-fit:contain;border-radius:12px;margin-bottom:4px;
                    filter:drop-shadow(0 2px 8px rgba(0,0,0,.25));">
      <?php else: ?>
      <div class="icon">
        <svg width="32" height="32" viewBox="0 0 32 32" fill="none">
          <path d="M16 4L6 9v14l10 5 10-5V9L16 4z" fill="rgba(255,255,255,.9)"/>
          <path d="M16 4v22M6 9l10 5 10-5" stroke="rgba(255,255,255,.4)" stroke-width="1.5"/>
        </svg>
      </div>
      <?php endif; ?>
      <h1>SISPEN-SNESAKA</h1>
      <p>Sistem Informasi Penilaian Siswa</p>
      <?php if ($sekolah): ?>
      <p style="font-size:12px;color:var(--text-muted);margin-top:4px;"><?= htmlspecialchars($sekolah['nama_sekolah']) ?></p>
      <?php endif; ?>
    </div>

    <?php if ($error): ?>
    <div class="alert alert-danger">
      <i class="fas fa-exclamation-circle"></i> <?= htmlspecialchars($error) ?>
    </div>
    <?php endif; ?>

    <form method="POST" action="">
      <div class="form-group">
        <label class="form-label">Username</label>
        <div style="position:relative;">
          <i class="fas fa-user" style="position:absolute;left:12px;top:50%;transform:translateY(-50%);color:var(--text-muted);font-size:13px;"></i>
          <input type="text" name="username" class="form-control" style="padding-left:36px;"
                 placeholder="Masukkan username" value="<?= htmlspecialchars($_POST['username'] ?? '') ?>" autofocus>
        </div>
      </div>

      <div class="form-group">
        <label class="form-label">Password</label>
        <div style="position:relative;">
          <i class="fas fa-lock" style="position:absolute;left:12px;top:50%;transform:translateY(-50%);color:var(--text-muted);font-size:13px;"></i>
          <input type="password" name="password" id="passwordInput" class="form-control" style="padding-left:36px;padding-right:40px;"
                 placeholder="Masukkan password">
          <button type="button" onclick="togglePassword()" style="position:absolute;right:10px;top:50%;transform:translateY(-50%);background:none;border:none;cursor:pointer;color:var(--text-muted);">
            <i class="fas fa-eye" id="toggleIcon"></i>
          </button>
        </div>
      </div>

      <button type="submit" class="btn btn-primary" style="width:100%;justify-content:center;padding:11px;">
        <i class="fas fa-sign-in-alt"></i> Masuk
      </button>
    </form>

    <p style="text-align:center;margin-top:20px;font-size:12px;color:var(--text-light);">
      Tahun Ajaran <?= htmlspecialchars($sekolah['tahun_ajaran_aktif'] ?? '2024/2025') ?> &bull; Semester <?= $sekolah['semester_aktif'] ?? '1' ?>
    </p>
  </div>
</div>

<script>
function togglePassword() {
  const input = document.getElementById('passwordInput');
  const icon = document.getElementById('toggleIcon');
  if (input.type === 'password') {
    input.type = 'text'; icon.className = 'fas fa-eye-slash';
  } else {
    input.type = 'password'; icon.className = 'fas fa-eye';
  }
}
</script>
</body>
</html>