<?php
/**
 * header.php - Layout Header untuk semua halaman
 * DIPERBAIKI - Tidak ada spasi di awal file
 */
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// ============================================
// DEFINISI BASE_URL JIKA BELUM ADA
// ============================================
if (!defined('BASE_URL')) {
    define('BASE_URL', '/reservasi-kapal-wisata');
}

// ============================================
// AMBIL DATA USER UNTUK HEADER
// ============================================
$userName = $_SESSION['nama'] ?? 'Pengguna';
$fotoFile = $_SESSION['foto'] ?? $_SESSION['foto_profil'] ?? null;

// Cek keberadaan file foto
$fotoUrl = '';
$fotoExists = false;
$inisial = strtoupper(substr($userName, 0, 1));

if (!empty($fotoFile)) {
    // Cek beberapa kemungkinan path
    $pathsToCheck = [
        __DIR__ . '/../../uploads/profile/' . $fotoFile,
        __DIR__ . '/../uploads/profile/' . $fotoFile,
        $_SERVER['DOCUMENT_ROOT'] . '/reservasi-kapal-wisata/uploads/profile/' . $fotoFile,
    ];
    
    foreach ($pathsToCheck as $path) {
        if (file_exists($path)) {
            $fotoExists = true;
            $fotoUrl = BASE_URL . '/uploads/profile/' . $fotoFile;
            break;
        }
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Reservasi Kapal Wisata Samosir</title>
    <style>
        /* ============================================
           RESET & VARIABLES
           ============================================ */
        * { margin: 0; padding: 0; box-sizing: border-box; }
        :root {
            --primary: #0f3b5e;
            --primary-dark: #09263f;
            --primary-light: #1a5a8a;
            --secondary: #f59e0b;
            --secondary-light: #fbbf24;
            --success: #10b981;
            --danger: #ef4444;
            --gray-50: #f8fafc;
            --gray-100: #f1f5f9;
            --gray-200: #e2e8f0;
            --gray-300: #cbd5e1;
            --gray-400: #94a3b8;
            --gray-500: #64748b;
            --gray-600: #475569;
            --gray-700: #334155;
            --gray-800: #1e293b;
            --gray-900: #0f172a;
            --shadow: 0 4px 24px rgba(0,0,0,0.06);
            --shadow-hover: 0 12px 40px rgba(0,0,0,0.12);
            --radius: 16px;
            --radius-sm: 8px;
            --transition: all 0.25s ease;
        }
        html { scroll-behavior: smooth; }
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background: var(--gray-50);
            color: var(--gray-800);
            line-height: 1.6;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }
        a { text-decoration: none; color: var(--primary); transition: var(--transition); }
        a:hover { color: var(--primary-light); }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 24px;
            width: 100%;
        }

        /* ===== HEADER ===== */
        header {
            background: linear-gradient(135deg, var(--primary-dark), var(--primary));
            color: #fff;
            padding: 0.6rem 0;
            position: sticky;
            top: 0;
            z-index: 1000;
            box-shadow: 0 2px 16px rgba(0,0,0,0.15);
        }
        header .container {
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 0.5rem;
        }
        header .logo {
            font-size: 1.3rem;
            font-weight: 700;
            letter-spacing: -0.5px;
            display: flex;
            align-items: center;
            gap: 0.5rem;
        }
        header .logo span { color: var(--secondary-light); }

        header nav {
            display: flex;
            align-items: center;
            gap: 0.5rem;
            flex-wrap: wrap;
        }
        header nav a {
            color: rgba(255,255,255,0.85);
            padding: 0.3rem 0.7rem;
            border-radius: var(--radius-sm);
            font-weight: 500;
            font-size: 0.8rem;
            transition: var(--transition);
        }
        header nav a:hover {
            background: rgba(255,255,255,0.12);
            color: #fff;
            text-decoration: none;
        }

        header nav .user-badge {
            background: rgba(255,255,255,0.1);
            padding: 0.15rem 0.7rem 0.15rem 0.3rem;
            border-radius: 20px;
            display: flex;
            align-items: center;
            gap: 0.4rem;
            font-size: 0.8rem;
            border: 1px solid rgba(255,255,255,0.08);
        }
        header nav .user-badge .avatar {
            width: 28px;
            height: 28px;
            border-radius: 50%;
            background: var(--secondary);
            color: var(--gray-900);
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 700;
            font-size: 0.7rem;
            overflow: hidden;
            flex-shrink: 0;
        }
        header nav .user-badge .avatar img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            display: block;
        }
        header nav .user-badge .user-name {
            white-space: nowrap;
            font-weight: 500;
            color: #fff;
        }

        header nav .btn-logout {
            background: rgba(239,68,68,0.2);
            padding: 0.2rem 0.7rem;
            border-radius: 4px;
            font-size: 0.7rem;
            border: 1px solid rgba(239,68,68,0.3);
            color: rgba(255,255,255,0.9);
            transition: var(--transition);
        }
        header nav .btn-logout:hover {
            background: rgba(239,68,68,0.4);
            color: #fff;
        }

        /* ===== FOOTER ===== */
        footer {
            background: var(--gray-900);
            color: var(--gray-400);
            text-align: center;
            padding: 1rem 0;
            margin-top: auto;
            border-top: 1px solid var(--gray-700);
            font-size: 0.8rem;
        }

        /* ===== RESPONSIVE HEADER ===== */
        @media (max-width: 768px) {
            header .container { flex-direction: column; align-items: stretch; gap: 0.2rem; }
            header nav { justify-content: center; gap: 0.2rem; flex-wrap: wrap; }
            header nav a { padding: 0.2rem 0.5rem; font-size: 0.7rem; }
            header nav .user-badge { padding: 0.1rem 0.4rem 0.1rem 0.2rem; font-size: 0.7rem; }
            header nav .user-badge .avatar { width: 22px; height: 22px; font-size: 0.6rem; }
            header nav .btn-logout { font-size: 0.65rem; padding: 0.15rem 0.5rem; }
        }
        @media (max-width: 480px) {
            header .logo { font-size: 1rem; }
            header nav a { font-size: 0.65rem; padding: 0.15rem 0.4rem; }
        }
    </style>
</head>
<body>
<header>
    <div class="container">
        <div class="logo">⛵ <span>Reservasi</span> Kapal</div>
        <nav>
            <?php if(isset($_SESSION['user_id'])): ?>
                <span class="user-badge">
                    <span class="avatar" id="headerAvatar">
                        <?php if($fotoExists && !empty($fotoUrl)): ?>
                            <img src="<?= $fotoUrl ?>" 
                                 alt="Foto Profil" 
                                 id="headerFoto"
                                 onerror="console.log('Gambar gagal load:', this.src); this.style.display='none'; this.parentElement.textContent='<?= $inisial ?>';">
                        <?php else: ?>
                            <?= $inisial ?>
                        <?php endif; ?>
                    </span>
                    <span class="user-name"><?= htmlspecialchars($userName) ?></span>
                </span>
                <a href="?page=logout" class="btn-logout">Logout</a>
            <?php else: ?>
                <a href="?page=login">Login</a>
                <a href="?page=register" style="background:rgba(255,255,255,0.12); padding:0.2rem 0.6rem; border-radius:4px;">Daftar</a>
            <?php endif; ?>
        </nav>
    </div>
</header>
<main>