<?php
require_once __DIR__ . '/helpers.php';
require_once __DIR__ . '/auth.php';
?>
<!doctype html>
<html lang="id">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?= sanitize(APP_NAME); ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
    <link rel="stylesheet" href="<?= base_url('assets/css/style.css'); ?>">
</head>
<body>
<header class="site-header">
    <nav class="navbar">
        <div class="nav-brand">
            <a href="<?= base_url('public/index.php?page=home'); ?>">
                <img src="<?= base_url('assets/img/logo.png'); ?>" alt="Al-Qanuni Group">
                <div class="brand-text">
                    <span class="brand-title">AL-Qanuni Group</span>
                    <span class="brand-subtitle">Pusat oleh-oleh Haji & Umroh</span>
                </div>
            </a>
        </div>
        <div class="nav-links">
            <a href="<?= base_url('public/index.php?page=home'); ?>">Beranda</a>
            <a href="<?= base_url('public/index.php?page=products'); ?>">Produk</a>
            <a href="<?= base_url('public/index.php?page=about'); ?>">Tentang</a>
            <a href="<?= base_url('public/index.php?page=cart'); ?>">Keranjang</a>
            <?php if (is_logged_in()): ?>
                <div class="profile-menu">
                    <button class="profile-btn" onclick="toggleProfileMenu(event)">
                        <i class="bi bi-person-circle"></i>
                        <span><?= sanitize(strtok(current_user()['name'], ' ')); ?></span>
                        <i class="bi bi-chevron-down small" style="font-size: 0.8em;"></i>
                    </button>
                    <div class="profile-dropdown" id="profileDropdown">
                        <div class="dropdown-header">
                            <strong>Halo, <?= sanitize(current_user()['name']); ?></strong>
                            <small class="text-muted d-block"><?= sanitize(current_user()['email']); ?></small>
                        </div>
                        <div class="dropdown-divider"></div>
                        <a href="<?= base_url('pages/profile.php'); ?>" class="dropdown-item">
                            <i class="bi bi-person me-2"></i> Edit Profil
                        </a>
                        <a href="<?= base_url('public/index.php?page=orders'); ?>" class="dropdown-item">
                            <i class="bi bi-bag me-2"></i> Pesanan Saya
                        </a>
                        <div class="dropdown-divider"></div>
                        <a href="<?= base_url('pages/logout.php'); ?>" class="dropdown-item text-danger">
                            <i class="bi bi-box-arrow-right me-2"></i> Keluar
                        </a>
                    </div>
                </div>
                <script>
                function toggleProfileMenu(e) {
                    e.stopPropagation();
                    document.getElementById('profileDropdown').classList.toggle('show');
                }
                // Close dropdown when clicking outside
                window.addEventListener('click', function(e) {
                    if (!e.target.closest('.profile-dropdown')) {
                        document.getElementById('profileDropdown').classList.remove('show');
                    }
                });
                </script>
            <?php else: ?>
                <a href="<?= base_url('pages/login.php'); ?>" class="btn-login">Masuk</a>
            <?php endif; ?>
        </div>
    </nav>
</header>
<main class="container">
    <?php if ($message = flash_message('success')): ?>
        <div class="alert success"><?= sanitize($message); ?></div>
    <?php endif; ?>
    <?php if ($message = flash_message('error')): ?>
        <div class="alert error"><?= sanitize($message); ?></div>
    <?php endif; ?>
