{% extends "base.html" %}

{% block title %}Boîte de réception ENT77 - GMAO{% endblock %}

{% block content %}
<div class="container-fluid py-4">
  <div class="d-flex justify-content-between align-items-center mb-4">
    <h4><i class="bi bi-envelope me-2"></i>Boîte de réception ENT77</h4>
    <div class="d-flex gap-2">
      <form method="POST" action="{{ url_for('ent.check') }}" class="show-progress-form" data-progress-text="Synchronisation ENT…">
        <button type="submit" class="btn btn-outline-success btn-sm">
          <i class="bi bi-download me-1"></i>Synchroniser les messages
        </button>
      </form>
      <form method="POST" action="{{ url_for('ent.check_all') }}" class="show-progress-form" data-progress-text="Import de TOUS les messages ENT (lus et non lus)…">
        {{ manual_form.hidden_tag() }}
        <button type="submit" class="btn btn-primary">
          <i class="bi bi-arrow-repeat me-1"></i>Vérifier maintenant (tous)
        </button>
      </form>
    </div>
  </div>

  {% if config %}
  <div class="row mb-4">
    <div class="col-md-3">
      <div class="card bg-light text-center p-2">
        <small class="text-muted">Intervalle</small>
        <strong>{{ config.check_interval_minutes }} min</strong>
      </div>
    </div>
    <div class="col-md-3">
      <div class="card bg-light text-center p-2">
        <small class="text-muted">Horaires</small>
        <strong>{{ config.work_start_hour }}h–{{ config.work_end_hour }}h</strong>
      </div>
    </div>
    <div class="col-md-3">
      <div class="card text-center p-2 {% if config.is_enabled %}bg-success-subtle{% else %}bg-secondary-subtle{% endif %}">
        <small class="text-muted">Statut</small>
        <strong>{% if config.is_enabled %}✅ Actif{% else %}⏸️ Désactivé{% endif %}</strong>
      </div>
    </div>
    <div class="col-md-3">
      <div class="card bg-light text-center p-2">
        <small class="text-muted">Auto-création</small>
        <strong>{% if config.auto_create_intervention %}🟢 Oui{% else %}🔴 Non{% endif %}</strong>
      </div>
    </div>
  </div>
  {% endif %}

  {% if messages %}
  <div class="alert alert-info mb-3">
    <i class="bi bi-info-circle me-2"></i>
    <strong>Dernier message importé :</strong> 
    "{{ messages[0].subject[:80] }}{% if messages[0].subject|length > 80 %}...{% endif %}"
  </div>
  {% endif %}

  {% if messages %}
  <div class="table-responsive">
    <table class="table table-hover align-middle">
      <thead class="table-dark">
        <tr>
          <th style="width:40px;"></th>
          <th>Date</th>
          <th>Expéditeur</th>
          <th>Objet</th>
          <th>Type</th>
          <th>Intervention</th>
          <th style="width:130px;">Actions</th>
        </tr>
      </thead>
      <tbody>
        {% for msg in messages %}
        <tr class="{% if msg.is_unread %}fw-bold{% endif %}">
          <td>
            {% if msg.is_unread %}<span class="badge bg-primary">•</span>{% endif %}
          </td>
          <td class="small">
            {{ msg.date|datetime_fmt if msg.date else '—' }}
          </td>
          <td>
            {{ msg.sender_name[:50] }}{% if msg.sender_name|length > 50 %}…{% endif %}
            {% if msg.is_forwarded and msg.original_sender %}
            <br><small class="text-muted">↳ {{ msg.original_sender[:50] }}{% if msg.original_sender|length > 50 %}…{% endif %}</small>
            {% endif %}
          </td>
          <td>
            <a href="{{ url_for('ent.message_detail', msg_id=msg.id) }}">
              {{ msg.subject[:60] }}{% if msg.subject|length > 60 %}…{% endif %}
            </a>
            {% if msg.has_attachment %}<i class="bi bi-paperclip ms-1 text-muted"></i>{% endif %}
          </td>
          <td>
            {% if msg.is_forwarded %}
            <span class="badge bg-info">Transféré</span>
            {% else %}
            <span class="badge bg-secondary">Direct</span>
            {% endif %}
          </td>
          <td>
            {% if msg.intervention_id %}
            <a href="{{ url_for('interventions.detail', id=msg.intervention_id) }}"
               class="badge bg-success text-decoration-none">
              #{{ msg.intervention_id }}
            </a>
            {% else %}
            <span class="text-muted">—</span>
            {% endif %}
          </td>
          <td>
            <div class="d-flex gap-1">
              <a href="{{ url_for('ent.message_detail', msg_id=msg.id) }}"
                 class="btn btn-sm btn-outline-primary" title="Voir">
                <i class="bi bi-eye"></i>
              </a>
            </div>
          </td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
  {% else %}
  <div class="text-center py-5">
    <i class="bi bi-inbox display-1 text-muted"></i>
    <p class="text-muted mt-3">Aucun message ENT77 récupéré pour le moment.</p>
    <p class="small text-muted">Cliquez sur « Vérifier maintenant » ou attendez la vérification automatique.</p>
  </div>
  {% endif %}
</div>
{% endblock %}
