<?php
if (defined('USER_HEADER_INCLUDED')) return;
define('USER_HEADER_INCLUDED', 1);
// Pastikan variabel berikut sudah didefinisikan sebelum include header.php:
// $isLogin, $userNama, $user, $activePage

// Jika user sudah login tapi data user belum diambil, ambil data user
if($isLogin && !isset($user)) {
    $userId = $_SESSION['user_id'];
    $q = mysqli_query($conn, "SELECT * FROM user WHERE id=$userId");
    $user = mysqli_fetch_assoc($q);
}
?>
<div class="header">
    <div class="logo">
        <img src="../assets/images/LogoAngMansion-Putih.png" alt="Logo" /> AngMansion
    </div>
    <div class="navbar">
        <a href="index.php" class="<?php echo ($activePage==="beranda") ? 'active' : ''; ?>">Beranda</a>
        <a href="kamar.php" class="<?php echo ($activePage==="kamar") ? 'active' : ''; ?>">Kamar</a>
        <?php if($isLogin): ?>
            <a href="layanan_request.php" class="<?php echo ($activePage==="layanan") ? 'active' : ''; ?>">Layanan</a>
            <a href="kontak.php" class="<?php echo ($activePage==="kontak") ? 'active' : ''; ?>">Hubungi Kami</a>
            <a href="riwayat_pemesanan.php" class="<?php echo ($activePage==="riwayat") ? 'active' : ''; ?>">Riwayat Pemesanan</a>
        <?php endif; ?>
        <?php if(!$isLogin): ?>
            <a href="register.php" class="<?php echo ($activePage==="daftar") ? 'active' : ''; ?>">Daftar</a>
            <a href="login.php" class="<?php echo ($activePage==="login") ? 'active' : ''; ?>">Login</a>
        <?php endif; ?>
    </div>
    <?php if($isLogin): ?>
    <div class="user-info">
        <span class="avatar">
            <?php 
            // Default path untuk logo
            $fotoPath = '../assets/images/LogoAngMansion-Putih.png';
            
            // Cek apakah user memiliki foto
            if(isset($user['foto']) && !empty($user['foto'])) {
                $userFotoPath = '../assets/images/profile/' . $user['foto'];
                
                // Cek apakah file foto benar-benar ada
                if(file_exists($userFotoPath)) {
                    $fotoPath = $userFotoPath;
                }
            }
            ?>
            <img src="<?php echo $fotoPath; ?>" alt="avatar" onerror="this.src='../assets/images/LogoAngMansion-Putih.png'" style="width: 100%; height: 100%; object-fit: cover; border-radius: 50%;">
        </span>
        <span class="username"><?php echo htmlspecialchars($userNama); ?></span>
        <a href="profile.php" class="btn">Profil</a>
        <a href="logout.php" class="btn btn-danger">Keluar</a>
    </div>
    <?php endif; ?>
</div> 