{% 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-assignment').classList.add('open')">+ Назначить</button>
    </div>
    <div class="table-wrap"><table><thead><tr><th>Объект</th><th>Сотрудник</th><th>Роль</th><th>Назначен</th><th class="actions"></th></tr></thead>
        <tbody>{% for a in assignments %}<tr>
            <td>{{ a.object.name if a.object else '' }}</td><td>{{ a.user.full_name if a.user else '' }}</td>
            <td><span class="badge badge-{{ a.role }}">{{ {'engineer':'Инженер','technician':'Техник'}[a.role] }}</span></td>
            <td>{{ a.assigned_at.strftime('%d.%m.%Y %H:%M') if a.assigned_at else '' }}</td>
            <td class="actions">
                <form method="post" action="/service/assignments/unassign" style="display:inline">
                    <input type="hidden" name="id" value="{{ a.id }}">
                    <button class="btn btn-danger btn-sm">Отвязать</button>
                </form>
            </td>
        </tr>{% endfor %}</tbody></table>
    </div>
</div>
<div class="modal-overlay" id="modal-assignment"><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/assignments/create">
        <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="user_id" required>{% for u in users %}<option value="{{ u.id }}">{{ u.full_name }} ({{ u.role }})</option>{% endfor %}</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 %}
