{% extends "base.html" %}
{% block title %}Déplacer l'équipement — GMAO Collège{% endblock %}

{% block content %}
<h1 class="mb-4">
    <i class="bi bi-arrow-left-right"></i> Déplacer l'équipement
</h1>

<div class="card">
    <div class="card-body">
        <div class="mb-3">
            <strong>{{ equipment.name }}</strong>
            {% if equipment.code %}<span class="text-muted">({{ equipment.code }})</span>{% endif %}
            
            {% if equipment.is_individual and equipment.parent %}
            <br><small class="text-muted">Élément n°{{ equipment.individual_number }} du groupe « <a href="{{ url_for('equipments.detail', id=equipment.parent_id) }}">{{ equipment.parent.name }}</a> »</small>
            {% endif %}
        </div>
        
        <form method="POST">
            {{ csrf_token() if csrf_token is defined }}
            
            {% if equipment.is_individual and equipment.parent %}
            {# Élément individuel : doit choisir un groupe cible #}
            <div class="alert alert-info">
                <i class="bi bi-info-circle"></i>
                Les éléments individuels doivent être déplacés vers un groupe du même type dans une autre salle.
            </div>
            
            <div class="mb-3">
                <label class="form-label">Salle actuelle</label>
                <input type="text" class="form-control" 
                       value="{{ current_room.building.name ~ ' > ' ~ current_room.name if current_room else 'Non définie' }}" 
                       disabled>
            </div>
            
            <div class="mb-3">
                <label for="group_id" class="form-label">Groupe cible</label>
                <select class="form-select" id="group_id" name="group_id" required>
                    <option value="">— Sélectionner un groupe —</option>
                    {% for group in target_groups %}
                    {% set group_room = group.effective_room %}
                    <option value="{{ group.id }}" data-room-id="{{ group_room.id if group_room else '' }}">
                        {{ group.name }} 
                        {% if group_room %}
                        ({{ group_room.building.name }} > {{ group_room.name }})
                        {% endif %}
                        — {{ group.quantity }} unités
                    </option>
                    {% endfor %}
                </select>
                {% if not target_groups %}
                <small class="text-danger">Aucun groupe du même type ({{ equipment.parent.name }}) disponible dans d'autres salles.</small>
                {% endif %}
            </div>
            
            <input type="hidden" name="room_id" id="room_id" value="">
            
            {% else %}
            {# Équipement standard ou groupe #}
            
            <div class="mb-3">
                <label for="room_id" class="form-label">Nouvelle salle</label>
                <select class="form-select" id="room_id" name="room_id">
                    <option value="">— Aucune salle —</option>
                    {% for room in rooms %}
                    <option value="{{ room.id }}" 
                            {% if current_room and room.id == current_room.id %}selected{% endif %}>
                        {{ room.building.name }} > {{ room.name }}
                    </option>
                    {% endfor %}
                </select>
            </div>
            
            <div class="mb-3">
                <label class="form-label">Salle actuelle</label>
                <input type="text" class="form-control" 
                       value="{{ current_room.building.name ~ ' > ' ~ current_room.name if current_room else 'Non définie' }}" 
                       disabled>
            </div>
            {% endif %}
            
            <div class="mt-4">
                <button type="submit" class="btn btn-primary" {% if equipment.is_individual and not target_groups %}disabled{% endif %}>
                    <i class="bi bi-check-lg"></i> Déplacer
                </button>
                <a href="{{ url_for('equipments.detail', id=equipment.id) }}" class="btn btn-outline-secondary">
                    Annuler
                </a>
            </div>
        </form>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    const groupSelect = document.getElementById('group_id');
    const roomInput = document.getElementById('room_id');
    
    if (groupSelect && roomInput) {
        groupSelect.addEventListener('change', function() {
            const selected = this.options[this.selectedIndex];
            const roomId = selected.dataset.roomId || '';
            roomInput.value = roomId;
        });
    }
});
</script>
{% endblock %}