<?php use App\Core\Csrf; ?>
<link rel="stylesheet" href="/public/assets/css/cda-v5-2.css">

<section class="page-intro">
<div>
<span class="page-kicker">Paramètres Studio</span>
<h2>Paramétrage des réductions</h2>
<p>Ces règles seront appliquées uniquement dans le module Facturation.</p>
</div>
<a class="button button-secondary" href="/public/parametres">Retour</a>
</section>

<section class="two-column-layout">
<article class="studio-card">
<h3>Nouvelle règle</h3>

<form method="post"
action="/public/parametres/reductions"
class="reduction-form">
<?= Csrf::field() ?>

<label><span>Code *</span>
<input required name="code" placeholder="BRAY-DUNOIS"></label>

<label><span>Libellé *</span>
<input required name="libelle"
placeholder="Réduction Bray-Dunois"></label>

<label><span>Condition</span>
<select name="condition_type" id="conditionType">
<option value="residence">Ville de résidence</option>
<option value="multi_activites">Plusieurs activités</option>
<option value="plusieurs_membres">Plusieurs membres</option>
<option value="cours">Cours précis</option>
<option value="pole">Pôle précis</option>
<option value="public">Public</option>
<option value="exceptionnelle">Exceptionnelle</option>
<option value="autre">Autre</option>
</select></label>

<label id="cityField"><span>Ville</span>
<input name="ville_cible" value="BRAY-DUNES"></label>

<label><span>Type</span>
<select name="type_reduction">
<option value="pourcentage">Pourcentage</option>
<option value="montant_fixe">Montant fixe</option>
</select></label>

<label><span>Valeur</span>
<input required type="number" min="0" step="0.01"
name="valeur"></label>

<label><span>Seuil éventuel</span>
<input type="number" min="0" name="seuil"></label>

<label><span>Priorité</span>
<input type="number" name="priorite" value="100"></label>

<label><span>Début</span>
<input type="date" name="date_debut"></label>

<label><span>Fin</span>
<input type="date" name="date_fin"></label>

<label class="check">
<input type="checkbox" name="cumulable" checked>
Cumulable
</label>

<label class="check">
<input type="checkbox" name="justificatif_requis">
Justificatif requis
</label>

<label class="check">
<input type="checkbox" name="actif">
Activer immédiatement
</label>

<label class="full">
<span>Explication obligatoire *</span>
<textarea required rows="4" name="explication"
placeholder="Décrire précisément les conditions d’application."></textarea>
</label>

<button class="button button-primary">
Enregistrer la règle
</button>
</form>
</article>

<article class="studio-card">
<h3>Règles paramétrées</h3>

<div class="rule-list">
<?php foreach($rules as $rule): ?>
<article>
<div>
<span><?= htmlspecialchars((string)$rule['code']) ?></span>
<strong><?= htmlspecialchars((string)$rule['libelle']) ?></strong>
<p><?= nl2br(htmlspecialchars((string)$rule['explication'])) ?></p>
<small>
<?= htmlspecialchars((string)$rule['condition_type']) ?>
<?= $rule['ville_cible']
    ? ' · '.htmlspecialchars((string)$rule['ville_cible'])
    : '' ?>
<?= $rule['actif'] ? ' · Active' : ' · Inactive' ?>
</small>
</div>
<b>
<?= $rule['type_reduction']==='pourcentage'
    ? number_format((float)$rule['valeur'],2,',',' ').' %'
    : number_format((float)$rule['valeur'],2,',',' ').' €' ?>
</b>
</article>
<?php endforeach; ?>
</div>
</article>
</section>

<script>
document.addEventListener('DOMContentLoaded',()=>{
 const type=document.getElementById('conditionType');
 const city=document.getElementById('cityField');
 const refresh=()=>city.hidden=type.value!=='residence';
 type.addEventListener('change',refresh);
 refresh();
});
</script>
