{% extends "admin/layout.twig" %}

{% block title %}Administração - Ingressos - {{ event.title }}{% endblock %}

{% block header_title %}Ingressos: {{ event.title }}{% endblock %}

{% block global_actions %}
    <a href="/admin/events" class="btn btn-secondary">&larr; Voltar para Eventos</a>
{% endblock %}

{% block extra_css %}
        .category-card { background: #fafafa; border: 1px solid #ddd; padding: 15px; margin-bottom: 20px; border-radius: 5px; }
        .category-header { display: flex; justify-content: space-between; margin-bottom: 15px; }
        .form-inline { display: flex; gap: 10px; margin-bottom: 20px; background: #eee; padding: 15px; border-radius: 5px; }
        .form-inline input { padding: 8px; border: 1px solid #ccc; border-radius: 4px; flex: 1; }
{% endblock %}

{% block content %}
<div class="content-container">
    <h3>Nova Categoria de Ingresso</h3>
    <form class="form-inline" method="POST" action="/admin/events/{{ event.id }}/categories">
        <input type="text" name="name" placeholder="Ex: Entrada Inteira, Meia Entrada" required>
        <input type="text" name="description" placeholder="Descrição (Opcional)">
        <button type="submit" class="btn btn-success">+ Adicionar Categoria</button>
    </form>

    <hr style="border: 1px solid #eee; margin: 30px 0;">

    {% if categories is empty %}
        <p>Nenhuma categoria de ingresso cadastrada.</p>
    {% else %}
        {% for category in categories %}
            <div class="category-card">
                <div class="category-header">
                    <div>
                        <h3 style="margin: 0;">{{ category.name }}</h3>
                        <small>{{ category.description }}</small>
                    </div>
                </div>

                <h4>Lotes de Preço</h4>
                {% if category.batches is empty %}
                    <p style="color: #888;">Nenhum lote cadastrado. Este ingresso não poderá ser vendido.</p>
                {% else %}
                    <table>
                        <thead>
                            <tr>
                                <th>Nome do Lote</th>
                                <th>Preço (R$)</th>
                                <th>Início das Vendas</th>
                                <th>Fim das Vendas</th>
                                <th>Capacidade</th>
                                <th class="actions">Ações</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for batch in category.batches %}
                                <tr>
                                    <td>{{ batch.name }}</td>
                                    <td>{{ batch.price|number_format(2, ',', '.') }}</td>
                                    <td>{{ batch.start_date|date('d/m/Y H:i') }}</td>
                                    <td>{{ batch.end_date|date('d/m/Y H:i') }}</td>
                                    <td>{{ batch.capacity ?: '&infin;' }}</td>
                                    <td class="actions">
                                        <a href="/admin/events/{{ event.id }}/categories/{{ category.id }}/batches/{{ batch.id }}/edit" class="btn btn-sm">Editar</a>
                                        <form method="POST" action="/admin/events/{{ event.id }}/categories/{{ category.id }}/batches/{{ batch.id }}/delete" style="display:inline;" onsubmit="return confirm('Excluir este lote?')">
                                            <button type="submit" class="btn btn-danger btn-sm">Excluir</button>
                                        </form>
                                    </td>
                                </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                {% endif %}

                <div style="margin-top: 15px; background: #fff; padding: 10px; border: 1px dashed #ccc;">
                    <strong>+ Novo Lote</strong>
                    <form style="display: flex; gap: 10px; margin-top: 10px;" method="POST" action="/admin/events/{{ event.id }}/categories/{{ category.id }}/batches">
                        <input type="text" name="name" placeholder="Ex: Lote 1" required style="width: 250px; flex: 1;">
                        <input type="number" step="0.01" name="price" placeholder="Preço" required style="width: 100px;">
                        <input type="datetime-local" name="start_date" required>
                        <input type="datetime-local" name="end_date" required>
                        <input type="number" name="capacity" placeholder="Vagas" style="width: 80px;">
                        <button type="submit" class="btn btn-success">Salvar Lote</button>
                    </form>
                </div>
            </div>
        {% endfor %}
    {% endif %}
</div>
{% endblock %}
