{% extends "base.html" %}
{% block title %}{{ title }} — GMAO Collège{% endblock %}

{% block content %}
<h1 class="mb-4">{{ title }}</h1>

<form method="POST">
    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
    <div class="card mb-4">
        <div class="card-body">
            <div class="row">
                <div class="col-md-6 mb-3">
                    <label class="form-label">Nom *</label>
                    <input type="text" name="name" class="form-control" value="{{ task.name if task else '' }}" required placeholder="Ex: Courrier, Réunion journalière">
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">Fréquence *</label>
                    <select name="frequency" class="form-select">
                        <option value="daily" {% if task and task.frequency == 'daily' %}selected{% else %}selected{% endif %}>Quotidien</option>
                        <option value="weekly" {% if task and task.frequency == 'weekly' %}selected{% endif %}>Hebdomadaire</option>
                        <option value="monthly" {% if task and task.frequency == 'monthly' %}selected{% endif %}>Mensuel</option>
                        <option value="yearly" {% if task and task.frequency == 'yearly' %}selected{% endif %}>Annuel</option>
                    </select>
                </div>
            </div>

            <div class="mb-3">
                <label class="form-label" for="admin-task-user">Tâche administrative de</label>
                <select id="admin-task-user" name="user_id" class="form-select">
                    {% for user in managed_users %}<option value="{{ user.id }}" {% if task and task.user_id == user.id %}selected{% elif not task and user.id == current_user.id %}selected{% endif %}>{{ user.full_name or user.username }}</option>{% endfor %}
                </select>
                <div class="form-text">La tâche apparaîtra dans « Ma journée » de cette personne.</div>
            </div>
            
            <div class="row">
                <div class="col-md-6 mb-3">
                    <label class="form-label">Heure de début</label>
                    <input type="time" name="start_time" class="form-control" value="{{ task.start_time.strftime('%H:%M') if task and task.start_time else '09:00' }}">
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">Durée (minutes)</label>
                    <input type="number" name="duration_minutes" class="form-control" value="{{ task.duration_minutes if task else '30' }}" min="5">
                </div>
            </div>
            
            <div class="row" id="weeklyOptions" style="display: {% if task and task.frequency == 'weekly' %}block{% else %}none{% endif %};">
                <div class="col-md-6 mb-3">
                    <label class="form-label">Jour de la semaine</label>
                    <select name="day_of_week" class="form-select">
                        <option value="0" {% if task and task.day_of_week == 0 %}selected{% endif %}>Lundi</option>
                        <option value="1" {% if task and task.day_of_week == 1 %}selected{% endif %}>Mardi</option>
                        <option value="2" {% if task and task.day_of_week == 2 %}selected{% endif %}>Mercredi</option>
                        <option value="3" {% if task and task.day_of_week == 3 %}selected{% endif %}>Jeudi</option>
                        <option value="4" {% if task and task.day_of_week == 4 %}selected{% endif %}>Vendredi</option>
                        <option value="5" {% if task and task.day_of_week == 5 %}selected{% endif %}>Samedi</option>
                        <option value="6" {% if task and task.day_of_week == 6 %}selected{% endif %}>Dimanche</option>
                    </select>
                </div>
            </div>
            
            <div class="row" id="monthlyOptions" style="display: {% if task and task.frequency == 'monthly' %}block{% else %}none{% endif %};">
                <div class="col-md-6 mb-3">
                    <label class="form-label">Jour du mois</label>
                    <input type="number" name="day_of_month" class="form-control" value="{{ task.day_of_month if task else '' }}" min="1" max="31">
                </div>
            </div>
            
            <div class="mb-3">
                <label class="form-label">Description</label>
                <textarea name="description" class="form-control" rows="2">{{ task.description if task else '' }}</textarea>
            </div>
            
            {% if task %}
            <div class="form-check">
                <input type="checkbox" name="is_active" class="form-check-input" id="isActive" {% if task.is_active %}checked{% endif %}>
                <label class="form-check-label" for="isActive">Tâche active</label>
            </div>
            {% else %}
            <input type="hidden" name="is_active" value="on">
            {% endif %}
        </div>
    </div>
    
    <div class="d-flex justify-content-between">
        <a href="{{ url_for('planning.admin_tasks') }}" class="btn btn-secondary">
            <i class="bi bi-arrow-left"></i> Annuler
        </a>
        <button type="submit" class="btn btn-primary">
            <i class="bi bi-check"></i> Enregistrer
        </button>
    </div>
</form>

<script>
document.querySelector('select[name="frequency"]').addEventListener('change', function() {
    document.getElementById('weeklyOptions').style.display = this.value === 'weekly' ? 'block' : 'none';
    document.getElementById('monthlyOptions').style.display = this.value === 'monthly' ? 'block' : 'none';
});
</script>
{% endblock %}
