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

<section class="page-intro">
<div>
<span class="page-kicker">Facturation</span>
<h2>Grille tarifaire TTC</h2>
<p>Choisissez le code du cours, vérifiez le libellé et saisissez le montant TTC.</p>
</div>
<div class="pricing-actions">
<?php if(Permission::can('reductions.view')): ?>
<a class="button button-secondary" href="/public/tarifs/reductions">Réductions</a>
<?php endif; ?>
<?php if(Permission::can('tarifs.pdf.generate') && $grid): ?>
<a class="button button-secondary" href="/public/tarifs/pdf" target="_blank">Aperçu PDF</a>
<?php endif; ?>
<?php if(Permission::can('tarifs.diffusion.preview') && $grid): ?>
<a class="button button-primary" href="/public/tarifs/diffusion">Diffuser</a>
<?php endif; ?>
</div>
</section>

<?php if(!empty($_SESSION['success'])): ?>
<div class="alert alert-success"><?= htmlspecialchars((string)$_SESSION['success']) ?></div>
<?php unset($_SESSION['success']); endif; ?>

<?php if(!$grid): ?>
<section class="form-card">
<header class="form-card-header">
<div class="form-step">1</div>
<div><h3>Créer la grille tarifaire</h3><p>Une grille par année scolaire.</p></div>
<div class="form-icon">€</div>
</header>

<form method="post" action="/public/tarifs/grille" class="pricing-grid-form">
<?= Csrf::field() ?>
<label><span>Code</span><input required name="code" value="TARIFS-<?= htmlspecialchars((string)($year['libelle']??'')) ?>"></label>
<label><span>Libellé</span><input required name="libelle" value="Tarifs <?= htmlspecialchars((string)($year['libelle']??'')) ?>"></label>
<label><span>Version</span><input type="number" min="1" name="version_numero" value="1"></label>
<label><span>Début</span><input required type="date" name="date_debut_validite"></label>
<label><span>Fin</span><input type="date" name="date_fin_validite"></label>
<button class="button button-primary">Créer la grille</button>
</form>
</section>
<?php else: ?>

<section class="pricing-header-card">
<div>
<span>Grille active</span>
<h3><?= htmlspecialchars((string)$grid['libelle']) ?></h3>
<p>Version <?= (int)$grid['version_numero'] ?> · <?= htmlspecialchars((string)$grid['annee_libelle']) ?></p>
</div>
<strong><?= htmlspecialchars((string)$grid['statut']) ?></strong>
</section>

<section class="pricing-layout">
<article class="form-card">
<header class="form-card-header">
<div class="form-step pink">2</div>
<div><h3>Ajouter un tarif</h3><p>Montants TTC, sans calcul de TVA.</p></div>
<div class="form-icon pink">€</div>
</header>

<form method="post" action="/public/tarifs/ligne" class="price-line-form">
<?= Csrf::field() ?>
<input type="hidden" name="grille_id" value="<?= (int)$grid['id'] ?>">

<label>
<span>Code et cours</span>
<select required name="atelier_id">
<option value="">Choisir un cours</option>
<?php foreach($courses as $course): ?>
<option value="<?= (int)$course['id'] ?>">
<?= htmlspecialchars((string)$course['code']) ?>
— <?= htmlspecialchars((string)$course['libelle']) ?>
— <?= htmlspecialchars((string)($course['publics']??'')) ?>
</option>
<?php endforeach; ?>
</select>
</label>

<label>
<span>Montant TTC</span>
<div class="money-field"><input required type="number" min="0" step="0.01" name="montant"><b>€ TTC</b></div>
</label>

<label>
<span>Périodicité</span>
<select name="periodicite">
<option value="annuel">Annuel</option>
<option value="trimestriel">Trimestriel</option>
<option value="mensuel">Mensuel</option>
<option value="seance">À la séance</option>
<option value="forfait">Forfait</option>
</select>
</label>

<button class="button button-primary">Ajouter le tarif</button>
</form>
</article>

<article class="card">
<header class="card-header">
<div><span>Tarifs enregistrés</span><h3><?= count($lines) ?> ligne(s)</h3></div>
</header>

<div class="price-table">
<div class="price-row price-head">
<span>Code</span><span>Cours</span><span>Périodicité</span><span>Montant TTC</span>
</div>
<?php foreach($lines as $line): ?>
<div class="price-row">
<strong><?= htmlspecialchars((string)($line['atelier_code']??'—')) ?></strong>
<span><?= htmlspecialchars((string)($line['atelier_libelle']??$line['libelle'])) ?></span>
<span><?= htmlspecialchars((string)$line['periodicite']) ?></span>
<b><?= number_format((float)$line['montant'],2,',',' ') ?> €</b>
</div>
<?php endforeach; ?>
</div>
</article>
</section>
<?php endif; ?>
