<?php
require_once __DIR__ . '/config/db.php';
require_once __DIR__ . '/includes/functions.php';

if (isset($_SESSION['siswa_auth'])) {
    redirect(baseUrl('diagnosa.php'));
}

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

    $stmt = $pdo->prepare("SELECT * FROM siswa WHERE nis = ?");
    $stmt->execute(array($nis));
    $siswa = $stmt->fetch();

    if ($siswa && password_verify($password, $siswa['password'])) {
        $_SESSION['siswa_auth'] = array(
            'nis' => $siswa['nis'],
            'nama' => $siswa['nama'],
            'id_kelas' => $siswa['id_kelas']
        );
        // Clear any previous "please login" messages
        if (isset($_SESSION['flash']) && $_SESSION['flash']['message'] === 'Silahkan login terlebih dahulu.') {
            unset($_SESSION['flash']);
        }
        redirect(baseUrl('diagnosa.php'));
    } else {
        $error = "NIS 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 Siswa - SMAN 1 Barumun</title>
    <link rel="stylesheet" href="<?= baseUrl('dist/style.css') ?>">
</head>
<body class="relative flex items-center justify-center min-h-screen p-6 overflow-hidden">
    <!-- Background Image with Overlay -->
    <div class="absolute inset-0 z-0">
        <img src="<?= baseUrl('header.webp') ?>" alt="Background" class="w-full h-full object-cover">
        <div class="absolute inset-0 bg-gradient-to-r from-primary/95 to-primary-dark/80"></div>
    </div>

    <div class="max-w-md w-full relative z-10">
        <div class="text-center mb-10 flex flex-col items-center">
            <div class="flex items-center gap-5 mb-2">
                <div class="w-16 h-16 bg-white/10 rounded-2xl p-2 backdrop-blur-md border border-white/20 shadow-xl">
                    <img src="<?= baseUrl('logo.png') ?>" alt="Logo" class="w-full h-full object-contain">
                </div>
                <div class="text-left">
                    <h1 class="text-3xl font-black text-white tracking-tight leading-none mb-1">SMA Neg. 1<span class="text-accent"> Barumun</span></h1>
                    <p class="text-primary-light font-bold uppercase tracking-[0.2em] text-[10px] opacity-70">Siswa Login Portal</p>
                </div>
            </div>
        </div>

        <div class="bg-white rounded-3xl p-10 shadow-2xl shadow-primary/5 border border-slate-100">
            <h2 class="text-2xl font-bold text-slate-800 mb-2">Halo, Siswa XII!</h2>
            <p class="text-slate-500 mb-8 leading-relaxed">Silahkan masuk untuk memulai diagnosa kesehatan mental Anda.</p>

            <?php if (isset($error)): ?>
                <div class="mb-6 p-4 rounded-xl bg-red-100 text-red-700 border border-red-200 text-sm font-medium">
                    <?= $error ?>
                </div>
            <?php endif; ?>

            <form action="" method="POST" class="space-y-6">
                <div>
                    <label class="block text-sm font-bold text-slate-700 mb-2">NIS (Username)</label>
                    <input type="text" name="nis" required class="w-full px-5 py-4 rounded-xl border border-slate-200 focus:border-primary focus:ring-4 focus:ring-primary/10 outline-none transition-all" placeholder="Nomor Induk Siswa">
                </div>
                <div>
                    <label class="block text-sm font-bold text-slate-700 mb-2">Password</label>
                    <input type="password" name="password" required class="w-full px-5 py-4 rounded-xl border border-slate-200 focus:border-primary focus:ring-4 focus:ring-primary/10 outline-none transition-all" placeholder="Masukkan password">
                    <p class="text-[10px] text-slate-400 mt-2 italic">*Gunakan NIS sebagai password default jika belum diubah.</p>
                </div>
                <button type="submit" class="w-full btn-primary py-4 font-bold text-lg">Mulai Sekarang</button>
            </form>
            
            <div class="mt-8 pt-8 border-t border-slate-50 text-center">
                <a href="index.php" class="text-sm font-bold text-slate-400 hover:text-primary transition-colors flex items-center justify-center gap-2">
                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"></path></svg>
                    Kembali ke Beranda
                </a>
            </div>
        </div>
        
        <p class="text-center text-white/40 mt-10 text-sm">
            &copy; <?= date('Y') ?> SMA Negeri 1 Barumun.
        </p>
    </div>
</body>
</html>
