{% extends 'base.html' %}
{% block content %}
<div class="page-toolbar">
  <div><h1 class="h3 mb-1">Consultas</h1><p class="text-muted mb-0">Mensajes recibidos desde el formulario de contacto del frontend.</p></div>
</div>
<div class="card table-panel">
  <div class="card-header">
    <form method="get" class="row g-3 align-items-end filter-toolbar">
      <div class="col-lg-6"><label class="form-label">Buscar</label><input class="form-control" name="q" value="{{ request.args.get('q','') }}" placeholder="Nombre, email, teléfono o detalle"></div>
      <div class="col-lg-3"><label class="form-label">Estado</label><select class="form-select searchable-select" name="status" data-placeholder="Todos"><option value="">Todos</option><option value="No Leido" {% if request.args.get('status')=='No Leido' %}selected{% endif %}>No Leido</option><option value="Leido" {% if request.args.get('status')=='Leido' %}selected{% endif %}>Leido</option></select></div>
      <div class="col-lg-3 d-flex gap-2"><button class="btn btn-outline-primary flex-fill">Filtrar</button><a class="btn btn-outline-secondary" href="{{ url_for('admin_consultas') }}">Limpiar</a></div>
    </form>
  </div>
  <div class="table-responsive wide-table">
    <table class="table align-middle mb-0">
      <thead><tr><th>Fecha/Hora</th><th>Nombre</th><th>Email</th><th>Teléfono</th><th>Detalle</th><th>Estado</th><th class="text-end">Acciones</th></tr></thead>
      <tbody>
        {% for item in inquiries %}
        <tr>
          <td>{{ item.created_at.strftime('%d/%m/%Y %H:%M') }}</td>
          <td>{{ item.name }}</td>
          <td>{{ item.email }}</td>
          <td>{{ item.phone or '—' }}</td>
          <td class="text-wrap">{{ item.detail }}</td>
          <td><span class="badge {% if item.status == 'No Leido' %}text-bg-warning{% else %}text-bg-success{% endif %}">{{ item.status }}</span></td>
          <td class="text-end">
            <form method="post" class="d-inline-flex gap-2">
              <input type="hidden" name="inquiry_id" value="{{ item.id }}">
              {% if item.status == 'No Leido' %}
                <button class="btn btn-sm btn-outline-success" name="status" value="Leido">Marcar leído</button>
              {% else %}
                <button class="btn btn-sm btn-outline-secondary" name="status" value="No Leido">Marcar no leído</button>
              {% endif %}
            </form>
          </td>
        </tr>
        {% else %}
        <tr><td colspan="7" class="text-center text-muted py-4">No hay consultas registradas.</td></tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
  {% if pagination.pages > 1 %}<div class="card-body border-top"><nav><ul class="pagination mb-0 justify-content-end"><li class="page-item {% if not pagination.has_prev %}disabled{% endif %}"><a class="page-link" href="{{ url_for('admin_consultas', page=pagination.prev_num, q=request.args.get('q',''), status=request.args.get('status','')) }}">Anterior</a></li>{% for page_num in pagination.iter_pages() %}{% if page_num %}<li class="page-item {% if page_num == pagination.page %}active{% endif %}"><a class="page-link" href="{{ url_for('admin_consultas', page=page_num, q=request.args.get('q',''), status=request.args.get('status','')) }}">{{ page_num }}</a></li>{% else %}<li class="page-item disabled"><span class="page-link">…</span></li>{% endif %}{% endfor %}<li class="page-item {% if not pagination.has_next %}disabled{% endif %}"><a class="page-link" href="{{ url_for('admin_consultas', page=pagination.next_num, q=request.args.get('q',''), status=request.args.get('status','')) }}">Siguiente</a></li></ul></nav></div>{% endif %}
</div>
{% endblock %}
