<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block title %}AtlasUI - MongoDB Atlas Administration{% endblock %}</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css">
    <link rel="stylesheet" href="{{ url_for('static', path='/css/style.css') }}">
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-success">
        <div class="container-fluid">
            <a class="navbar-brand" href="/">
                <i class="bi bi-database"></i> AtlasUI
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav">
                    <li class="nav-item">
                        <a class="nav-link" href="/organizations">
                            <i class="bi bi-diagram-3"></i> Organizations
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/projects">
                            <i class="bi bi-folder"></i> Projects
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/clusters">
                            <i class="bi bi-hdd-rack"></i> Clusters
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/clusters">
                            <i class="bi bi-database"></i> Databases
                        </a>
                    </li>
                </ul>
                <ul class="navbar-nav ms-auto">
                    <li class="nav-item">
                        <button class="btn btn-outline-light btn-sm" id="exitServerBtn" onclick="exitServer()">
                            <i class="bi bi-power"></i> Exit
                        </button>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="container-fluid mt-2">
        {% block content %}{% endblock %}
    </div>

    <footer class="mt-5 py-3 bg-light">
        <div class="container text-center">
            <span class="text-muted">AtlasUI v{{ app_version }} - MongoDB Atlas Administration Interface</span>
            <br>
            <span class="text-muted small">by <a href="mailto:{{ app_author }}" class="text-decoration-none">{{ app_author }}</a></span>
        </div>
    </footer>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="{{ url_for('static', path='/js/app.js') }}"></script>
    <script>
        async function exitServer() {
            if (confirm('Are you sure you want to exit the server?\n\nThis will shut down AtlasUI and close all active MongoDB sessions.')) {
                try {
                    const response = await fetch('/api/server/shutdown', {
                        method: 'POST'
                    });

                    if (response.ok) {
                        document.body.innerHTML = '<div style="display: flex; justify-content: center; align-items: center; height: 100vh; flex-direction: column;"><h2>Server Shutting Down</h2><p>AtlasUI server has been shut down successfully.</p><p class="text-muted">You can close this window.</p></div>';
                    } else {
                        alert('Failed to shut down server. Please check the console.');
                    }
                } catch (err) {
                    // Server already shut down, connection refused
                    document.body.innerHTML = '<div style="display: flex; justify-content: center; align-items: center; height: 100vh; flex-direction: column;"><h2>Server Shutting Down</h2><p>AtlasUI server has been shut down successfully.</p><p class="text-muted">You can close this window.</p></div>';
                }
            }
        }
    </script>
    {% block scripts %}{% endblock %}
</body>
</html>
