
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
    <script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap5.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/chart.js@4.3.0/dist/chart.umd.min.js"></script>
    <script>
    var BASE_URL = '<?php echo getBaseUrl(); ?>';
    var CSRF_TOKEN = '<?php echo $csrfToken; ?>';
    var RESTAURANT_ID = '<?php echo $restaurantId; ?>';
    var CURRENCY = '<?php echo $currency; ?>';

    document.getElementById('sidebarToggle').addEventListener('click', function() {
        document.getElementById('sidebar').classList.toggle('collapsed');
        document.getElementById('content').classList.toggle('expanded');
    });

    function loadNotifications() {
        fetch(BASE_URL + '/restaurant-admin/ajax/notifications.php', {
            method: 'GET',
            headers: {'X-Requested-With': 'XMLHttpRequest'}
        })
        .then(function(r) { return r.json(); })
        .then(function(data) {
            if (data.success) {
                var badge = document.querySelector('.notification-badge');
                if (data.unread_count > 0) {
                    badge.textContent = data.unread_count;
                    badge.style.display = 'inline-block';
                } else {
                    badge.style.display = 'none';
                }
                var list = document.getElementById('notificationList');
                if (data.notifications && data.notifications.length > 0) {
                    var html = '';
                    data.notifications.forEach(function(n) {
                        html += '<a class="dropdown-item notification-item ' + (n.is_read == 0 ? 'fw-bold' : '') + '" href="' + (n.url || '#') + '">';
                        html += '<div class="small">' + escapeHtml(n.title) + '</div>';
                        html += '<div class="text-muted" style="font-size:11px;">' + escapeHtml(n.message) + '</div>';
                        html += '</a>';
                    });
                    list.innerHTML = html;
                } else {
                    list.innerHTML = '<div class="text-center p-3 text-muted">No notifications</div>';
                }
            }
        })
        .catch(function() {});
    }

    function escapeHtml(text) {
        var div = document.createElement('div');
        div.textContent = text;
        return div.innerHTML;
    }

    document.getElementById('markAllRead').addEventListener('click', function(e) {
        e.preventDefault();
        fetch(BASE_URL + '/restaurant-admin/ajax/notifications.php', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'X-Requested-With': 'XMLHttpRequest'
            },
            body: 'action=mark_all_read&csrf_token=' + encodeURIComponent(CSRF_TOKEN)
        })
        .then(function(r) { return r.json(); })
        .then(function(data) {
            loadNotifications();
        });
    });

    function checkPendingOrders() {
        fetch(BASE_URL + '/restaurant-admin/ajax/notifications.php?action=pending_orders', {
            method: 'GET',
            headers: {'X-Requested-With': 'XMLHttpRequest'}
        })
        .then(function(r) { return r.json(); })
        .then(function(data) {
            if (data.success) {
                var badge = document.querySelector('.pending-orders-badge');
                if (badge) {
                    if (data.count > 0) {
                        badge.textContent = data.count;
                        badge.style.display = 'inline-block';
                    } else {
                        badge.style.display = 'none';
                    }
                }
            }
        })
        .catch(function() {});
    }

    loadNotifications();
    setInterval(loadNotifications, 30000);
    checkPendingOrders();
    setInterval(checkPendingOrders, 15000);

    function showAlert(type, message) {
        var alertHtml = '<div class="alert alert-' + type + ' alert-dismissible fade show" role="alert">';
        alertHtml += message;
        alertHtml += '<button type="button" class="btn-close" data-bs-dismiss="alert"></button>';
        alertHtml += '</div>';
        var content = document.querySelector('.container-fluid');
        content.insertAdjacentHTML('afterbegin', alertHtml);
        setTimeout(function() {
            var alerts = content.querySelectorAll('.alert');
            if (alerts.length > 0) {
                alerts[0].remove();
            }
        }, 5000);
    }

    function ajaxPost(url, data, callback) {
        fetch(url, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'X-Requested-With': 'XMLHttpRequest'
            },
            body: data
        })
        .then(function(r) { return r.json(); })
        .then(function(result) { callback(result); })
        .catch(function(err) {
            callback({success: false, message: 'Network error occurred.'});
        });
    }

    function formatCurrency(amount) {
        var symbols = {'LKR':'Rs.','USD':'$','EUR':'\u20AC','GBP':'\u00A3','INR':'\u20B9'};
        var sym = symbols[CURRENCY] || CURRENCY + ' ';
        return sym + parseFloat(amount).toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
    }
    </script>
    <script src="<?php echo getBaseUrl(); ?>/restaurant-admin/assets/app.js"></script>
</body>
</html>
