{% extends "base.html" %}
{% block title %}Заявки{% endblock %}
{% block content %}
<h1>Заявки</h1>
<div class="card">
  <div class="filter-bar">
    <form method="get" style="display:flex;gap:8px;flex-wrap:wrap">
      <select name="org_id" onchange="this.form.submit()">
        <option value="">Все организации</option>
        {% for org in orgs %}
          <option value="{{ org.id }}" {% if filter_org_id == org.id %}selected{% endif %}>{{ org.name }}</option>
        {% endfor %}
      </select>
      <select name="status" onchange="this.form.submit()">
        <option value="">Все статусы</option>
        <option value="open" {% if filter_status == 'open' %}selected{% endif %}>Открыта</option>
        <option value="in_progress" {% if filter_status == 'in_progress' %}selected{% endif %}>В работе</option>
        <option value="closed" {% if filter_status == 'closed' %}selected{% endif %}>Закрыта</option>
      </select>
    </form>
    <span style="color:#666;font-size:.9rem">Всего: {{ tickets|length }}</span>
  </div>
  <table>
    <thead>
      <tr><th>#</th><th>Организация</th><th>Автор</th><th>Описание</th><th>Статус</th><th>Создана</th><th></th></tr>
    </thead>
    <tbody>
    {% for t in tickets %}
      <tr>
        <td>{{ t.id }}</td>
        <td>{{ t.org_name }}</td>
        <td>{{ t.author_name or '—' }}</td>
        <td>{{ (t.description or '')[:60] }}{% if (t.description or '')|length > 60 %}…{% endif %}</td>
        <td>
          <span class="badge badge-{{ t.status }}">
            {% if t.status == 'open' %}Открыта
            {% elif t.status == 'in_progress' %}В работе
            {% else %}Закрыта{% endif %}
          </span>
        </td>
        <td>{{ t.created_at[:16] }}</td>
        <td><a href="/admin/tickets/{{ t.id }}" class="btn btn-secondary">Открыть</a></td>
      </tr>
    {% else %}
      <tr><td colspan="7" style="text-align:center;color:#999">Заявок нет</td></tr>
    {% endfor %}
    </tbody>
  </table>
</div>
{% endblock %}
