{% 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 sections</h1>
        <div>
            <a href="{{ url_for('admin.add_section') }}" class="btn btn-primary">
                <i class="fas fa-plus-circle"></i> Ajouter une section
            </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>Page</th>
                            <th>Titre</th>
                            <th>Type</th>
                            <th>Ordre</th>
                            <th>Statut</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for section in sections %}
                        <tr>
                            <td>{{ section.id }}</td>
                            <td>
                                <a href="{{ url_for('admin.edit_page', id=section.page_id) }}">
                                    {{ section.page.title }}
                                </a>
                            </td>
                            <td>{{ section.title }}</td>
                            <td>
                                {% if section.section_type == 'text' %}
                                <span class="badge bg-primary">Texte</span>
                                {% elif section.section_type == 'banner' %}
                                <span class="badge bg-success">Bannière</span>
                                {% elif section.section_type == 'gallery' %}
                                <span class="badge bg-info">Galerie</span>
                                {% elif section.section_type == 'contact' %}
                                <span class="badge bg-warning">Contact</span>
                                {% elif section.section_type == 'products' %}
                                <span class="badge bg-dark">Produits</span>
                                {% elif section.section_type == 'services' %}
                                <span class="badge bg-secondary">Services</span>
                                {% else %}
                                <span class="badge bg-light text-dark">{{ section.section_type }}</span>
                                {% endif %}
                            </td>
                            <td>{{ section.order }}</td>
                            <td>
                                {% if section.is_active %}
                                <span class="badge bg-success">Actif</span>
                                {% else %}
                                <span class="badge bg-secondary">Inactif</span>
                                {% endif %}
                            </td>
                            <td>
                                <div class="btn-group btn-group-sm" role="group">
                                    <a href="{{ url_for('admin.edit_section', id=section.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="{{ section.id }}" title="Supprimer">
                                        <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_section', id=0) }}".replace('0', id));
            $('#deleteModal').modal('show');
        });
    });
</script>
{% endblock %}