{% extends "base.html" %}
{% block title %}Nouvelle intervention — GMAO{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1><i class="bi bi-plus-circle"></i> Nouvelle intervention</h1>
        <a href="{{ url_for('interventions.index') }}" class="btn btn-secondary">
            <i class="bi bi-arrow-left"></i> Retour
        </a>
    </div>
    
    {% if interpretation %}
    <div class="alert alert-info mb-3">
        <i class="bi bi-robot"></i> <strong>Intervention créée à partir d'une interprétation automatique</strong>
        <span class="badge bg-{% if interpretation.analysis_type == 'curative' %}danger{% elif interpretation.analysis_type == 'preventive' %}primary{% elif interpretation.analysis_type == 'administrative' %}warning text-dark{% elif interpretation.analysis_type == 'formation' %}info text-dark{% else %}secondary{% endif %} ms-2">
            {{ interpretation.analysis_type|title }}
        </span>
        <span class="badge bg-{% if interpretation.analysis_action == 'create' %}success{% elif interpretation.analysis_action == 'update' %}warning text-dark{% else %}secondary{% endif %}">
            {{ interpretation.analysis_action|title }}
        </span>
        {% if interpretation.confidence_score %}
        <span class="badge bg-light text-dark">Confiance: {{ '%.0f'|format(interpretation.confidence_score * 100) }}%</span>
        {% endif %}
    </div>
    {% endif %}

    <div class="card">
        <div class="card-body">
            <form method="POST">
                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                {% if from_interpretation %}
                <input type="hidden" name="from_interpretation" value="{{ from_interpretation }}">
                <input type="hidden" name="source" value="{{ source }}">
                {% endif %}
                
                <div class="row mb-3">
                    <div class="col-md-8">
                        <label class="form-label">Titre *</label>
                        <input type="text" name="title" class="form-control" required
                               value="{{ prefilled.title if prefilled is defined else (intervention.title if intervention is defined else '') }}">
                        {% if json_suggestions and json_suggestions.title %}
                        <small class="text-success"><i class="bi bi-lightbulb"></i> Suggestion: {{ json_suggestions.title }}</small>
                        {% endif %}
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">Priorité</label>
                        <select name="priority" class="form-select">
                            {% for key, value in priorities.items() %}
                            <option value="{{ key }}" {% if (prefilled.priority if prefilled is defined else (intervention.priority if intervention is defined else 'normale')) == key %}selected{% endif %}>
                                {{ value }}
                            </option>
                            {% endfor %}
                        </select>
                        {% if json_suggestions and json_suggestions.priority %}
                        <small class="text-success"><i class="bi bi-lightbulb"></i> Suggestion: {{ json_suggestions.priority }}</small>
                        {% endif %}
                    </div>
                </div>

                <div class="row mb-3">
                    <div class="col-md-6">
                        <label class="form-label">Demandeur</label>
                        <input type="text" name="requester_name" class="form-control"
                               value="{{ prefilled.requester_name if prefilled is defined else (intervention.requester_name if intervention is defined else current_user.username) }}"
                               placeholder="Nom du demandeur (ex: Jean Dupont, Périodique...)">
                        <small class="text-muted">Si vide, votre nom d'utilisateur sera utilisé</small>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Statut</label>
                        <select name="status" class="form-select">
                            {% for key, value in statuses.items() %}
                            <option value="{{ key }}" {% if (intervention.status if intervention is defined else 'en_attente') == key %}selected{% endif %}>
                                {{ value }}
                            </option>
                            {% endfor %}
                        </select>
                    </div>
                </div>

                <div class="row mb-3">
                    <div class="col-md-6">
                        <label class="form-label">Type</label>
                        <select name="intervention_type" class="form-select">
                            <option value="curatif" {% if (prefilled.type if prefilled is defined else (intervention.type if intervention is defined else '')) == 'curatif' %}selected{% endif %}>Curatif</option>
                            <option value="preventif" {% if (prefilled.type if prefilled is defined else (intervention.type if intervention is defined else '')) == 'preventif' %}selected{% endif %}>Préventif</option>
                            <option value="amelioratif" {% if (prefilled.type if prefilled is defined else (intervention.type if intervention is defined else '')) == 'amelioratif' %}selected{% endif %}>Amélioratif</option>
                            <option value="administratif" {% if (prefilled.type if prefilled is defined else (intervention.type if intervention is defined else '')) == 'administratif' %}selected{% endif %}>Administratif</option>
                            <option value="formation" {% if (prefilled.type if prefilled is defined else (intervention.type if intervention is defined else '')) == 'formation' %}selected{% endif %}>Formation</option>
                        </select>
                        {% if json_suggestions and json_suggestions.type %}
                        <small class="text-success"><i class="bi bi-lightbulb"></i> Suggestion: {{ json_suggestions.type }}</small>
                        {% endif %}
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Lot</label>
                        <select name="lot_id" class="form-select">
                            <option value="">-- Sélectionner --</option>
                            {% for lot in lots %}
                            <option value="{{ lot.id }}">{{ lot.name }}</option>
                            {% endfor %}
                        </select>
                    </div>
                </div>

                <div class="row mb-3">
                    <div class="col-md-6">
                        <label class="form-label">Équipement</label>
                        <select name="equipment_id" class="form-select" id="equipment_select">
                            <option value="">-- Sélectionner --</option>
                            <option value="divers" {% if (equipment_id if equipment_id is defined else (intervention.equipment_id if intervention is defined else None)) is none %}selected{% endif %}>Divers</option>
                            {% for equipment in equipments %}
                            <option value="{{ equipment.id }}" {% if (equipment_id if equipment_id is defined else (intervention.equipment_id if intervention is defined else None)) == equipment.id %}selected{% endif %}>
                                {{ equipment.name }}
                            </option>
                            {% endfor %}
                        </select>
                        {% if json_suggestions and json_suggestions.equipment %}
                        <div id="equipment_suggestion">
                            {% if equipment_match %}
                            <small class="text-success"><i class="bi bi-check-circle"></i> Équipement trouvé: {{ equipment_match.name }}</small>
                            {% else %}
                            <small class="text-info">
                                <i class="bi bi-info-circle"></i> Suggestion: "{{ json_suggestions.equipment }}" — 
                                <strong>Utilisez "Divers" si non applicable</strong>
                            </small>
                            {% endif %}
                        </div>
                        {% endif %}
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Salle / Localisation</label>
                        <select name="room_id" class="form-select" id="room_select">
                            <option value="">-- Sélectionner --</option>
                            {% for room in rooms %}
                            <option value="{{ room.id }}" {% if (room_id if room_id is defined else (intervention.room_id if intervention is defined else None)) == room.id %}selected{% endif %}>
                                {{ room.name }}
                            </option>
                            {% endfor %}
                        </select>
                        {% if json_suggestions and json_suggestions.location %}
                        <div id="room_suggestion">
                            {% if room_match %}
                            <small class="text-success"><i class="bi bi-check-circle"></i> Salle trouvée: {{ room_match.name }}</small>
                            {% else %}
                            <small class="text-info">
                                <i class="bi bi-info-circle"></i> Suggestion: "{{ json_suggestions.location }}"
                            </small>
                            {% endif %}
                        </div>
                        {% endif %}
                    </div>
                </div>

                <div class="mb-3">
                    <label class="form-label">Description</label>
                    <textarea name="description" class="form-control" rows="4">{{ prefilled.description if prefilled is defined else (intervention.description if intervention is defined else '') }}</textarea>
                    {% if json_suggestions and json_suggestions.description and json_suggestions.description != (prefilled.description if prefilled is defined else '') %}
                    <small class="text-success"><i class="bi bi-lightbulb"></i> Suggestion: {{ json_suggestions.description[:200] }}{% if json_suggestions.description|length > 200 %}...{% endif %}</small>
                    {% endif %}
                </div>

                <div class="row mb-3">
                    <div class="col-md-6">
                        <label class="form-label">Assigné à</label>
                        <select name="assigned_to_id" class="form-select">
                            <option value="">-- Non assigné --</option>
                            {% for user in users %}
                            <option value="{{ user.id }}" {% if (intervention.assigned_to_id if intervention is defined else None) == user.id %}selected{% endif %}>{{ user.username }}</option>
                            {% endfor %}
                        </select>
                        {% if json_suggestions and json_suggestions.assignee %}
                        <small class="text-success"><i class="bi bi-lightbulb"></i> Suggestion: {{ json_suggestions.assignee }}</small>
                        {% endif %}
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Date d'intervention suggérée</label>
                        <input type="date" name="intervention_date" class="form-control" 
                               value="{{ prefilled.date.strftime('%Y-%m-%d') if prefilled is defined and prefilled.date else (intervention.scheduled_date.strftime('%Y-%m-%d') if intervention is defined and intervention.scheduled_date else '') }}">
                        {% if json_suggestions and json_suggestions.date %}
                        <small class="text-success"><i class="bi bi-lightbulb"></i> Suggestion: {{ json_suggestions.date }}</small>
                        {% endif %}
                    </div>
                </div>
                
                <div class="row mb-3">
                    <div class="col-md-6">
                        <label class="form-label">Heure début prévue</label>
                        <input type="time" name="scheduled_start" class="form-control"
                               value="{{ prefilled.scheduled_start if prefilled is defined and prefilled.scheduled_start else (intervention.scheduled_start.strftime('%H:%M') if intervention is defined and intervention.scheduled_start else '') }}">
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Heure fin prévue</label>
                        <input type="time" name="scheduled_end" class="form-control"
                               value="{{ prefilled.scheduled_end if prefilled is defined and prefilled.scheduled_end else (intervention.scheduled_end.strftime('%H:%M') if intervention is defined and intervention.scheduled_end else '') }}">
                    </div>
                </div>

                <div class="mb-3">
                    <label class="form-label">Notes</label>
                    <textarea name="notes" class="form-control" rows="5" placeholder="Notes internes...">{{ prefilled.notes if prefilled.notes is defined else '' }}</textarea>
                </div>

                <div class="text-end">
                    {% if from_interpretation %}
                    <a href="{{ url_for('outlook_dashboard.reject_interpretation', interpretation_id=from_interpretation) }}" class="btn btn-outline-danger me-2" onclick="return confirm('Refuser cette interprétation? Elle ne sera pas transformée en intervention.');">
                        <i class="bi bi-x-circle"></i> Refuser
                    </a>
                    {% endif %}
                    <a href="{{ url_for('interventions.index') }}" class="btn btn-secondary me-2">Annuler</a>
                    <button type="submit" class="btn btn-primary">
                        <i class="bi bi-check-lg"></i> Créer l'intervention
                    </button>
                </div>
            </form>
        </div>
    </div>
    
    {% if interpretation and interpretation.raw_json %}
    <div class="card mt-3">
        <div class="card-header">
            <button class="btn btn-sm btn-outline-secondary" type="button" data-bs-toggle="collapse" data-bs-target="#rawJsonCollapse">
                <i class="bi bi-code"></i> Voir l'analyse JSON complète
            </button>
        </div>
        <div class="collapse" id="rawJsonCollapse">
            <div class="card-body">
                <pre class="bg-light p-2 small">{{ interpretation.raw_json }}</pre>
            </div>
        </div>
    </div>
    {% endif %}
</div>
{% endblock %}