<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    {% block head %}{% endblock %}
    <meta name="csrf-token" content="{{ csrf_token }}">
    <title>{% block title %}{% if site_settings.company_name %}DocuPulse for {{ site_settings.company_name }}{% else %}DocuPulse{% endif %}{% endblock %}</title>
    <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <link href="{{ url_for('static', filename='css/file-grid.css') }}?v={{ 'css/file-grid.css'|asset_version }}" rel="stylesheet">
    <link href="{{ url_for('static', filename='css/base.css') }}?v={{ 'css/base.css'|asset_version }}" rel="stylesheet">
    <link rel="stylesheet" href="{{ url_for('static', filename='css/colors.css') }}?v={{ 'css/colors.css'|asset_version }}">
    <link rel="stylesheet" href="{{ url_for('main.dynamic_colors') }}?v={{ site_settings.updated_at.timestamp() }}" onload="console.log('[CSS] Dynamic colors loaded with version:', '{{ site_settings.updated_at.timestamp() }}')">
    {% block extra_css %}{% endblock %}
    <script src="{{ url_for('static', filename='js/color-logger.js') }}?v={{ 'js/color-logger.js'|asset_version }}"></script>
</head>
<body data-page="{% if request.endpoint == 'main.dashboard' %}dashboard{% elif request.endpoint == 'rooms.rooms' %}rooms{% elif request.endpoint == 'conversations.conversations' %}conversations{% endif %}">
    <!-- Navigation -->
    <nav class="navbar navbar-expand-lg navbar-dark">
        <div class="container-fluid">
            <button class="navbar-toggler border-0 me-2" type="button" id="sidebarToggle">
                <i class="fas fa-bars text-white"></i>
            </button>
            <a class="navbar-brand d-flex align-items-center" href="{{ url_for('main.dashboard') }}">
                {% if site_settings.company_logo %}
                <img src="{{ url_for('static', filename='uploads/company_logos/' + site_settings.company_logo) }}" 
                     alt="Company Logo" 
                     style="height: 40px;">
                {% endif %}
            </a>
            <div class="d-flex align-items-center">
                {% if not is_master %}
                <div class="d-none d-lg-flex align-items-center me-3">
                    <a class="nav-link text-white position-relative" href="{{ url_for('main.notifications') }}">
                        <i class="fas fa-bell text-xl" style="width: 2rem; height: 2rem; display: flex; align-items: center; justify-content: center;"></i>
                        {% if unread_notifications > 0 %}
                        <span class="position-absolute top-0 start-100 translate-middle badge rounded-pill bg-danger" style="font-size: 0.6rem;">
                            {{ unread_notifications }}
                        </span>
                        {% endif %}
                    </a>
                </div>
                {% endif %}
                <div class="dropdown">
                    <a class="nav-link d-flex align-items-center gap-2 d-lg-none" href="{{ url_for('main.profile') }}">
                        <img src="{{ url_for('profile_pic', filename=current_user.profile_picture) if current_user.profile_picture else url_for('static', filename='default-avatar.png') }}"
                             alt="Profile Picture"
                             class="w-8 h-8 rounded-full object-cover border-2 border-white shadow"
                             style="display: inline-block; vertical-align: middle;">
                    </a>
                    <a class="nav-link dropdown-toggle d-flex align-items-center gap-2 d-none d-lg-flex" href="#" id="navbarDropdownDesktop" role="button" data-bs-toggle="dropdown">
                        <img src="{{ url_for('profile_pic', filename=current_user.profile_picture) if current_user.profile_picture else url_for('static', filename='default-avatar.png') }}"
                             alt="Profile Picture"
                             class="w-8 h-8 rounded-full object-cover border-2 border-white shadow"
                             style="display: inline-block; vertical-align: middle;">
                        <span class="text-white font-medium">{{ current_user.username }}</span>
                    </a>
                    <ul class="dropdown-menu dropdown-menu-end">
                        {% if is_master %}
                        <li><a class="dropdown-item" href="{{ url_for('main.profile') }}"><i class="fas fa-user"></i> Profile</a></li>
                        <li><a class="dropdown-item" href="{{ url_for('main.settings') }}"><i class="fas fa-cog"></i> Settings</a></li>
                        <li><hr class="dropdown-divider"></li>
                        {% else %}
                        {% if not is_master %}
                        <li><a class="dropdown-item" href="{{ url_for('main.profile') }}"><i class="fas fa-user"></i> Profile</a></li>
                        {% if current_user.is_admin %}
                        <li><a class="dropdown-item" href="{{ url_for('main.settings') }}"><i class="fas fa-cog"></i> Settings</a></li>
                        {% endif %}
                        <li><hr class="dropdown-divider"></li>
                        {% endif %}
                        {% endif %}
                        <li><a class="dropdown-item" href="{{ url_for('auth.logout') }}"><i class="fas fa-sign-out-alt"></i> Logout</a></li>
                    </ul>
                </div>
            </div>
        </div>
    </nav>

    <div class="container-fluid">
        <div class="row" style="margin-top: -10px;">
            <!-- Sidebar -->
            <div class="col-md-3 col-lg-2 px-0 sidebar" id="sidebar">
                <div class="p-3">
                    <ul class="nav flex-column">
                        <li class="nav-item">
                            <a class="nav-link {% if request.endpoint == 'main.dashboard' %}active{% endif %}" href="{{ url_for('main.dashboard') }}">
                                <i class="fas fa-home"></i> Dashboard
                            </a>
                        </li>
                        {% if is_master %}
                        <li class="nav-item">
                            <a class="nav-link {% if request.endpoint == 'main.instances' %}active{% endif %}" href="{{ url_for('main.instances') }}">
                                <i class="fas fa-server"></i> Instances
                            </a>
                        </li>
                        {% endif %}
                        <li class="nav-item">
                            <a class="nav-link {% if request.endpoint == 'contacts.contacts_list' %}active{% endif %}" href="{{ url_for('contacts.contacts_list') }}">
                                <i class="fas fa-address-book"></i> Contacts
                            </a>
                        </li>
                        {% if not is_master %}
                        <li class="nav-item">
                            <a class="nav-link {% if request.endpoint == 'conversations.conversations' %}active{% endif %}" href="{{ url_for('conversations.conversations') }}">
                                <i class="fas fa-comments"></i> Conversations
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link {% if request.endpoint == 'rooms.rooms' %}active{% endif %}" href="{{ url_for('rooms.rooms') }}">
                                <i class="fas fa-door-open"></i> Rooms
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link {% if request.endpoint == 'main.starred' %}active{% endif %}" href="{{ url_for('main.starred') }}">
                                <i class="fas fa-star"></i> Starred
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link {% if request.endpoint == 'main.trash' %}active{% endif %}" href="{{ url_for('main.trash') }}">
                                <i class="fas fa-trash"></i> Trash
                            </a>
                        </li>
                        {% endif %}
                        <!-- Mobile-only menu items -->
                        {% if not is_master %}
                        <li class="nav-item d-lg-none">
                            <hr class="my-2">
                            <a class="nav-link" href="{{ url_for('main.notifications') }}">
                                <i class="fas fa-bell"></i> Notifications
                            </a>
                        </li>
                        {% if current_user.is_admin %}
                        <li class="nav-item d-lg-none">
                            <a class="nav-link" href="{{ url_for('main.settings') }}">
                                <i class="fas fa-cog"></i> Settings
                            </a>
                        </li>
                        {% endif %}
                        <li class="nav-item d-lg-none">
                            <a class="nav-link" href="{{ url_for('main.profile') }}">
                                <i class="fas fa-user"></i> Profile
                            </a>
                        </li>
                        {% endif %}
                        <li class="nav-item d-lg-none">
                            <a class="nav-link" href="{{ url_for('auth.logout') }}">
                                <i class="fas fa-sign-out-alt"></i> Logout
                            </a>
                        </li>
                    </ul>
                </div>
            </div>

            <!-- Main Content -->
            <div class="col-md-9 col-lg-10 main-content">
                {% block content %}{% endblock %}
            </div>
        </div>
    </div>

    <!-- Overlay for mobile sidebar -->
    <div class="sidebar-overlay" id="sidebarOverlay"></div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const sidebar = document.getElementById('sidebar');
            const sidebarToggle = document.getElementById('sidebarToggle');
            const sidebarOverlay = document.getElementById('sidebarOverlay');
            const mainContent = document.querySelector('.main-content');

            function toggleSidebar() {
                sidebar.classList.toggle('show');
                sidebarOverlay.classList.toggle('show');
                document.body.classList.toggle('sidebar-open');
            }

            sidebarToggle.addEventListener('click', toggleSidebar);
            sidebarOverlay.addEventListener('click', toggleSidebar);

            // Close sidebar when clicking a link on mobile
            const sidebarLinks = sidebar.querySelectorAll('.nav-link');
            sidebarLinks.forEach(link => {
                link.addEventListener('click', () => {
                    if (window.innerWidth < 768) {
                        toggleSidebar();
                    }
                });
            });

            // Handle window resize
            window.addEventListener('resize', () => {
                if (window.innerWidth >= 768) {
                    sidebar.classList.remove('show');
                    sidebarOverlay.classList.remove('show');
                    document.body.classList.remove('sidebar-open');
                }
            });
        });
    </script>
    {% block extra_js %}{% endblock %}
</body>
</html> 