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

{% block content %}
<div class="container-fluid py-4">
    <!-- Stats Row -->
    <div class="row g-4 mb-4">
        <div class="col-md-3">
            <div class="card border-0 shadow-sm bg-slate-900 text-white p-3 h-100">
                <div class="d-flex align-items-center">
                    <div class="rounded-circle bg-blue-500 p-3 me-3">
                        <i class="bi bi-truck fs-4"></i>
                    </div>
                    <div>
                        <h3 class="fw-bold mb-0">{{ total_units }}</h3>
                        <span class="text-white-50 small">Всего техники</span>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-0 shadow-sm p-3 h-100">
                <div class="d-flex align-items-center">
                    <div class="rounded-circle bg-success-subtle p-3 me-3 text-success">
                        <i class="bi bi-check-circle fs-4"></i>
                    </div>
                    <div>
                        <h3 class="fw-bold mb-0 text-success">{{ active_units }}</h3>
                        <span class="text-muted small">В работе</span>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-0 shadow-sm p-3 h-100">
                <div class="d-flex align-items-center">
                    <div class="rounded-circle bg-danger-subtle p-3 me-3 text-danger">
                        <i class="bi bi-exclamation-triangle fs-4"></i>
                    </div>
                    <div>
                        <h3 class="fw-bold mb-0 text-danger">{{ broken_units }}</h3>
                        <span class="text-muted small">Сломано</span>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-0 shadow-sm p-3 h-100">
                <div class="d-flex align-items-center">
                    <div class="rounded-circle bg-warning-subtle p-3 me-3 text-warning">
                        <i class="bi bi-wrench fs-4"></i>
                    </div>
                    <div>
                        <h3 class="fw-bold mb-0 text-warning">{{ repair_units }}</h3>
                        <span class="text-muted small">В ремонте</span>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="row g-4">
        <!-- Recent Activities -->
        <div class="col-lg-8">
            <div class="card border-0 shadow-sm mb-4">
                <div class="card-header bg-white py-3 d-flex justify-content-between align-items-center">
                    <h6 class="fw-bold mb-0">Ближайшие ТО</h6>
                    <a href="{% url 'maintenance_list' %}" class="btn btn-sm btn-link">Все ТО</a>
                </div>
                <div class="table-responsive">
                    <table class="table table-hover align-middle mb-0">
                        <thead class="bg-light">
                            <tr>
                                <th>Техника</th>
                                <th>Тип</th>
                                <th>Дата</th>
                                <th>Статус</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for m in recent_maintenances %}
                            <tr>
                                <td><a href="{{ m.fleet_unit.get_absolute_url }}" class="text-decoration-none fw-bold">{{ m.fleet_unit.name }}</a></td>
                                <td>{{ m.m_type }}</td>
                                <td>{{ m.planned_date }}</td>
                                <td><span class="badge bg-{{ m.status|yesno:'success,warning,secondary' }} rounded-pill small">{{ m.get_status_display }}</span></td>
                            </tr>
                            {% empty %}
                            <tr><td colspan="4" class="text-center py-4 text-muted">Нет запланированных ТО</td></tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>

            <div class="card border-0 shadow-sm">
                <div class="card-header bg-white py-3 d-flex justify-content-between align-items-center">
                    <h6 class="fw-bold mb-0">Последние поломки</h6>
                    <a href="{% url 'breakdown_list' %}" class="btn btn-sm btn-link">Весь журнал</a>
                </div>
                <div class="list-group list-group-flush">
                    {% for b in recent_breakdowns %}
                    <div class="list-group-item d-flex justify-content-between align-items-center py-3">
                        <div>
                            <div class="fw-bold text-danger">{{ b.system_node }}</div>
                            <div class="small text-muted">{{ b.fleet_unit.name }} — {{ b.date|date:"d.m.Y" }}</div>
                        </div>
                        <span class="badge bg-light text-dark border">{{ b.get_status_display }}</span>
                    </div>
                    {% empty %}
                    <div class="list-group-item text-center py-4 text-muted">Поломок не зафиксировано</div>
                    {% endfor %}
                </div>
            </div>
        </div>

        <!-- Quick Actions & Search -->
        <div class="col-lg-4">
            <div class="card border-0 shadow-sm mb-4">
                <div class="card-body">
                    <h6 class="fw-bold mb-3">Быстрый поиск</h6>
                    <form action="{% url 'fleet_list' %}" method="get">
                        <div class="input-group mb-3">
                            <input type="text" name="search" class="form-control border-end-0" placeholder="Госномер или VIN...">
                            <button class="btn btn-outline-secondary border-start-0" type="submit">
                                <i class="bi bi-search"></i>
                            </button>
                        </div>
                    </form>
                </div>
            </div>

            <div class="card border-0 shadow-sm mb-4">
                <div class="card-body">
                    <h6 class="fw-bold mb-3">Быстрые действия</h6>
                    <div class="d-grid gap-2">
                        <a href="{% url 'fleet_add' %}" class="btn btn-primary py-2 rounded-pill">
                            <i class="bi bi-plus-lg me-2"></i>Добавить технику
                        </a>
                        <a href="{% url 'maintenance_add' %}" class="btn btn-outline-primary py-2 rounded-pill">
                            <i class="bi bi-calendar-plus me-2"></i>Запланировать ТО
                        </a>
                        <a href="{% url 'breakdown_add' %}" class="btn btn-outline-danger py-2 rounded-pill">
                            <i class="bi bi-exclamation-circle me-2"></i>Заявить о поломке
                        </a>
                    </div>
                </div>
            </div>

            <div class="card border-0 shadow-sm bg-blue-50">
                <div class="card-body">
                    <h6 class="fw-bold text-blue-900 mb-2">Мобильное приложение</h6>
                    <p class="small text-blue-800 mb-3">Приложение оптимизировано для работы механиков с телефона. Используйте QR-коды для быстрого доступа к чек-листам.</p>
                    <i class="bi bi-phone-vibrate text-blue-500 display-4 d-block text-center"></i>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}