<?php
/**
 * Global Header Template
 * CMTC Shopping
 */
ob_start();
require_once __DIR__ . '/../config/db.php';
require_once __DIR__ . '/../functions/helpers.php';

$database = new Database();
$db = $database->getConnection();

// Auto-update admin user display name to ADMIN
try {
    $db->exec("UPDATE users SET prefix = '', firstname = 'ADMIN', lastname = '' WHERE username = 'admin' OR id = 1");
    if (isset($_SESSION['user_id']) && ($_SESSION['user_id'] == 1 || ($_SESSION['username'] ?? '') === 'admin')) {
        $_SESSION['fullname'] = 'ADMIN';
    }
} catch (Exception $e) {
    // Ignore error
}

// Load system settings
$sys_settings = [
    'system_name' => 'CMTC Smart Outfit Shopping & Booking',
    'theme_color' => 'primary',
    'main_color' => '#0d6efd',
    'footer_text' => '© 2026 CMTC Smart Outfit Shopping & Booking. All Rights Reserved.',
    'version' => '1.0.0',
    'booking_open' => 1,
    'limit_per_person' => 5
];
try {
    if ($db) {
        $stmt_settings = $db->query("SELECT * FROM system_settings LIMIT 1");
        if ($stmt_settings) {
            $fetched_settings = $stmt_settings->fetch();
            if ($fetched_settings) {
                $sys_settings = array_merge($sys_settings, $fetched_settings);
            }
        }
    }
} catch (Throwable $e) {}

// Session timeout handler (30 minutes)
$timeout = 1800; // 30 mins
if (isset($_SESSION['last_activity']) && (time() - $_SESSION['last_activity'] > $timeout)) {
    session_unset();
    session_destroy();
    header("Location: " . base_url('login.php?timeout=1'));
    exit();
}
$_SESSION['last_activity'] = time();
?>
<!DOCTYPE html>
<html lang="th" data-bs-theme="light">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo sanitize($sys_settings['system_name']); ?></title>
    <link rel="icon" type="image/x-icon" href="<?php echo base_url('uploads/' . sanitize($sys_settings['favicon'] ?? '')); ?>">
    
    <!-- Bootstrap 5.3 -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Bootstrap Icons -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css" rel="stylesheet">
    <!-- Font Awesome -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <!-- SweetAlert2 -->
    <link href="https://cdn.jsdelivr.net/npm/sweetalert2@11.7.12/dist/sweetalert2.min.css" rel="stylesheet">
    <!-- DataTables -->
    <link href="https://cdn.datatables.net/1.13.4/css/dataTables.bootstrap5.min.css" rel="stylesheet">
    <!-- Custom CSS -->
    <link href="<?php echo base_url('assets/css/custom.css?v=' . time()); ?>" rel="stylesheet">
    
    <!-- Google Fonts Outfit & Inter -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=Sarabun:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    
    <!-- jQuery -->
    <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
</head>
<body>
<div class="app-container">
