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

{% block content %}
<div class="container-fluid py-4">
    <div class="row">
        <div class="col-lg-8">
            <div class="card shadow-sm mb-4 border-0">
                <div class="card-header bg-white py-3 d-flex justify-content-between align-items-center">
                    <h5 class="mb-0 fw-bold">
                        <i class="bi bi-tools text-primary me-2"></i>
                        {{ maintenance.m_type }} - {{ maintenance.fleet_unit.name }}
                    </h5>
                    <span class="badge bg-{{ maintenance.status|yesno:'success,warning,secondary' }} rounded-pill px-3">
                        {% if maintenance.status == 'planned' %}Планируется
                        {% elif maintenance.status == 'in_progress' %}В процессе
                        {% else %}Выполнено{% endif %}
                    </span>
                </div>
                <div class="card-body">
                    <div class="row mb-4">
                        <div class="col-md-6">
                            <p class="text-muted small mb-1">Техника</p>
                            <p class="fw-bold mb-3"><a href="{{ maintenance.fleet_unit.get_absolute_url }}">{{ maintenance.fleet_unit }}</a></p>
                            
                            <p class="text-muted small mb-1">Плановая дата</p>
                            <p class="fw-bold mb-3">{{ maintenance.planned_date }}</p>
                        </div>
                        <div class="col-md-6">
                            <p class="text-muted small mb-1">Исполнитель</p>
                            <p class="fw-bold mb-3">{{ maintenance.mechanic|default:"Не назначен" }}</p>
                            
                            <p class="text-muted small mb-1">Плановый пробег/моточасы</p>
                            <p class="fw-bold mb-3">{{ maintenance.planned_runtime }}</p>
                        </div>
                    </div>

                    <hr class="my-4">

                    <h6 class="fw-bold mb-3">Чек-лист операций</h6>
                    <div class="list-group list-group-flush border rounded mb-4">
                        {% for item in maintenance.checklist %}
                        <div class="list-group-item d-flex align-items-center py-3">
                            <form action="{% url 'maintenance_process' maintenance.pk %}" method="post" class="d-flex align-items-center w-100">
                                {% csrf_token %}
                                <input type="hidden" name="task_index" value="{{ forloop.counter0 }}">
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" name="done" value="true" 
                                           id="task_{{ forloop.counter0 }}" 
                                           {% if item.done %}checked{% endif %}
                                           onchange="this.form.submit()">
                                    <label class="form-check-label {% if item.done %}text-decoration-line-through text-muted{% endif %}" for="task_{{ forloop.counter0 }}">
                                        {{ item.task }}
                                    </label>
                                </div>
                            </form>
                        </div>
                        {% empty %}
                        <div class="list-group-item text-muted text-center py-4">Чек-лист пуст</div>
                        {% endfor %}
                    </div>

                    {% if maintenance.status != 'completed' %}
                    <div class="mt-4 pt-3 border-top">
                        <h6 class="fw-bold mb-3">Завершение ТО</h6>
                        <form action="{% url 'maintenance_complete' maintenance.pk %}" method="post" enctype="multipart/form-data">
                            {% csrf_token %}
                            <div class="row g-3">
                                <div class="col-md-6">
                                    <label class="form-label small fw-bold">Фактическая дата</label>
                                    <input type="date" name="actual_date" class="form-control" value="{% now 'Y-m-d' %}" required>
                                </div>
                                <div class="col-md-6">
                                    <label class="form-label small fw-bold">Фактический пробег/моточасы</label>
                                    <input type="number" name="actual_runtime" class="form-control" required>
                                </div>
                                <div class="col-12">
                                    <label class="form-label small fw-bold">Использованные запчасти</label>
                                    <textarea name="parts_used" class="form-control" rows="2"></textarea>
                                </div>
                                <div class="col-12">
                                    <button type="submit" class="btn btn-success w-100 py-2 fw-bold">
                                        <i class="bi bi-check-circle me-2"></i>Завершить ТО и обновить статус техники
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div>
                    {% else %}
                    <div class="mt-4 pt-3 border-top">
                        <div class="alert alert-success d-flex align-items-center">
                            <i class="bi bi-info-circle-fill me-2"></i>
                            <div>ТО успешно завершено {{ maintenance.actual_date }}</div>
                        </div>
                        <div class="row">
                            <div class="col-md-6">
                                <p class="text-muted small mb-1">Фактический пробег/моточасы</p>
                                <p class="fw-bold">{{ maintenance.actual_runtime }}</p>
                            </div>
                            <div class="col-md-6">
                                <p class="text-muted small mb-1">Запчасти</p>
                                <p>{{ maintenance.parts_used|default:"-" }}</p>
                            </div>
                        </div>
                        <a href="{% url 'maintenance_pdf' maintenance.pk %}" class="btn btn-outline-primary btn-sm mt-2">
                            <i class="bi bi-file-pdf me-1"></i>Скачать Акт ТО (PDF)
                        </a>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
        
        <div class="col-lg-4">
            <div class="card shadow-sm border-0 mb-4">
                <div class="card-header bg-white fw-bold py-3">Примечания</div>
                <div class="card-body">
                    <p class="mb-0">{{ maintenance.notes|default:"Нет примечаний" }}</p>
                </div>
            </div>
            
            {% if maintenance.fleet_unit.photo %}
            <div class="card shadow-sm border-0">
                <img src="{{ maintenance.fleet_unit.photo.url }}" class="card-img-top" alt="{{ maintenance.fleet_unit.name }}">
                <div class="card-body">
                    <p class="card-text small text-muted text-center">{{ maintenance.fleet_unit.name }}</p>
                </div>
            </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}