<?php
/**
 * Purchase Manager Auth Check
 * Verify if user is logged in, if not redirect to login
 */

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Check if purchase manager is logged in
if (!isset($_SESSION['purchase_manager_id']) || empty($_SESSION['purchase_manager_id'])) {
    // Redirect to login page
    header('Location: index.php');
    exit;
}

// Session timeout check
$timeout = 1800; // 30 minutes
$_SESSION['last_activity'] = $_SESSION['last_activity'] ?? time();

if (time() - $_SESSION['last_activity'] > $timeout) {
    session_destroy();
    header('Location: index.php?session=expired');
    exit;
}

// Update last activity
$_SESSION['last_activity'] = time();

// Make user info available globally
$purchase_manager_id = $_SESSION['purchase_manager_id'];
$purchase_manager_name = $_SESSION['purchase_manager_name'] ?? 'Purchase Manager';
$purchase_manager_email = $_SESSION['purchase_manager_email'] ?? '';
$purchase_manager_phone = $_SESSION['purchase_manager_phone'] ?? '';
?>
