{% extends "base.html" %}
{% block title %}{{ 'Modifier' if constraint else 'Nouvelle' }} contrainte - GMAO{% endblock %}
{% block content %}
<div class="container-fluid">
    <h2 class="h4 mb-3"><i class="bi bi-shield-exclamation"></i> {{ 'Modifier' if constraint else 'Nouvelle' }} contrainte</h2>
    <form method="POST">
        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
        <div class="card border-0 shadow-sm mb-3">
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-md-4">
                        <label class="form-label">Salle concernee</label>
                        <select name="room_id" class="form-select">
                            <option value="">Toutes les salles</option>
                            {% for r in rooms %}
                            <option value="{{ r.id }}" {{ 'selected' if constraint and constraint.room_id == r.id }}>{{ r.name }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">Type de contrainte</label>
                        <select name="constraint_type" class="form-select">
                            <option value="time" {{ 'selected' if constraint and constraint.constraint_type == 'time' }}>Horaire (creneau bloque)</option>
                            <option value="occupancy" {{ 'selected' if constraint and constraint.constraint_type == 'occupancy' }}>Salle occupee (toute la journee)</option>
                            <option value="event" {{ 'selected' if constraint and constraint.constraint_type == 'event' }}>Evenement special</option>
                        </select>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">Jour de la semaine</label>
                        <select name="day_of_week" class="form-select">
                            <option value="">Tous les jours</option>
                            {% for d in range(7) %}
                            {% set names = {0:'Dimanche',1:'Lundi',2:'Mardi',3:'Mercredi',4:'Jeudi',5:'Vendredi',6:'Samedi'} %}
                            <option value="{{ d }}" {{ 'selected' if constraint and constraint.day_of_week == d }}>{{ names[d] }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">Heure de debut</label>
                        <input type="time" name="start_time" class="form-control" value="{{ constraint.start_time.strftime('%H:%M') if constraint and constraint.start_time else '' }}">
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">Heure de fin</label>
                        <input type="time" name="end_time" class="form-control" value="{{ constraint.end_time.strftime('%H:%M') if constraint and constraint.end_time else '' }}">
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">Valide du</label>
                        <input type="date" name="effective_date_start" class="form-control" value="{{ constraint.effective_date_start.strftime('%Y-%m-%d') if constraint and constraint.effective_date_start else '' }}">
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">Valide jusqu'au</label>
                        <input type="date" name="effective_date_end" class="form-control" value="{{ constraint.effective_date_end.strftime('%Y-%m-%d') if constraint and constraint.effective_date_end else '' }}">
                    </div>
                    <div class="col-md-8">
                        <label class="form-label">Raison</label>
                        <input type="text" name="reason" class="form-control" value="{{ constraint.reason if constraint else '' }}" placeholder="ex: Cours PRONOTE, Pas d'intervention bruyante dans la cour">
                    </div>
                    <div class="col-md-2">
                        <label class="form-label">Source</label>
                        <select name="source" class="form-select">
                            <option value="manual" {{ 'selected' if constraint and constraint.source == 'manual' }}>Manuel</option>
                            <option value="pronote" {{ 'selected' if constraint and constraint.source == 'pronote' }}>PRONOTE</option>
                            <option value="vacances" {{ 'selected' if constraint and constraint.source == 'vacances' }}>Vacances</option>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <div class="form-check mt-4">
                            <input type="checkbox" name="is_active" class="form-check-input" id="active" {{ 'checked' if not constraint or constraint.is_active }}>
                            <label class="form-check-label" for="active">Actif</label>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="d-flex gap-2">
            <button type="submit" class="btn btn-primary"><i class="bi bi-check-circle"></i> {{ 'Modifier' if constraint else 'Creer' }}</button>
            <a href="{{ url_for('constraints.index') }}" class="btn btn-outline-secondary">Annuler</a>
        </div>
    </form>
</div>
{% endblock %}