{% extends 'layouts/admin.twig' %}

{% block head %}
    <link rel="stylesheet" href="/modules/BlockRegistry/admin/css/block_registry.css">
{% endblock %}

{% block content %}

    <ul class="breadcrumbs el">
        <li><a href="/admin/">Admin</a> /</li>
        <li><a href="{{ route('admin.blocks.index') }}">Block Registry</a> /</li>
        <li class="end">Templates</li>
    </ul>

    <h1>{{ title }}</h1>

    {% include 'admin/block_registry/_tabnav.twig' with { active_tab: 'templates' } %}

    {% for message in messages %}
        <div class="alert alert-{{ message.type ?? 'info' }}">
            {{ message.content ?? message }}
        </div>
    {% endfor %}

    <div class="block-registry-toolbar">
        <div class="toolbar-left">
            <a href="{{ route('admin.blocks.templates.create') }}" class="btn btn-primary">
                <i class="fa fa-plus"></i> Create Template
            </a>
        </div>
    </div>

    {% if block_templates|length > 0 %}
        <table class="manage-items">
            <thead>
                <tr>
                    <th width="40">#</th>
                    <th>Name</th>
                    <th>Blocks</th>
                    <th>Assigned To</th>
                    <th width="150">Actions</th>
                </tr>
            </thead>
            <tbody class="tabular">
                {% for bt in block_templates %}
                <tr>
                    <td>{{ bt.id }}</td>
                    <td>
                        <i class="fa fa-clipboard"></i>
                        <strong>{{ bt.name }}</strong>
                        {% if bt.description %}
                            <div class="entity-block-snippet">{{ bt.description }}</div>
                        {% endif %}
                    </td>
                    <td>
                        <span class="badge">{{ bt.items_count }}</span> block(s)
                    </td>
                    <td>
                        {% if bt.templateAssignments|length > 0 %}
                            {% for assignment in bt.templateAssignments %}
                                <span class="category-tag">{{ assignment.template_name }}</span>
                            {% endfor %}
                        {% else %}
                            <span class="category-tag uncategorized">None</span>
                        {% endif %}
                    </td>
                    <td class="actions">
                        <a href="{{ route('admin.blocks.templates.edit', {'id': bt.id}) }}" title="Edit">
                            <i class="fa fa-pencil"></i> Edit
                        </a>
                        <form action="{{ route('admin.blocks.templates.destroy', {'id': bt.id}) }}" method="POST" style="display:inline" onsubmit="return confirm('Delete this block template?')">
                            <input type="hidden" name="_method" value="DELETE">
                            <button type="submit" title="Delete" class="btn-link error">
                                <i class="fa fa-times-circle"></i>
                            </button>
                        </form>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    {% else %}
        <div class="empty-state">
            <i class="fa fa-clipboard fa-3x"></i>
            <h3>No block templates yet</h3>
            <p>Block templates let you define reusable collections of blocks that can be applied to pages, products, or categories in one click.</p>
            <a href="{{ route('admin.blocks.templates.create') }}" class="btn btn-primary">
                <i class="fa fa-plus"></i> Create Template
            </a>
        </div>
    {% endif %}

{% endblock %}
