{% extends 'base.html' %}
{% load static %}

{% block content %}
<div class="container-fluid py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h3 class="fw-bold mb-0">Журнал поломок</h3>
        <a href="{% url 'breakdown_add' %}" class="btn btn-danger rounded-pill px-4">
            <i class="bi bi-exclamation-triangle me-2"></i>Сообщить о поломке
        </a>
    </div>

    <div class="card border-0 shadow-sm">
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead class="bg-light">
                    <tr>
                        <th class="border-0 px-4">Техника</th>
                        <th class="border-0">Дата</th>
                        <th class="border-0">Узел / Система</th>
                        <th class="border-0">Статус</th>
                        <th class="border-0 text-end px-4">Действия</th>
                    </tr>
                </thead>
                <tbody>
                    {% for b in breakdowns %}
                    <tr>
                        <td class="px-4">
                            <div class="fw-bold"><a href="{{ b.fleet_unit.get_absolute_url }}">{{ b.fleet_unit.name }}</a></div>
                            <div class="small text-muted">{{ b.fleet_unit.plate_number|default:b.fleet_unit.vin }}</div>
                        </td>
                        <td>{{ b.date }}</td>
                        <td>{{ b.system_node }}</td>
                        <td>
                            {% if b.status == 'reported' %}
                                <span class="badge bg-danger rounded-pill">Заявлено</span>
                            {% elif b.status == 'repaired' %}
                                <span class="badge bg-success rounded-pill">Отремонтировано</span>
                            {% elif b.status == 'need_part' %}
                                <span class="badge bg-warning text-dark rounded-pill">Нужна деталь</span>
                            {% else %}
                                <span class="badge bg-secondary rounded-pill">{{ b.status }}</span>
                            {% endif %}
                        </td>
                        <td class="text-end px-4">
                            <div class="btn-group">
                                <a href="{{ b.fleet_unit.get_absolute_url }}" class="btn btn-sm btn-outline-primary rounded-pill px-3 me-2">К технике</a>
                                {% if b.status == 'need_part' %}
                                <a href="{% url 'part_request_add' %}?breakdown={{ b.pk }}" class="btn btn-sm btn-outline-warning rounded-pill px-3 me-2">Заказать запчасть</a>
                                {% endif %}
                                {% if user.is_staff %}
                                <form action="{% url 'breakdown_delete' b.pk %}" method="POST" class="d-inline" onsubmit="return confirm('Удалить запись о поломке?')">
                                    {% csrf_token %}
                                    <button type="submit" class="btn btn-sm btn-outline-danger rounded-pill px-2" title="Удалить"><i class="bi bi-trash"></i></button>
                                </form>
                                {% endif %}
                            </div>
                        </td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="5" class="text-center py-5 text-muted">Нет зафиксированных поломок</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>
{% endblock %}