{% extends "base.html" %}
{% block title %}Ma journée{% endblock %}
{% block content %}
{% if informational_alerts %}<div class="alert alert-warning" role="status"><strong>Informations à examiner</strong><ul class="mb-0">{% for alert in informational_alerts %}<li>{% if alert.meter %}{{ alert.meter.name }}{% else %}{{ alert.title }}{% endif %} — {{ alert.message }}{% if alert.intervention_id %} — Intervention créée : #{{ alert.intervention_id }}{% endif %}</li>{% endfor %}</ul><small>Ces alertes n'occupent aucun créneau ; une intervention liée constitue le travail planifiable.</small></div>{% endif %}
<div class="container-fluid py-3">
  <div class="d-flex flex-wrap justify-content-between align-items-center gap-2 mb-3">
    <div>
      <h1 class="h3 mb-1"><i class="bi bi-calendar2-week"></i> Ma journée</h1>
      <p class="text-muted mb-0">Une proposition de tâches pour le {{ target_date.strftime('%d/%m/%Y') }}. Vous gardez la main sur les changements.</p>
    </div>
    <div class="btn-group" role="group" aria-label="Navigation entre les jours">
      <a class="btn btn-outline-secondary" href="{{ url_for('planning.my_day', date=previous_date.isoformat()) }}">← Jour précédent</a>
      <a class="btn btn-outline-primary" href="{{ url_for('planning.my_day') }}">Aujourd'hui</a>
      <a class="btn btn-outline-secondary" href="{{ url_for('planning.my_day', date=next_date.isoformat()) }}">Jour suivant →</a>
    </div>
  </div>

  {% if not hours_configured %}
  <div class="alert alert-warning" role="alert">
    <strong>Horaires de travail non configurés pour cette journée.</strong>
    La proposition automatique reste désactivée. Configurez les horaires du
    technicien ou utilisez la planification manuelle existante.
    <a class="alert-link d-block mt-1" href="{{ url_for('planning.time_tracking') }}">Configurer les horaires de travail</a>
  </div>
  {% endif %}

  {% if candidates %}
  <div class="d-flex flex-wrap gap-2 mb-3" aria-label="Résumé de la journée">
    <span class="badge text-bg-primary">{{ candidates|length }} tâche(s)</span>
    <span class="badge text-bg-warning">{{ candidates|selectattr('status', 'equalto', 'à replanifier')|list|length }} à replanifier</span>
    <span class="badge text-bg-danger">{{ candidates|selectattr('task_type', 'equalto', 'emergency')|list|length }} urgence(s)</span>
    <span class="badge text-bg-danger">{{ candidates|selectattr('status', 'equalto', 'conflit')|list|length }} conflit(s)</span>
  </div>
  <div class="timeline d-grid gap-3" aria-label="Tâches proposées">
    {% for item in candidates %}
    <article class="card shadow-sm border-start border-{{ 'danger' if item.task_type == 'emergency' else 'primary' }} border-4">
      <div class="card-body">
        <div class="d-flex flex-wrap justify-content-between gap-2">
          <div>
            <div class="text-muted small">
              {% if item.proposed_start and item.proposed_end %}
                {{ item.proposed_start.strftime('%H:%M') }} – {{ item.proposed_end.strftime('%H:%M') }}
              {% else %}Horaire à définir{% endif %}
              · {{ item.duration_minutes }} min
            </div>
            <h2 class="h5 mb-1 mt-1">{{ item.title }}</h2>
            <div class="small text-muted">{{ item.location_label }}</div>
            {% if item.source_type == 'meter_reading_occurrence' and item.due_date %}
            <div class="small {% if item.due_date < target_date %}text-danger{% else %}text-muted{% endif %}">
              Échéance cible : {{ item.due_date.strftime('%d/%m/%Y') }}{% if item.due_date < target_date %} · en retard{% endif %}
            </div>
            {% endif %}
          </div>
          <div class="text-end">
            <span class="badge text-bg-secondary">{{ item.type_label }}</span>
            <span class="badge text-bg-light border">{{ item.constraint_label }}</span>
            <div class="small mt-1">{{ item.status|capitalize }}</div>
          </div>
        </div>
        {% if item.explanation %}<p class="mb-0 mt-2 small"><i class="bi bi-info-circle"></i> {{ item.explanation }}</p>{% endif %}
        {% if item.source_type == 'meter_reading_occurrence' %}
        <div class="d-flex flex-wrap gap-2 mt-3">
          <a class="btn btn-sm btn-primary" href="{{ url_for('planning.meter_occurrence_detail', occurrence_id=item.source_id) }}">Saisir le relevé</a>
          <a class="btn btn-sm btn-outline-secondary" href="{{ url_for('planning.meter_occurrence_detail', occurrence_id=item.source_id) }}#sans-releve">Traiter sans relevé</a>
        </div>
        {% endif %}
        {% if item.source_type == 'scheduled_task' and execution_states.get(item.source_id) %}
        {% set execution = execution_states.get(item.source_id) %}
        <div class="small mt-2">Temps réalisé : {{ execution.realized }} min · restant : {{ execution.remaining }} min</div>
        <div class="d-flex flex-wrap gap-1 mt-2">
          {% if execution.status in ('planned', 'suspended', 'postponed') %}<form method="post" action="{{ url_for('planning.scheduled_start', task_id=item.source_id) }}"><input type="hidden" name="csrf_token" value="{{ csrf_token() }}"><button class="btn btn-sm btn-outline-success">Démarrer</button></form>{% endif %}
          {% if execution.status == 'in_progress' %}<form method="post" action="{{ url_for('planning.scheduled_pause', task_id=item.source_id) }}"><input type="hidden" name="csrf_token" value="{{ csrf_token() }}"><input type="hidden" name="reason" value="securite"><button class="btn btn-sm btn-outline-warning">Mettre en pause</button></form><form method="post" action="{{ url_for('planning.scheduled_complete', task_id=item.source_id) }}"><input type="hidden" name="csrf_token" value="{{ csrf_token() }}"><button class="btn btn-sm btn-outline-primary">Terminer</button></form>{% endif %}
          {% if execution.status == 'paused' %}<form method="post" action="{{ url_for('planning.scheduled_resume', task_id=item.source_id) }}"><input type="hidden" name="csrf_token" value="{{ csrf_token() }}"><button class="btn btn-sm btn-outline-success">Reprendre</button></form>{% endif %}
        </div>
        {% endif %}
        {% set item_alternatives = alternatives.get((item.source_type, item.source_id), []) %}
        {% if item_alternatives and item.status != 'conflit' %}
        <details class="mt-2">
          <summary class="small text-primary" style="cursor:pointer">Replanifier cette tâche</summary>
          <form method="post" action="{{ url_for('planning.my_day_reschedule') }}" class="row g-2 mt-1 align-items-end">
            <input type="hidden" name="source_type" value="{{ item.source_type }}">
            <input type="hidden" name="source_id" value="{{ item.source_id }}">
            <input type="hidden" name="date" value="{{ target_date.isoformat() }}">
            <div class="col-8 col-sm-5"><label class="form-label small" for="slot-{{ item.source_type }}-{{ item.source_id }}">Créneau compatible</label>
              <select class="form-select form-select-sm" id="slot-{{ item.source_type }}-{{ item.source_id }}" name="slot" required>
                {% for start, end in item_alternatives %}<option value="{{ start.strftime('%H:%M') }}|{{ end.strftime('%H:%M') }}">{{ start.strftime('%H:%M') }} – {{ end.strftime('%H:%M') }}</option>{% endfor %}
              </select>
            </div>
            <div class="col-auto"><button class="btn btn-sm btn-outline-primary" type="submit">Enregistrer</button></div>
          </form>
        </details>
        {% endif %}
      </div>
    </article>
    {% endfor %}
  </div>
  {% else %}
  <div class="card border-0 bg-light">
    <div class="card-body py-5 text-center">
      <i class="bi bi-calendar-check fs-1 text-muted"></i>
      <h2 class="h5 mt-3">Aucune tâche à afficher</h2>
      <p class="text-muted mb-0">Les interventions, maintenances préventives et tâches administratives du jour apparaîtront ici.</p>
      {% if hours_configured %}<a class="btn btn-outline-primary btn-sm mt-3" href="{{ url_for('planning.room_schedules') }}">Vérifier le planning des salles</a>{% endif %}
    </div>
  </div>
  {% endif %}
</div>
{% endblock %}
