{% extends 'base.html' %}
{% block title %}Assignations par defaut{% endblock %}
{% block content %}
<div class="flex justify-between items-center mb-4">
    <div>
        <a href="/campaigns" class="text-xs text-gray-500 hover:text-gray-300">← Campagnes</a>
        <h2 class="text-xl font-bold text-cyber-accent">Assignations par defaut</h2>
        <p class="text-xs text-gray-500 mt-1">Regles appliquees automatiquement a la creation de chaque campagne. Priorite : serveur > application > app_group > domaine > zone.</p>
    </div>
</div>

{% if msg %}
<div class="mb-4 p-2 rounded text-sm {% if msg == 'error' %}bg-red-900/30 text-cyber-red{% else %}bg-green-900/30 text-cyber-green{% endif %}">
    {% if msg == 'added' %}Regle ajoutee.{% elif msg == 'deleted' %}Regle supprimee.{% elif msg == 'error' %}Erreur (regle dupliquee ?).{% endif %}
</div>
{% endif %}

<!-- Regles existantes -->
<div class="card overflow-x-auto">
    <table class="w-full table-cyber">
        <thead><tr>
            <th class="p-2">Priorite</th>
            <th class="p-2">Type</th>
            <th class="text-left p-2">Valeur</th>
            <th class="p-2">Intervenant</th>
            <th class="text-left p-2">Note</th>
            <th class="p-2">Actions</th>
        </tr></thead>
        <tbody>
        {% for r in rules %}
        <tr>
            <td class="p-2 text-center">{{ r.priority }}</td>
            <td class="p-2 text-center"><span class="badge {% if r.rule_type == 'server' %}badge-red{% elif r.rule_type == 'app_type' %}badge-yellow{% elif r.rule_type == 'domain' %}badge-blue{% elif r.rule_type == 'zone' %}badge-green{% else %}badge-gray{% endif %}">{{ r.rule_type }}</span></td>
            <td class="p-2 font-mono text-sm text-cyber-accent">{{ r.rule_value }}</td>
            <td class="p-2 text-center">{{ r.display_name }}</td>
            <td class="p-2 text-xs text-gray-400">{{ r.note or '' }}</td>
            <td class="p-2 text-center">
                <form method="POST" action="/assignments/{{ r.id }}/delete" style="display:inline">
                    <button class="btn-sm bg-red-900/30 text-cyber-red" onclick="return confirm('Supprimer ?')">Suppr</button>
                </form>
            </td>
        </tr>
        {% endfor %}
        {% if not rules %}
        <tr><td colspan="6" class="p-4 text-center text-gray-500">Aucune regle definie</td></tr>
        {% endif %}
        </tbody>
    </table>
</div>

<!-- Ajouter une regle -->
<div class="card p-4 mt-4">
    <h4 class="text-sm font-bold text-cyber-accent mb-3">Ajouter une regle</h4>
    <form method="POST" action="/assignments/add" class="flex gap-3 items-end flex-wrap">
        <div>
            <label class="text-xs text-gray-500">Type</label>
            <select name="rule_type" class="text-xs py-1 px-2" id="rule-type" onchange="
                var v = document.getElementById('rule-value');
                var dl = document.getElementById('values-list');
                var opts = document.getElementById('all-options').innerHTML;
                if (this.value === 'server') { v.removeAttribute('list'); v.placeholder = 'ex: vpdsiawsus1'; v.value = ''; }
                else { v.setAttribute('list', 'values-list'); v.placeholder = 'Sélectionner...'; v.value = ''; dl.innerHTML = opts; }
            ">
                <option value="domain">Domaine</option>
                <option value="app_type">Application (spécifiques)</option>
                <option value="app_group">Groupe applicatif</option>
                <option value="zone">Zone</option>
                <option value="server">Serveur spécifique</option>
            </select>
        </div>
        <div>
            <label class="text-xs text-gray-500">Valeur</label>
            <input type="text" name="rule_value" id="rule-value" class="text-xs py-1 px-2 w-44" required list="values-list" placeholder="Sélectionner...">
            <datalist id="values-list">
                {% for d in domains %}<option value="{{ d.code }}">{{ d.name }}</option>{% endfor %}
                {% for z in zones %}<option value="{{ z.name }}">{{ z.name }}</option>{% endfor %}
                {% for a in app_types %}<option value="{{ a }}">{{ a }}</option>{% endfor %}
            </datalist>
            <template id="all-options">
                {% for d in domains %}<option value="{{ d.code }}">{{ d.name }}</option>{% endfor %}
                {% for z in zones %}<option value="{{ z.name }}">{{ z.name }}</option>{% endfor %}
                {% for a in app_types %}<option value="{{ a }}">{{ a }}</option>{% endfor %}
            </template>
        </div>
        <div>
            <label class="text-xs text-gray-500">Intervenant</label>
            <select name="user_id" class="text-xs py-1 px-2">
                {% for u in intervenants %}<option value="{{ u.id }}">{{ u.display_name }}</option>{% endfor %}
            </select>
        </div>
        <div>
            <label class="text-xs text-gray-500">Priorite</label>
            <input type="number" name="priority" value="10" min="1" max="99" class="text-xs py-1 px-2 w-16">
        </div>
        <div class="flex-1">
            <label class="text-xs text-gray-500">Note</label>
            <input type="text" name="note" class="text-xs py-1 px-2 w-full" placeholder="ex: Referent EMV">
        </div>
        <button type="submit" class="btn-primary px-4 py-1 text-sm">Ajouter</button>
    </form>
</div>
{% endblock %}
