<?php
session_start();
include 'koneksi.php';

/* ======================
   JIKA SUDAH LOGIN
====================== */
if (isset($_SESSION['siswa_id'])) {
    header("Location: siswa/dashboard.php");
    exit;
}
if (isset($_SESSION['guru_id'])) {
    header("Location: guru/dashboard.php");
    exit;
}

$error = "";

/* ======================
   PROSES LOGIN
====================== */
if (isset($_POST['login'])) {
    $username = mysqli_real_escape_string($koneksi,$_POST['username']);
    $password = md5($_POST['password']);

    $q = mysqli_query($koneksi,"
        SELECT * FROM users
        WHERE username='$username'
        AND password='$password'
        LIMIT 1
    ");

    if (mysqli_num_rows($q)==1) {
        $u = mysqli_fetch_assoc($q);

        if ($u['role']=='siswa') {
            $_SESSION['siswa_id'] = $u['id'];
            $_SESSION['id']       = $u['id'];   // kompatibel dashboard siswa
            $_SESSION['role']     = 'siswa';
            header("Location: siswa/dashboard.php");
        } 
        elseif ($u['role']=='guru') {
            $_SESSION['guru_id'] = $u['id'];
            $_SESSION['id']      = $u['id'];
            $_SESSION['role']    = 'guru';
            header("Location: guru/dashboard.php");
        }
        exit;
    } else {
        $error = "Username atau password salah!";
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Login | MAYABSEN</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<link href="bootstrap-5.3.8-dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&display=swap" rel="stylesheet">

<style>
body{
    font-family:'Poppins',sans-serif;
    background:linear-gradient(135deg,#eaf2f8,#fdfefe);
    height:100vh;
    display:flex;
    align-items:center;
    justify-content:center;
}
.login-card{
    background:#fff;
    padding:40px 35px;
    border-radius:22px;
    width:100%;
    max-width:420px;
    box-shadow:0 12px 30px rgba(0,0,0,.15);
}
.brand{
    text-align:center;
    font-weight:700;
    font-size:24px;
    color:#1e6f91;
}
.btn-login{
    background:#1e6f91;
    border:none;
    border-radius:14px;
    padding:12px;
}
.btn-login:hover{
    background:#144c66;
}
.register-link{
    text-align:center;
    margin-top:18px;
    font-size:14px;
}
.register-link a{
    font-weight:600;
    color:#1e6f91;
    text-decoration:none;
}
.register-link a:hover{
    text-decoration:underline;
}
</style>
</head>

<body>

<div class="login-card">
    <div class="brand mb-4">
        <i class="bi bi-fingerprint"></i> MAYABSEN
        <div class="text-muted fs-6">Login Siswa / Guru</div>
    </div>

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

    <form method="POST">
        <div class="mb-3">
            <label class="form-label">Username</label>
            <input type="text" name="username" class="form-control rounded-pill px-4" required>
        </div>

        <div class="mb-4">
            <label class="form-label">Password</label>
            <input type="password" name="password" class="form-control rounded-pill px-4" required>
        </div>

        <button name="login" class="btn btn-login text-white w-100">
            <i class="bi bi-box-arrow-in-right"></i> Login
        </button>
    </form>

    <!-- LINK REGISTRASI -->
    <div class="register-link">
        Belum punya akun?
        <a href="registration.php">
            Daftar di sini
        </a>
    </div>
</div>

<script src="bootstrap-5.3.8-dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
