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

<section class="page-intro">
<div>
<span class="page-kicker">Tarification</span>
<h2>Réductions</h2>
<p>Chaque réduction doit comporter une explication.</p>
</div>
<a class="button button-secondary" href="/public/tarifs">Retour à la grille</a>
</section>

<section class="v51-pricing-layout">
<article class="v51-card">
<h3>Nouvelle réduction</h3>

<form method="post" action="/public/tarifs/reductions" class="v51-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="reductionCondition">
<option value="residence">Lieu de résidence</option>
<option value="multi_activites">Plusieurs activités</option>
<option value="plusieurs_membres">Plusieurs membres de la famille</option>
<option value="cours">Cours précis</option>
<option value="pole">Pôle précis</option>
<option value="public">Public Enfant ou Adulte</option>
<option value="exceptionnelle">Réduction exceptionnelle</option>
<option value="autre">Autre règle</option>
</select>
</label>

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

<label>
<span>Type de réduction</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>Date de début *</span><input required type="date" name="date_debut"></label>
<label><span>Date de 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="actif"> Activer immédiatement</label>

<label class="full">
<span>Explication obligatoire *</span>
<textarea required name="explication" rows="4"
placeholder="Expliquer précisément qui peut bénéficier de la réduction et dans quelles conditions."></textarea>
</label>

<button class="button button-primary">Créer la réduction</button>
</form>
</article>

<article class="v51-card">
<h3>Règles existantes</h3>

<div class="v51-discount-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'] ?: 'Aucune explication'))) ?></p>
<small>
<?= htmlspecialchars((string)$rule['condition_type']) ?>
<?php if(!empty($rule['ville_cible'])): ?>
· <?= htmlspecialchars((string)$rule['ville_cible']) ?>
<?php endif; ?>
</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 condition=document.getElementById('reductionCondition');
    const city=document.getElementById('cityField');
    const refresh=()=>{
        city.hidden=condition.value!=='residence';
    };
    condition.addEventListener('change',refresh);
    refresh();
});
</script>
