<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% if title %}{{ title }} - {% endif %}Administration GMPL</title>
    
    <!-- Favicon -->
    <link rel="shortcut icon" href="{{ url_for('static', filename='img/favicon.ico') }}">
    
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    
    <!-- Font Awesome -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    
    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap" rel="stylesheet">
    
    <!-- DataTables CSS (if needed) -->
    <link href="https://cdn.datatables.net/1.11.5/css/dataTables.bootstrap5.min.css" rel="stylesheet">
    
    <!-- Custom Admin CSS -->
    <link href="{{ url_for('static', filename='css/admin.css') }}" rel="stylesheet">
    
    {% block extra_css %}{% endblock %}
</head>
<body>
    {% if current_user.is_authenticated %}
        <!-- Sidebar -->
        <div class="sidebar">
            <div class="sidebar-header">
                <img src="{{ url_for('static', filename='img/gmpl_logo.png') }}" alt="GMPL Logo" style="max-width: 100%; max-height: 70px;">
                <div class="sidebar-brand mt-2">Administration</div>
            </div>
            
            <ul class="sidebar-nav nav flex-column">
                <li class="nav-item">
                    <a class="nav-link {% if request.endpoint == 'admin.dashboard' %}active{% endif %}" href="{{ url_for('admin.dashboard') }}">
                        <i class="fas fa-tachometer-alt"></i> Tableau de bord
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link {% if request.endpoint == 'admin.products' or request.endpoint == 'admin.add_product' or request.endpoint == 'admin.edit_product' %}active{% endif %}" href="{{ url_for('admin.products') }}">
                        <i class="fas fa-box"></i> Produits
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link {% if request.endpoint == 'admin.categories' or request.endpoint == 'admin.add_category' or request.endpoint == 'admin.edit_category' %}active{% endif %}" href="{{ url_for('admin.categories') }}">
                        <i class="fas fa-tags"></i> Catégories
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link {% if request.endpoint == 'admin.services' or request.endpoint == 'admin.add_service' or request.endpoint == 'admin.edit_service' %}active{% endif %}" href="{{ url_for('admin.services') }}">
                        <i class="fas fa-tools"></i> Services
                    </a>
                </li>

                <li class="nav-item">
                    <a class="nav-link {% if request.endpoint == 'admin.contacts' or request.endpoint == 'admin.view_contact' %}active{% endif %}" href="{{ url_for('admin.contacts') }}">
                        <i class="fas fa-envelope"></i> Messages
                        {% set unread_count = contacts|selectattr('is_read', 'equalto', false)|list|length if contacts is defined else 0 %}
                        {% if unread_count > 0 %}
                            <span class="badge bg-danger rounded-pill ms-2" id="unreadMessageCount">{{ unread_count }}</span>
                        {% endif %}
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link {% if request.endpoint == 'admin.users' or request.endpoint == 'admin.add_user' or request.endpoint == 'admin.edit_user' %}active{% endif %}" href="{{ url_for('admin.users') }}">
                        <i class="fas fa-users"></i> Utilisateurs
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link {% if request.endpoint == 'admin.pages' or request.endpoint == 'admin.add_page' or request.endpoint == 'admin.edit_page' %}active{% endif %}" href="{{ url_for('admin.pages') }}">
                        <i class="fas fa-file-alt"></i> Pages
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link {% if request.endpoint == 'admin.sections' or request.endpoint == 'admin.add_section' or request.endpoint == 'admin.edit_section' %}active{% endif %}" href="{{ url_for('admin.sections') }}">
                        <i class="fas fa-puzzle-piece"></i> Sections
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link {% if request.endpoint == 'admin.settings' %}active{% endif %}" href="{{ url_for('admin.settings') }}">
                        <i class="fas fa-cog"></i> Paramètres
                    </a>
                </li>
                <li class="nav-item mt-4">
                    <a class="nav-link" href="{{ url_for('admin.logout') }}">
                        <i class="fas fa-sign-out-alt"></i> Déconnexion
                    </a>
                </li>
            </ul>
        </div>

        <!-- Page Content -->
        <div class="content">
            <!-- Top Navbar -->
            <nav class="navbar navbar-expand-lg navbar-light admin-navbar mb-4">
                <div class="container-fluid">
                    <button class="btn btn-primary" id="sidebarCollapse">
                        <i class="fas fa-bars"></i>
                    </button>
                    <ul class="navbar-nav ms-auto">
                        <li class="nav-item dropdown">
                            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                                <i class="fas fa-user-circle me-1"></i> {{ current_user.username }}
                            </a>
                            <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
                                <li><a class="dropdown-item" href="{{ url_for('admin.edit_user', id=current_user.id) }}">Mon profil</a></li>
                                <li><hr class="dropdown-divider"></li>
                                <li><a class="dropdown-item" href="{{ url_for('admin.logout') }}">Déconnexion</a></li>
                            </ul>
                        </li>
                    </ul>
                </div>
            </nav>

            <!-- Flash Messages -->
            {% with messages = get_flashed_messages(with_categories=true) %}
                {% if messages %}
                    {% for category, message in messages %}
                        <div class="alert alert-{{ category }} alert-dismissible fade show" role="alert">
                            {{ message }}
                            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                        </div>
                    {% endfor %}
                {% endif %}
            {% endwith %}

            <!-- Main Content -->
            {% block content %}{% endblock %}

            <!-- Footer -->
            <footer class="admin-footer mt-5">
                <p class="mb-0">&copy; {{ now.year }} GMPL - Administration. Tous droits réservés.</p>
            </footer>
        </div>
    {% else %}
        <!-- Login Content -->
        {% block login_content %}{% endblock %}
    {% endif %}

    <!-- Delete Confirmation Modal -->
    <div class="modal fade" id="deleteModal" tabindex="-1" aria-labelledby="deleteModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header bg-danger text-white">
                    <h5 class="modal-title" id="deleteModalLabel">Confirmation de suppression</h5>
                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    Êtes-vous sûr de vouloir supprimer cet élément ?
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Annuler</button>
                    <form method="post" action="">
                        <button type="submit" class="btn btn-danger">Supprimer</button>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <!-- jQuery (needed for DataTables) -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    
    <!-- Bootstrap Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    
    <!-- DataTables JS (if needed) -->
    <script src="https://cdn.datatables.net/1.11.5/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.11.5/js/dataTables.bootstrap5.min.js"></script>
    
    <!-- Admin JavaScript -->
    <script src="{{ url_for('static', filename='js/admin.js') }}"></script>
    
    {% block extra_js %}{% endblock %}
</body>
</html>
