{% extends "base.html" %}
{% block title %}Interprétations - GMAO{% endblock %}
{% block content %}
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h4><i class="bi bi-robot"></i> Interprétations des emails</h4>
        <a href="{{ url_for('dashboard.index') }}" class="btn btn-outline-secondary">
            <i class="bi bi-arrow-left"></i> Retour
        </a>
    </div>
    
    <!-- Filtres -->
    <div class="card mb-3">
        <div class="card-body">
            <form method="GET" class="row g-3">
                <div class="col-md-4">
                    <label class="form-label">Statut</label>
                    <select name="status" class="form-select" onchange="this.form.submit()">
                        <option value="pending" {% if status_filter == 'pending' %}selected{% endif %}>En attente</option>
                        <option value="accepted" {% if status_filter == 'accepted' %}selected{% endif %}>Acceptées</option>
                        <option value="rejected" {% if status_filter == 'rejected' %}selected{% endif %}>Refusées</option>
                        <option value="all" {% if status_filter == 'all' %}selected{% endif %}>Toutes</option>
                    </select>
                </div>
                <div class="col-md-4">
                    <label class="form-label">Type</label>
                    <select name="type" class="form-select" onchange="this.form.submit()">
                        <option value="">Tous les types</option>
                        <option value="curative" {% if type_filter == 'curative' %}selected{% endif %}>Curative</option>
                        <option value="preventive" {% if type_filter == 'preventive' %}selected{% endif %}>Préventive</option>
                        <option value="ameliorative" {% if type_filter == 'ameliorative' %}selected{% endif %}>Améliorative</option>
                        <option value="administrative" {% if type_filter == 'administrative' %}selected{% endif %}>Administrative</option>
                        <option value="formation" {% if type_filter == 'formation' %}selected{% endif %}>Formation</option>
                        <option value="information" {% if type_filter == 'information' %}selected{% endif %}>Information</option>
                    </select>
                </div>
                <div class="col-md-4 d-flex align-items-end">
                    <a href="{{ url_for('outlook_dashboard.all_interpretations') }}" class="btn btn-outline-secondary">
                        <i class="bi bi-x-circle"></i> Réinitialiser
                    </a>
                </div>
            </form>
        </div>
    </div>
    
    <!-- Liste des interprétations -->
    {% if interpretations %}
    <div class="table-responsive">
        <table class="table table-hover">
            <thead>
                <tr>
                    <th>Date</th>
                    <th>Type</th>
                    <th>Titre</th>
                    <th>Description</th>
                    <th>Statut</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                {% for interp in interpretations %}
                <tr class="{% if interp.status == 'rejected' %}table-secondary{% elif interp.status == 'accepted' %}table-success{% endif %}">
                    <td>{{ interp.created_at|datetime_fmt }}</td>
                    <td>
                        {% if interp.analysis_type == 'curative' %}
                        <span class="badge bg-danger">Curative</span>
                        {% elif interp.analysis_type == 'preventive' %}
                        <span class="badge bg-primary">Préventive</span>
                        {% elif interp.analysis_type == 'ameliorative' %}
                        <span class="badge bg-success">Améliorative</span>
                        {% elif interp.analysis_type == 'administrative' %}
                        <span class="badge bg-warning text-dark">Admin</span>
                        {% elif interp.analysis_type == 'formation' %}
                        <span class="badge bg-info text-dark">Formation</span>
                        {% elif interp.analysis_type == 'information' %}
                        <span class="badge bg-secondary">Information</span>
                        {% else %}
                        <span class="badge bg-secondary">{{ interp.analysis_type }}</span>
                        {% endif %}
                    </td>
                    <td>
                        {% if interp.mail %}
                        <a href="{{ url_for('outlook_pages.mail_view', account_id=interp.mail.account_id, mail_id=interp.mail.id) }}">
                            {{ interp.suggested_title[:50] if interp.suggested_title else '(sans titre)' }}
                        </a>
                        {% else %}
                        <span class="text-muted">{{ interp.suggested_title[:50] if interp.suggested_title else '(sans titre)' }}</span>
                        {% endif %}
                    </td>
                    <td>{{ interp.suggested_description[:80] if interp.suggested_description else '-' }}</td>
                    <td>
                        {% if interp.status == 'pending' %}
                        <span class="badge bg-warning text-dark">En attente</span>
                        {% elif interp.status == 'accepted' %}
                        <span class="badge bg-success">Acceptée</span>
                        {% elif interp.status == 'rejected' %}
                        <span class="badge bg-secondary">Refusée</span>
                        {% else %}
                        <span class="badge bg-secondary">{{ interp.status }}</span>
                        {% endif %}
                    </td>
                    <td>
                        {% if interp.status == 'pending' %}
                        <div class="btn-group btn-group-sm">
                            <a href="{{ url_for('trainings.import_outlook', interpretation_id=interp.id) if interp.analysis_type == 'formation' else url_for('outlook_dashboard.create_intervention_from_interpretation', interp_id=interp.id) }}"
                               class="btn btn-outline-success" title="{{ 'Importer la formation' if interp.analysis_type == 'formation' else 'Créer l’intervention' }}">
                                <i class="bi bi-check-lg"></i>
                            </a>
                            <button type="button" class="btn btn-outline-danger" 
                                    onclick="rejectInterpretation({{ interp.id }})" title="Refuser">
                                <i class="bi bi-x-lg"></i>
                            </button>
                        </div>
                        {% elif interp.status == 'rejected' %}
                        <button type="button" class="btn btn-sm btn-outline-secondary"
                                onclick="restoreInterpretation({{ interp.id }})" title="Restaurer">
                            <i class="bi bi-arrow-counterclockwise"></i>
                        </button>
                        {% endif %}
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
    {% else %}
    <div class="alert alert-info">
        <i class="bi bi-info-circle"></i> Aucune interprétation trouvée.
    </div>
    {% endif %}
</div>

<script>
function rejectInterpretation(interpId) {
    if (!confirm('Refuser cette interprétation ?')) return;
    
    fetch(`/outlook/interpretation/${interpId}/reject`, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        }
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            location.reload();
        } else {
            alert('Erreur: ' + (data.error || 'Erreur inconnue'));
        }
    })
    .catch(error => {
        alert('Erreur: ' + error);
    });
}

function restoreInterpretation(interpId) {
    if (!confirm('Restaurer cette interprétation ?')) return;
    
    fetch(`/outlook/api/interpretation/${interpId}`, {
        method: 'PUT',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({ status: 'pending' })
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            location.reload();
        } else {
            alert('Erreur: ' + (data.error || 'Erreur inconnue'));
        }
    })
    .catch(error => {
        alert('Erreur: ' + error);
    });
}
</script>
{% endblock %}
