<?php
// config/session.php

// Start session kalau belum start
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

/**
 * Check if user is logged in
 */
function checkLogin() {
    if (!isset($_SESSION['user_id'])) {
        header("Location: ../auth/login.php");
        exit();
    }
}

/**
 * Check user role
 * @param string $role
 */
function checkRole($role) {
    if (!isset($_SESSION['role']) || $_SESSION['role'] != $role) {
        echo "<h3>Access Denied!</h3>";
        exit();
    }
}

/**
 * Optional: Redirect based on role (useful for dashboard)
 */
function redirectByRole() {
    if ($_SESSION['role'] == "staff") {
        header("Location: ../dashboard_staff.php");
    } elseif ($_SESSION['role'] == "technician") {
        header("Location: ../dashboard_technician.php");
    }
    exit();
}
?>
