{% extends "base.html" %}
{% block title %}Заявка #{{ ticket.id }}{% endblock %}
{% block content %}
<h1>Заявка #{{ ticket.id }}</h1>
<a href="/admin/tickets" class="btn btn-secondary" style="margin-bottom:16px;display:inline-block">← К списку</a>

<div style="display:grid;grid-template-columns:2fr 1fr;gap:16px">
  <div>
    <div class="card">
      <h2>Описание</h2>
      <p style="white-space:pre-wrap;line-height:1.6">{{ ticket.description }}</p>
    </div>

    {% if attachments %}
    <div class="card">
      <h2>Вложения</h2>
      <ul style="list-style:none;display:flex;flex-direction:column;gap:8px">
        {% for a in attachments %}
        <li>
          {% if 'IMAGE' in a.attachment_type %}🖼
          {% elif 'VIDEO' in a.attachment_type %}🎬
          {% elif 'AUDIO' in a.attachment_type %}🎙
          {% else %}📎{% endif %}
          {{ a.attachment_type.split('.')[-1] }}
          {% if a.attachment_url %}
            — <a href="{{ a.attachment_url }}" target="_blank">Открыть</a>
          {% endif %}
          <span style="color:#999;font-size:.8rem">{{ a.created_at[:16] }}</span>
        </li>
        {% endfor %}
      </ul>
    </div>
    {% endif %}

    {% if ticket.last_comment %}
    <div class="card">
      <h2>Последний комментарий</h2>
      <p style="white-space:pre-wrap;line-height:1.6">{{ ticket.last_comment }}</p>
    </div>
    {% endif %}
  </div>

  <div>
    <div class="card">
      <h2>Сведения</h2>
      <table style="width:100%">
        <tr><td style="color:#666;font-size:.85rem">Организация</td><td>{{ ticket.org_name }}</td></tr>
        <tr><td style="color:#666;font-size:.85rem">Автор</td><td>{{ ticket.author_name or ticket.author_username or '—' }}</td></tr>
        <tr><td style="color:#666;font-size:.85rem">Создана</td><td>{{ ticket.created_at[:16] }}</td></tr>
        <tr><td style="color:#666;font-size:.85rem">Обновлена</td><td>{{ ticket.updated_at[:16] }}</td></tr>
        <tr>
          <td style="color:#666;font-size:.85rem">Статус</td>
          <td>
            <span class="badge badge-{{ ticket.status }}">
              {% if ticket.status == 'open' %}Открыта
              {% elif ticket.status == 'in_progress' %}В работе
              {% else %}Закрыта{% endif %}
            </span>
          </td>
        </tr>
      </table>
    </div>

    <div class="card">
      <h2>Обновить статус</h2>
      <form action="/admin/tickets/{{ ticket.id }}/status" method="post">
        <div class="form-row">
          <label>Новый статус</label>
          <select name="status">
            <option value="open" {% if ticket.status == 'open' %}selected{% endif %}>Открыта</option>
            <option value="in_progress" {% if ticket.status == 'in_progress' %}selected{% endif %}>В работе</option>
            <option value="closed" {% if ticket.status == 'closed' %}selected{% endif %}>Закрыта</option>
          </select>
        </div>
        <div class="form-row">
          <label>Комментарий</label>
          <textarea name="last_comment" placeholder="Необязательно">{{ ticket.last_comment or '' }}</textarea>
        </div>
        <button class="btn btn-primary" type="submit">Сохранить</button>
      </form>
    </div>
  </div>
</div>
{% endblock %}
