<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block title %}ARCH - Sistema de Gestión Documental{% endblock %}</title>
    
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    
    <!-- Font Awesome Icons -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    
    <!-- Custom CSS -->
    <link href="{{ url_for('static', filename='css/main.css') }}" rel="stylesheet">
    
    {% block extra_css %}{% endblock %}
</head>
<body>
    <!-- Navigation -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container">
            <a class="navbar-brand" href="{{ url_for('projects.list') }}">
                <img src="{{ url_for('static', filename='img/logo.png') }}" alt="ARCH" height="30">
                ARCH
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" 
                    aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav me-auto">
                    {% if current_user.is_authenticated %}
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('projects.list') }}">Proyectos</a>
                    </li>
                    {% if current_user.has_permission(5000) %}
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('schemas.list') }}">Esquemas</a>
                    </li>
                    {% endif %}
                    {% if current_user.has_permission(9000) %}
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" id="adminDropdown" role="button" data-bs-toggle="dropdown">
                            Administración
                        </a>
                        <ul class="dropdown-menu">
                            <li><a class="dropdown-item" href="{{ url_for('users.list') }}">Usuarios</a></li>
                            <li><a class="dropdown-item" href="{{ url_for('admin.filetypes') }}">Tipos de archivo</a></li>
                            <li><a class="dropdown-item" href="{{ url_for('admin.system') }}">Estado del sistema</a></li>
                        </ul>
                    </li>
                    {% endif %}
                    {% endif %}
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('main.about') }}">Acerca de</a>
                    </li>
                </ul>
                <ul class="navbar-nav">
                    {% if current_user.is_authenticated %}
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                            <i class="fas fa-user-circle me-1"></i> {{ current_user.nombre }}
                        </a>
                        <ul class="dropdown-menu dropdown-menu-end">
                            {% if current_user.has_permission(5000) %}
                            <li><a class="dropdown-item" href="{{ url_for('users.list') }}">Usuarios</a></li>
                            {% endif %}
                            {% if current_user.has_permission(9000) %}
                            <li><a class="dropdown-item" href="{{ url_for('admin.dashboard') }}">Administración</a></li>
                            <li><hr class="dropdown-divider"></li>
                            {% endif %}
                            <li><a class="dropdown-item" href="{{ url_for('auth.profile') }}">Mi perfil</a></li>
                            <li><a class="dropdown-item" href="{{ url_for('auth.logout') }}">Cerrar sesión</a></li>
                        </ul>
                    </li>
                    {% else %}
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('auth.login') }}">Iniciar sesión</a>
                    </li>
                    {% endif %}
                </ul>
            </div>
        </div>
    </nav>

    <!-- Flash Messages -->
    {% with messages = get_flashed_messages(with_categories=true) %}
        {% if messages %}
        <div class="container mt-3">
            {% for category, message in messages %}
                {% set alert_class = 'primary' %}
                {% if category == 'error' or category == 'danger' %}
                    {% set alert_class = 'danger' %}
                {% elif category == 'warning' %}
                    {% set alert_class = 'warning' %}
                {% elif category == 'success' %}
                    {% set alert_class = 'success' %}
                {% elif category == 'info' %}
                    {% set alert_class = 'info' %}
                {% endif %}
                <div class="alert alert-{{ alert_class }} alert-dismissible fade show" role="alert">
                    {{ message }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            {% endfor %}
        </div>
        {% endif %}
    {% endwith %}

    <!-- Main Content -->
    <main>
        <!-- Título de la página -->
        <h1 class="mb-4">{% block page_title %}{% endblock %}</h1>
        
        <!-- Contenido -->
        {% block content %}{% endblock %}
    </main>

    <!-- Footer -->
    <footer class="bg-light py-4 mt-5">
        <div class="container">
            <div class="row">
                <div class="col-md-6">
                    <p class="mb-0">&copy; {{ now.year }} ARCH - Sistema de Gestión Documental</p>
                </div>
                <div class="col-md-6 text-end">
                    <a href="{{ url_for('main.about') }}" class="text-decoration-none">Acerca de</a>
                </div>
            </div>
        </div>
    </footer>

    <!-- Bootstrap Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    
    <!-- jQuery (For bootstrap plugins that require it) -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    
    <!-- Custom JavaScript -->
    <script src="{{ url_for('static', filename='js/main.js') }}"></script>
    
    {% block extra_js %}{% endblock %}
</body>
</html>