<!doctype html>
<html lang="{{ current_locale() }}" class="h-100">
    {% set current_theme = current_theme() %}

    <head>
        {{ current_theme.headertags() }} {{ current_theme.local_style() }}
        <title>{{ _('Administración de Blog') }}</title>
    </head>

    <body>
        {{ current_theme.navbar() }}

        <main>
            {{ current_theme.notify() }}

            <div class="container">
                <div class="row">
                    <div class="col-md-12">
                        <div class="d-flex justify-content-between align-items-center mb-4">
                            <h1>{{ _('Administración de Blog') }}</h1>
                            <a href="{{ url_for('blog.admin_create_post') }}" class="btn btn-primary">
                                <i class="fas fa-plus"></i> {{ _('Nueva entrada') }}
                            </a>
                        </div>

                        <!-- Filter by status -->
                        <div class="mb-3">
                            <div class="btn-group" role="group">
                                <a
                                    href="{{ url_for('blog.admin_blog_index', status='all') }}"
                                    class="btn btn-outline-secondary{% if current_status == 'all' %} active{% endif %}"
                                >
                                    {{ _('Todas') }}
                                </a>
                                <a
                                    href="{{ url_for('blog.admin_blog_index', status='draft') }}"
                                    class="btn btn-outline-secondary{% if current_status == 'draft' %} active{% endif %}"
                                >
                                    {{ _('Borradores') }}
                                </a>
                                <a
                                    href="{{ url_for('blog.admin_blog_index', status='pending') }}"
                                    class="btn btn-outline-secondary{% if current_status == 'pending' %} active{% endif %}"
                                >
                                    {{ _('Pendientes') }}
                                </a>
                                <a
                                    href="{{ url_for('blog.admin_blog_index', status='published') }}"
                                    class="btn btn-outline-secondary{% if current_status == 'published' %} active{% endif %}"
                                >
                                    {{ _('Publicadas') }}
                                </a>
                                <a
                                    href="{{ url_for('blog.admin_blog_index', status='banned') }}"
                                    class="btn btn-outline-secondary{% if current_status == 'banned' %} active{% endif %}"
                                >
                                    {{ _('Baneadas') }}
                                </a>
                            </div>
                        </div>

                        {% if posts %}
                        <div class="table-responsive">
                            <table class="table table-striped">
                                <thead>
                                    <tr>
                                        <th>{{ _('Título') }}</th>
                                        <th>{{ _('Autor') }}</th>
                                        <th>{{ _('Estado') }}</th>
                                        <th>{{ _('Comentarios') }}</th>
                                        <th>{{ _('Fecha') }}</th>
                                        <th>{{ _('Acciones') }}</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for post in posts %}
                                    <tr>
                                        <td>
                                            {% if post.status == 'published' %}
                                            <a href="{{ url_for('blog.blog_post', slug=post.slug) }}" target="_blank">
                                                {{ post.title }}
                                            </a>
                                            {% else %} {{ post.title }} {% endif %}
                                        </td>
                                        <td>{{ post.author.nombre }} {{ post.author.apellido or '' }}</td>
                                        <td>
                                            {% if post.status == 'draft' %}
                                            <span class="badge bg-secondary">{{ _('Borrador') }}</span>
                                            {% elif post.status == 'pending' %}
                                            <span class="badge bg-warning">{{ _('Pendiente') }}</span>
                                            {% elif post.status == 'published' %}
                                            <span class="badge bg-success">{{ _('Publicada') }}</span>
                                            {% elif post.status == 'banned' %}
                                            <span class="badge bg-danger">{{ _('Baneada') }}</span>
                                            {% endif %}
                                        </td>
                                        <td>
                                            {% if post.allow_comments %} {{ post.comment_count }} {% else %}
                                            <span class="text-muted">{{ _('Deshabilitados') }}</span>
                                            {% endif %}
                                        </td>
                                        <td>
                                            {% if post.published_at %} {{ post.published_at.strftime('%d/%m/%Y') }} {% else %}
                                            {{ post.fecha_alta.strftime('%d/%m/%Y') }} {% endif %}
                                        </td>
                                        <td>
                                            <!-- Actions -->

                                            <a
                                                href="{{ url_for('blog.admin_edit_post', post_id=post.id) }}"
                                                class="badge text-bg-primary"
                                            >
                                                {{ _('Editar') }}
                                            </a>

                                            {% if post.status == 'pending' %}
                                            <a
                                                class="badge text-bg-success"
                                                href="{{ url_for('blog.approve_post', post_id=post.id) }}"
                                            >
                                                {{ _('Aprobar') }}
                                            </a>
                                            {% endif %} {% if post.status != 'banned' %}
                                            <a
                                                class="badge text-bg-danger"
                                                href="{{ url_for('blog.ban_post', post_id=post.id) }}"
                                            >
                                                {{ _('Banear') }}
                                            </a>
                                            {% endif %}
                                        </td>
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>

                        <!-- Pagination -->
                        {% if pagination.pages > 1 %}
                        <nav>
                            <ul class="pagination">
                                {% if pagination.has_prev %}
                                <li class="page-item">
                                    <a
                                        class="page-link"
                                        href="{{ url_for('blog.admin_blog_index', page=pagination.prev_num, status=current_status) }}"
                                    >
                                        {{ _('Anterior') }}
                                    </a>
                                </li>
                                {% endif %} {% for page_num in pagination.iter_pages() %} {% if page_num %} {% if page_num !=
                                pagination.page %}
                                <li class="page-item">
                                    <a
                                        class="page-link"
                                        href="{{ url_for('blog.admin_blog_index', page=page_num, status=current_status) }}"
                                    >
                                        {{ page_num }}
                                    </a>
                                </li>
                                {% else %}
                                <li class="page-item active">
                                    <span class="page-link">{{ page_num }}</span>
                                </li>
                                {% endif %} {% else %}
                                <li class="page-item disabled">
                                    <span class="page-link">...</span>
                                </li>
                                {% endif %} {% endfor %} {% if pagination.has_next %}
                                <li class="page-item">
                                    <a
                                        class="page-link"
                                        href="{{ url_for('blog.admin_blog_index', page=pagination.next_num, status=current_status) }}"
                                    >
                                        {{ _('Siguiente') }}
                                    </a>
                                </li>
                                {% endif %}
                            </ul>
                        </nav>
                        {% endif %} {% else %}
                        <div class="alert alert-info">
                            {{ _('No hay entradas de blog') }} {% if current_status != 'all' %}{{ _('con estado') }} "{{
                            current_status }}"{% endif %} {{ _('en este momento.') }}
                        </div>
                        {% endif %}

                        <div class="mt-4">
                            <a href="{{ url_for('blog.admin_tags') }}" class="btn btn-outline-secondary">
                                <i class="fas fa-tags"></i> {{ _('Gestionar etiquetas') }}
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </main>
    </body>
</html>
