<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Majuro Eats</title>
    <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@400;500;600;700&family=Open+Sans:wght@400;600&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="assets/css/main.css?v=<?php echo time(); ?>">
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" />
</head>
<body>

<header>
    <a href="/" class="logo">Majuro Eats</a>
    <div class="address-container">
        <span id="address-display">Enter delivery address</span>
    </div>
    
    <div class="user-actions">
        <?php
        require_once 'db/config.php';
        $cart_item_count = 0;
        $db = db();
        if (isset($_SESSION['user_id'])) {
            $stmt = $db->prepare('SELECT SUM(quantity) as item_count FROM cart WHERE user_id = ?');
            $stmt->execute([$_SESSION['user_id']]);
            $result = $stmt->fetch();
            if ($result && $result['item_count'] > 0) {
                $cart_item_count = $result['item_count'];
            }
        } else {
            $stmt = $db->prepare('SELECT SUM(quantity) as item_count FROM cart WHERE session_id = ?');
            $stmt->execute([session_id()]);
            $result = $stmt->fetch();
            if ($result && $result['item_count'] > 0) {
                $cart_item_count = $result['item_count'];
            }
        }
        ?>
        <a href="cart.php" class="cart-icon">
            <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="9" cy="21" r="1"></circle><circle cx="20" cy="21" r="1"></circle><path d="M1 1h4l2.68 13.39a2 2 0 0 0 2 1.61h9.72a2 2 0 0 0 2-1.61L23 6H6"></path></svg>
            <span class="cart-item-count"><?= $cart_item_count ?></span>
        </a>
        <?php if (isset($_SESSION['user_id'])): ?>
            <span>Welcome, <?php echo htmlspecialchars($_SESSION['user_name']); ?></span>
            <a href="profile.php">My Profile</a>
            <a href="order_history.php">Order History</a>
            <a href="logout.php">Logout</a>
        <?php else: ?>
            <a href="login.php">Login</a>
            <a href="signup.php">Sign Up</a>
        <?php endif; ?>
    </div>
</header>

<!-- The Modal -->
<div id="address-modal" class="modal">
    <div class="modal-content">
        <span class="close-button">&times;</span>
        <h2>Enter your delivery address</h2>
        <input type="text" id="modal-address-input" placeholder="e.g. Uliga, Majuro">
        <button id="save-address-button">Save Address</button>
    </div>
</div>

<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script>
<script src="assets/js/main.js?v=<?php echo time(); ?>" defer></script>

