{% extends "base.html.twig" %}

{% block body %}

<div class="box pad">

<h3>Subir anuncio</h3>

<form method="POST" action="{{ path('subir_anuncio') }}" enctype="multipart/form-data">

<div class="form-group">
<label>Titulo</label>
<input type="text" name="titulo" class="form-control">
</div>

<div class="form-group">
<label>Enlace</label>
<input type="text" name="enlace" class="form-control">
</div>

<div class="form-group">
<label>Imagen</label>
<input type="file" name="imagen" required>
</div>

<div class="form-group">
    <label>Fecha de inicio</label>
    <input type="datetime-local" name="fecha_inicio" class="form-control" required>
</div>

<div class="form-group">
    <label>Fecha de fin</label>
    <input type="datetime-local" name="fecha_fin" class="form-control" required>
</div>
<br>

<button class="btn btn-success">
Subir anuncio
</button>

</form>


{% if anuncios is defined and anuncios|length > 0 %}
<div class="box pad" style="margin-top:30px;">
    <h3>Lista de anuncios</h3>
    <table class="table table-bordered table-striped">
       <thead>
    <tr>
        <th>ID</th>
        <th>Titulo</th>
        <th>Imagen</th>
        <th>Enlace</th>
        <th>Activo</th>
        <th>Fecha Inicio</th>
        <th>Fecha Fin</th>
        <th>Acciones</th>
    </tr>
</thead>
<tbody>
{% for anuncio in anuncios %}
    <tr>
        <td>{{ anuncio.id }}</td>
        <td>{{ anuncio.titulo }}</td>
        <td>
            <img src="/anuncios/{{ anuncio.imagen }}" style="max-width:100px; border-radius:5px;">
        </td>
        <td>
            {% if anuncio.enlace %}
                <a href="{{ anuncio.enlace }}" target="_blank">{{ anuncio.enlace }}</a>
            {% endif %}
        </td>
        <td>{{ anuncio.activo == 1 ? 'Activo' : 'Inactivo' }}</td>
        <td>{{ anuncio.fecha_inicio|date('Y-m-d H:i') }}</td>
        <td>{{ anuncio.fecha_fin|date('Y-m-d H:i') }}</td>
        <td>
            <a href="{{ path('editar_anuncio', {'id': anuncio.id}) }}" class="btn btn-info btn-sm">Editar</a>
            {% if anuncio.activo == 1 %}
                <a href="{{ path('cambiar_estado_anuncio', {'id': anuncio.id, 'estado': 0}) }}" class="btn btn-warning btn-sm">Desactivar</a>
            {% else %}
                <a href="{{ path('cambiar_estado_anuncio', {'id': anuncio.id, 'estado': 1}) }}" class="btn btn-success btn-sm">Activar</a>
            {% endif %}
        </td>
    </tr>
{% endfor %}
</tbody>
    </table>
</div>
{% endif %}

</div>

{% endblock %}