<?php
/**
 * Page Header Template
 */
$currentUrl = $_SERVER['REQUEST_URI'] ?? '';
$currentUser = current_user();
$flashMessages = get_flash();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= e($pageTitle ?? APP_NAME) ?></title>
    <link rel="stylesheet" href="<?= APP_URL ?>/assets/css/style.css">
    <link rel="stylesheet" href="<?= APP_URL ?>/assets/css/responsive.css">

    <!-- ======================  PWA — MANIFEST  ========================= -->
    <link rel="manifest" href="/invcount/manifest.json">
    <meta name="theme-color" content="#16213e">

    <!-- iOS home-screen: suppress Safari chrome -->
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="apple-mobile-web-app-title" content="InvCounter">

    <!-- Favicon / touch icon (reuses 192×192 source for quickest look-up) -->
    <link rel="icon" type="image/png" sizes="32x32"
          href="<?= APP_URL ?>/assets/images/icons/icon-192x192.png">
    <link rel="apple-touch-icon"
          href="<?= APP_URL ?>/assets/images/icons/icon-192x192.png">

    <!-- PWA Stylesheets -->
    <link rel="stylesheet" href="<?= APP_URL ?>/assets/css/pwa.css">

    <!-- PWA Helper Scripts (Service Worker, Background Sync, Notifications) -->
    <script src="<?= APP_URL ?>/assets/js/pwa-helper.js" defer></script>
    <script src="<?= APP_URL ?>/assets/js/sync-manager.js" defer></script>
    <script src="<?= APP_URL ?>/assets/js/notification-handler.js" defer></script>
    <!-- ================================================================= -->
</head>
<body>
<?php if (SessionManager::isLoggedIn()): ?>
<div class="app-layout">
    <?php include APP_ROOT . '/templates/sidebar.php'; ?>
    <div class="main-wrapper">
        <header class="topbar">
            <button class="sidebar-toggle" onclick="toggleSidebar()">&#9776;</button>
            <div class="topbar-title"><?= e($pageTitle ?? 'Dashboard') ?></div>
            <div class="topbar-actions">
                <div class="user-menu">
                    <div class="user-avatar"><?= strtoupper(substr($currentUser['full_name'] ?? 'U', 0, 1)) ?></div>
                    <div class="user-info">
                        <span class="user-name"><?= e($currentUser['full_name'] ?? 'User') ?></span>
                        <span class="user-role"><?= e(ucfirst($currentUser['role'] ?? '')) ?></span>
                    </div>
                    <a href="<?= APP_URL ?>/logout.php" class="btn-logout" title="Logout">&#10140;</a>
                </div>
            </div>
        </header>
        <main class="main-content">
<?php else: ?>
<body class="auth-body">
<?php endif; ?>

<?php if (!empty($flashMessages)): ?>
<div class="flash-container">
    <?php foreach ($flashMessages as $flash): ?>
    <div class="alert alert-<?= e($flash['type']) ?>" onclick="this.remove()">
        <span><?= e($flash['message']) ?></span>
        <button type="button" class="alert-close">&times;</button>
    </div>
    <?php endforeach; ?>
</div>
<?php endif; ?>
