{% extends "page.html" %}
{% block page_content %}
<div class="card">
    <div class="card-header"><h2>Задачи</h2>
        {% if user.role in ['owner','engineer'] %}
        <button class="btn btn-primary btn-sm" onclick="document.getElementById('modal-task').classList.add('open')">+ Создать</button>
        {% endif %}
    </div>
    <form method="get" class="filter-bar">
        <select name="status_filter" onchange="this.form.submit()"><option value="">Все статусы</option><option value="open" {% if status_filter == 'open' %}selected{% endif %}>Открытые</option><option value="in_progress" {% if status_filter == 'in_progress' %}selected{% endif %}>В работе</option><option value="completed" {% if status_filter == 'completed' %}selected{% endif %}>Завершённые</option></select>
        <input type="text" name="search" placeholder="Поиск..." value="{{ search }}">
        <button class="btn btn-primary btn-sm">Найти</button>
    </form>
    <div class="table-wrap"><table><thead><tr><th>Приоритет</th><th>Название</th><th>Объект</th><th>Исполнитель</th><th>Статус</th><th>Срок</th><th class="actions"></th></tr></thead>
        <tbody>{% for t in tasks %}<tr>
            <td><span class="badge badge-{{ {'P1':'critical','P2':'high','P3':'medium','P4':'low'}[t.priority] }}">{{ t.priority }}</span></td>
            <td>{{ t.title }}</td><td>{{ t.object.name if t.object else '' }}</td>
            <td>{{ t.assignee.full_name if t.assignee else '' }}</td>
            <td><span class="badge badge-{{ t.status }}">{{ {'open':'Открыта','in_progress':'В работе','completed':'Завершена','cancelled':'Отменена'}[t.status] }}</span></td>
            <td><small>{{ t.deadline.strftime('%d.%m.%Y') if t.deadline else '' }}</small></td>
            <td class="actions">{% if t.status in ['open','in_progress'] and (user.role in ['owner','engineer'] or t.assigned_to == user.id) %}
                <form method="post" action="/service/tasks/close" style="display:inline" onsubmit="return confirm('Завершить задачу?')"><input type="hidden" name="id" value="{{ t.id }}"><button class="btn btn-success btn-sm">✓ Закрыть</button></form>
                {% endif %}
            </td>
        </tr>{% endfor %}</tbody></table>
    </div>
</div>
<div class="modal-overlay" id="modal-task"><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/tasks/create">
        <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-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="assigned_to"><option value="">—</option>{% for u in users %}<option value="{{ u.id }}">{{ u.full_name }}</option>{% endfor %}</select></div>
        </div>
        <div class="form-row">
            <div class="form-group"><label>Приоритет</label><select name="priority"><option value="P3">P3 — Medium</option><option value="P1">P1 — Critical</option><option value="P2">P2 — High</option><option value="P4">P4 — Low</option></select></div>
            <div class="form-group"><label>Срок</label><input type="date" name="deadline"></div>
        </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 %}
