<?php
session_start();

// Admin credentials (in production, use hashed passwords and database storage)
define('ADMIN_USERNAME', 'admin');
define('ADMIN_PASSWORD', 'XChange2025!Admin'); // Change this password!

// Check if user is logged in
function isLoggedIn() {
    return isset($_SESSION['admin_logged_in']) && $_SESSION['admin_logged_in'] === true;
}

// Require login for admin pages
function requireLogin() {
    if (!isLoggedIn()) {
        header('Location: login.php');
        exit;
    }
}

// Handle login
function handleLogin($username, $password) {
    if ($username === ADMIN_USERNAME && $password === ADMIN_PASSWORD) {
        $_SESSION['admin_logged_in'] = true;
        $_SESSION['admin_username'] = $username;
        $_SESSION['login_time'] = time();
        return true;
    }
    return false;
}

// Handle logout
function handleLogout() {
    session_destroy();
    header('Location: login.php');
    exit;
}

// Check for logout request
if (isset($_GET['logout'])) {
    handleLogout();
}

// Auto-logout after 2 hours of inactivity
if (isLoggedIn() && isset($_SESSION['login_time'])) {
    if (time() - $_SESSION['login_time'] > 7200) { // 2 hours
        handleLogout();
    }
}
?>
