<?php
session_start();
include 'includes/config.php';

$error = '';

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

    if (empty($no_ic) || empty($kata_laluan)) {
        $error = "Sila isi No Kad Pengenalan dan Kata Laluan.";
    } else {
        $sql = "SELECT * FROM pelajar WHERE no_ic = ? LIMIT 1";
        $stmt = mysqli_prepare($conn, $sql);
        mysqli_stmt_bind_param($stmt, "s", $no_ic);
        mysqli_stmt_execute($stmt);
        $result = mysqli_stmt_get_result($stmt);

        if ($result && mysqli_num_rows($result) == 1) {
            $row = mysqli_fetch_assoc($result);

            if (password_verify($kata_laluan, $row['kata_laluan'])) {
                $_SESSION['id_pelajar'] = $row['id'];
                $_SESSION['nama'] = $row['nama'];
                $_SESSION['no_ic'] = $row['no_ic'];
                $_SESSION['status'] = $row['status'] ?? 'Pelajar';

                header('Location: dashboard_pelajar.php');
                exit;
            } else {
                $error = "Kata laluan salah.";
            }
        } else {
            $error = "No Kad Pengenalan tidak ditemui. Sila daftar jika belum ada akaun.";
        }
    }
}
?>

<!DOCTYPE html>
<html lang="ms">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Login Pelajar - e-SPEKTRA</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css" rel="stylesheet">
<style>
body {
    background: linear-gradient(to right, #6a11cb, #2575fc);
    font-family: 'Rubik', sans-serif;
    height: 100vh;
}
.login-container {
    max-width: 400px;
    margin: auto;
    padding: 2rem;
    background: white;
    border-radius: 12px;
    box-shadow: 0 8px 25px rgba(0,0,0,0.15);
}
.login-header {
    text-align: center;
    margin-bottom: 1.5rem;
}
.login-header h3 {
    font-weight: 700;
    color: #3b0a77;
}
.form-control:focus {
    box-shadow: 0 0 0 0.2rem rgba(58, 123, 213, 0.25);
}
.btn-login {
    background: #3b0a77;
    color: white;
    font-weight: 600;
    transition: 0.3s;
}
.btn-login:hover {
    background: #2a0650;
}
.error-box {
    margin-bottom: 1rem;
    padding: 0.75rem 1rem;
    border-radius: 8px;
    background-color: #f8d7da;
    color: #842029;
    display: flex;
    align-items: center;
}
.error-box i {
    margin-right: 0.5rem;
}
.register-link {
    font-size: 0.9rem;
    text-align: center;
    display: block;
    margin-top: 1rem;
}
</style>
</head>
<body>

<div class="d-flex justify-content-center align-items-center" style="height:100vh;">
    <div class="login-container">
        <div class="login-header">
            <h3>Login Pelajar</h3>
            <p class="text-muted">Sistem e-SPEKTRA</p>
        </div>

        <?php if ($error): ?>
            <div class="error-box"><i class="bi bi-exclamation-circle-fill"></i> <?= htmlspecialchars($error) ?></div>
        <?php endif; ?>

        <form method="post" action="">
            <div class="mb-3">
                <label for="no_ic" class="form-label">No Kad Pengenalan</label>
                <input type="text" id="no_ic" name="no_ic" class="form-control" required autofocus>
            </div>
            <div class="mb-3">
                <label for="password" class="form-label">Kata Laluan</label>
                <input type="password" id="password" name="password" class="form-control" required>
            </div>
            <button type="submit" class="btn btn-login w-100"><i class="bi bi-box-arrow-in-right"></i> Log Masuk</button>
        </form>

        <a href="daftar_pelajar2.php" class="register-link">Belum ada akaun? Daftar di sini</a>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>

</html>
