<?php
session_start();
require_once "../config/db.php";

$username = $_POST['username'];
$password = $_POST['password'];

$query = $db->prepare("SELECT * FROM users WHERE username = ?");
$query->execute([$username]);
$user = $query->fetch(PDO::FETCH_ASSOC);

if ($user && password_verify($password, $user['password'])) {

    $_SESSION['admin'] = true;
    $_SESSION['user_id'] = $user['id'];
    $_SESSION['username'] = $user['username'];
    $_SESSION['nama'] = $user['nama'];
    $_SESSION['role'] = $user['role']; // ⬅ ROLE DISIMPAN

    // Redirect sesuai role
    if ($user['role'] === "superadmin") {
        header("Location: ../pages/dashboard.php");
    } else if ($user['role'] === "guru") {
        header("Location: ../pages/dashboard_guru.php");
    }
    exit;
}

header("Location: login.php?error=1");
exit;
?>
