{% extends "page.html" %}
{% block page_content %}
<div class="card">
    <div class="card-header"><h2>Отчёты</h2>
        <button class="btn btn-primary btn-sm" onclick="document.getElementById('modal-report').classList.add('open')">+ Создать</button>
    </div>
    <form method="get" class="filter-bar">
        <select name="type_filter" onchange="this.form.submit()"><option value="">Все типы</option><option value="inspection" {% if type_filter == 'inspection' %}selected{% endif %}>Осмотр</option><option value="service" {% if type_filter == 'service' %}selected{% endif %}>Обслуживание</option><option value="emergency" {% if type_filter == 'emergency' %}selected{% endif %}>Авария</option><option value="audit" {% if type_filter == 'audit' %}selected{% endif %}>Аудит</option><option value="monthly" {% if type_filter == 'monthly' %}selected{% endif %}>Ежемесячный</option></select>
    </form>
    <div class="table-wrap"><table><thead><tr><th>Тип</th><th>Название</th><th>Объект</th><th>Статус</th><th>Результат</th><th>Дата</th></tr></thead>
        <tbody>{% for r in reports %}<tr>
            <td><span class="badge badge-{{ r.report_type }}">{{ {'inspection':'Осмотр','service':'Обслуживание','emergency':'Авария','audit':'Аудит','monthly':'Ежемесячный'}[r.report_type] }}</span></td>
            <td>{{ r.title }}</td><td>{{ r.object.name if r.object else '' }}</td>
            <td><span class="badge badge-{{ r.status }}">{{ {'draft':'Черновик','final':'Готов','cancelled':'Отменён'}[r.status] }}</span></td>
            <td><span class="badge badge-{{ r.result_status }}">{{ {'ok':'OK','fixed':'Исправлено','partial':'Частично','revisit':'Повторный'}[r.result_status] }}</span></td>
            <td><small>{{ r.created_at.strftime('%d.%m.%Y') if r.created_at else '' }}</small></td>
        </tr>{% endfor %}</tbody></table>
    </div>
</div>
<div class="modal-overlay" id="modal-report"><div class="modal">
    <button type="button" class="modal-close" onclick="this.closest('.modal-overlay').classList.remove('open')">✕</button>
    <h2>Новый отчёт</h2>
    <form method="post" action="/service/reports/create">
        <div class="form-row">
            <div class="form-group"><label>Объект</label><select name="object_id" required>{% for o in objects %}<option value="{{ o.id }}">{{ o.name }}</option>{% endfor %}</select></div>
            <div class="form-group"><label>Тип</label><select name="report_type"><option value="inspection">Осмотр</option><option value="service">Обслуживание</option><option value="emergency">Авария</option><option value="audit">Аудит</option><option value="monthly">Ежемесячный</option></select></div>
        </div>
        <div class="form-group"><label>Название</label><input type="text" name="title" required></div>
        <div class="form-group"><label>Описание</label><textarea name="description"></textarea></div>
        <div class="form-group"><label>Результаты</label><textarea name="findings"></textarea></div>
        <div class="form-group"><label>Рекомендации</label><textarea name="recommendations"></textarea></div>
        <div class="form-group"><label>Статус</label><select name="status"><option value="draft">Черновик</option><option value="final">Готов</option></select></div>
        <div class="form-actions"><button type="submit" class="btn btn-primary">Создать</button>
        <button type="button" class="btn btn-secondary" onclick="this.closest('.modal-overlay').classList.remove('open')">Отмена</button></div>
    </form>
</div></div>
{% endblock %}
