{% extends "base.html" %}

{% block title %}{{ title }} — GMAO{% endblock %}

{% block content %}
<div class="py-2 py-md-4" style="max-width:900px;">

  <h2 class="mb-3 h5"><i class="bi bi-pencil-square"></i> {{ title }}</h2>

  <form method="POST" action="{% if intervention is defined and intervention %}{{ url_for('interventions.edit', id=intervention.id) }}{% else %}{{ url_for('interventions.create') }}{% endif %}">
    {{ form.hidden_tag() }}

    <!-- Section principale -->
    <div class="card border-0 shadow-sm mb-3">
      <div class="card-header bg-primary text-white py-2" style="font-size:0.9rem;"><i class="bi bi-file-text"></i> Informations générales</div>
      <div class="card-body py-2">
        <div class="row g-2 g-md-3">
          <div class="col-md-8">
            {{ form.title.label(class="form-label fw-bold mb-1") }}
            {{ form.title(class="form-control", placeholder="Ex: Fenêtre fendue — vitre à remplacer") }}
            {% for error in form.title.errors %}<div class="text-danger small">{{ error }}</div>{% endfor %}
          </div>
          <div class="col-md-4">
            {{ form.priority.label(class="form-label fw-bold mb-1") }}
            {{ form.priority(class="form-select") }}
          </div>
          <div class="col-md-6">
            {{ form.type.label(class="form-label fw-bold mb-1") }}
            {{ form.type(class="form-select") }}
          </div>
          <div class="col-md-6">
            {{ form.preventive_type.label(class="form-label fw-bold mb-1") }}
            {{ form.preventive_type(class="form-select") }}
          </div>
          <div class="col-12">
            {{ form.description.label(class="form-label mb-1") }}
            {{ form.description(class="form-control", rows=4, placeholder="Description détaillée du problème...") }}
          </div>
        </div>
      </div>
    </div>

    <!-- Équipement & Assignation -->
    <div class="card border-0 shadow-sm mb-3">
      <div class="card-header bg-warning py-2" style="font-size:0.9rem;"><i class="bi bi-hdd"></i> Équipement & Assignation</div>
      <div class="card-body py-2">
        <div class="row g-2 g-md-3">
          <div class="col-md-6">
            {{ form.equipment_id.label(class="form-label fw-bold mb-1") }}
            {{ form.equipment_id(class="form-select") }}
          </div>
          <div class="col-md-6">
            {{ form.technician_id.label(class="form-label mb-1") }}
            {{ form.technician_id(class="form-select") }}
          </div>
          <div class="col-md-6">
            {{ form.assigned_to_id.label(class="form-label mb-1") }}
            {{ form.assigned_to_id(class="form-select") }}
          </div>
          <div class="col-md-6">
            {{ form.company_id.label(class="form-label mb-1") }}
            {{ form.company_id(class="form-select") }}
          </div>
        </div>
      </div>
    </div>

    <!-- GIMA & Budget -->
    <div class="card border-0 shadow-sm mb-3">
      <div class="card-header bg-info text-white py-2" style="font-size:0.9rem;"><i class="bi bi-building"></i> GIMA & Budget prévisionnel</div>
      <div class="card-body py-2">
        <div class="row g-2 g-md-3">
          <div class="col-md-4">
            {{ form.gima_number.label(class="form-label mb-1") }}
            {{ form.gima_number(class="form-control", placeholder="N° demande GIMA") }}
          </div>
          <div class="col-md-4">
            {{ form.gima_service.label(class="form-label mb-1") }}
            {{ form.gima_service(class="form-select") }}
          </div>
          <div class="col-md-4">
            {{ form.budget_amount.label(class="form-label mb-1") }}
            {{ form.budget_amount(class="form-control", placeholder="0.00") }}
          </div>
          <div class="col-md-6">
            <div class="form-check mt-2">
              {{ form.needs_budget(class="form-check-input") }}
              {{ form.needs_budget.label(class="form-check-label") }}
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- Détails -->
    <div class="card border-0 shadow-sm mb-3">
      <div class="card-header bg-secondary text-white py-2" style="font-size:0.9rem;"><i class="bi bi-gear"></i> Détails</div>
      <div class="card-body py-2">
        <div class="row g-2 g-md-3">
          <div class="col-md-4">
            {{ form.requested_at.label(class="form-label mb-1") }}
            {{ form.requested_at(class="form-control", type="date") }}
          </div>
          <div class="col-md-4">
            {{ form.cost.label(class="form-label mb-1") }}
            {{ form.cost(class="form-control", placeholder="0.00") }}
          </div>
          <div class="col-md-4">
            {{ form.time_spent.label(class="form-label mb-1") }}
            {{ form.time_spent(class="form-control", placeholder="Minutes") }}
          </div>
          <div class="col-md-4">
            {{ form.recurrence_month.label(class="form-label mb-1") }}
            {{ form.recurrence_month(class="form-control", placeholder="Mois (ex: 6)") }}
          </div>
          <div class="col-md-4">
            {{ form.requester_name.label(class="form-label mb-1") }}
            {{ form.requester_name(class="form-control") }}
          </div>
          <div class="col-md-4">
            {{ form.requester_contact.label(class="form-label mb-1") }}
            {{ form.requester_contact(class="form-control") }}
          </div>
        </div>
      </div>
    </div>

    <div class="d-flex gap-2 mb-3">
      <button type="submit" class="btn btn-primary">
        <i class="bi bi-check-lg"></i> {{ 'Enregistrer' if intervention else 'Créer' }}
      </button>
      <a href="{{ url_for('interventions.index') }}" class="btn btn-outline-secondary">Annuler</a>
    </div>
  </form>
</div>
{% endblock %}