{% extends 'admin/base.html' %}

{% block content %}
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1>Gestion des pages</h1>
        <div>
            <a href="{{ url_for('admin.add_page') }}" class="btn btn-primary">
                <i class="fas fa-plus-circle"></i> Ajouter une page
            </a>
        </div>
    </div>

    <div class="card shadow-sm">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-striped table-hover datatable">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Titre</th>
                            <th>Slug (URL)</th>
                            <th>Sections</th>
                            <th>Statut</th>
                            <th>Date de création</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for page in pages %}
                        <tr>
                            <td>{{ page.id }}</td>
                            <td>{{ page.title }}</td>
                            <td>{{ page.slug }}</td>
                            <td>{{ page.sections|length }}</td>
                            <td>
                                {% if page.is_active %}
                                <span class="badge bg-success">Actif</span>
                                {% else %}
                                <span class="badge bg-secondary">Inactif</span>
                                {% endif %}
                            </td>
                            <td>{{ page.created_at.strftime('%d/%m/%Y') }}</td>
                            <td>
                                <div class="btn-group btn-group-sm" role="group">
                                    <a href="{{ url_for('admin.edit_page', id=page.id) }}" class="btn btn-info" title="Modifier">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    <button type="button" class="btn btn-danger delete-btn" data-item-id="{{ page.id }}" title="Supprimer" {% if page.sections %}disabled{% endif %}>
                                        <i class="fas fa-trash-alt"></i>
                                    </button>
                                </div>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
    $(document).ready(function() {
        // Initialize DataTable
        $('.datatable').DataTable({
            "language": {
                "url": "//cdn.datatables.net/plug-ins/1.11.5/i18n/fr-FR.json"
            },
            "order": [[0, "desc"]]
        });
        
        // Delete confirmation
        $('.delete-btn').on('click', function() {
            var id = $(this).data('item-id');
            var deleteForm = $('#deleteModal').find('form');
            deleteForm.attr('action', "{{ url_for('admin.delete_page', id=0) }}".replace('0', id));
            $('#deleteModal').modal('show');
        });
    });
</script>
{% endblock %}