{% raw %}{% extends "base.html" %}

{% block title %}{% endraw %}{{ entity_name|title }}{% raw %} List{% endblock %}

{% block content %}
<div class="container mt-5">
    <div class="d-flex justify-content-between align-items-center">
        <h1>{% endraw %}{{ entity_name|title }}{% raw %} List</h1>
        <a href="{{ url_for('{% endraw %}{{ entity_name }}{% raw %}.create') }}" class="btn btn-primary">Create New</a>
    </div>
    
    <table class="table table-striped mt-4">
        <thead>
            <tr>
                <th>ID</th>
                {% endraw %}
                {%- for prop in properties %}
                {% if prop.name != 'id' %}
                <th>{{ prop.name|title }}</th>
                {% endif %}
                {%- endfor %}
                {% raw %}
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            {% for item in items %}
            <tr>
                <td>{{ item.id }}</td>
                {% endraw %}
                {%- for prop in properties %}
                {% if prop.name != 'id' %}
                {# Affichage adapté selon le type #}
                {% if prop.is_relation and prop.relation_type == 'many' %}
                <td>{{ '{{ item.' + prop.name + '|length }} élément(s)' }}</td>
                {% elif prop.is_relation %}
                <td>{{ '{{ item.' + prop.name + '.id if item.' + prop.name + ' else "N/A" }}' }}</td>
                {% elif 'list[str]' in prop.type %}
                <td>{{ '{{ item.' + prop.name + '|join(", ") if item.' + prop.name + ' else "" }}' }}</td>
                {% elif 'bool' in prop.type %}
                <td>{{ '{{ "Oui" if item.' + prop.name + ' else "Non" }}' }}</td>
                {% else %}
                <td>{{ '{{ item.' + prop.name + ' }}' }}</td>
                {% endif %}
                {% endif %}
                {%- endfor %}
                {% raw %}
                <td>
                    <div class="d-flex gap-2">
                        <a href="{{ url_for('{% endraw %}{{ entity_name }}{% raw %}.read', id=item.id) }}" class="btn btn-sm btn-info">View</a>
                        <a href="{{ url_for('{% endraw %}{{ entity_name }}{% raw %}.update', id=item.id) }}" class="btn btn-sm btn-warning">Edit</a>
                        <form method="POST" action="{{ url_for('{% endraw %}{{ entity_name }}{% raw %}.delete', id=item.id) }}" class="d-inline">
                            <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to delete this item?');">Delete</button>
                        </form>
                    </div>
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</div>
{% endblock %}{% endraw %}