<?php use App\Core\Csrf;$errors=$_SESSION['form_errors']??[];unset($_SESSION['form_errors']); ?>
<link rel="stylesheet" href="/public/assets/css/cda-accounting-ui-965.css?v=9.6.5">
<main class="accounting-page">
<section class="cda-hero accounting-hero"><div><span class="cda-eyebrow">AXIOM Studio · Comptable</span><h2>Paiements à valider</h2><p>Comparez cette liste avec le relevé bancaire, puis cochez uniquement les factures effectivement réglées.</p></div><div class="cda-actionbar"><a class="cda-btn cda-btn-secondary" href="/public/facturation">← Facturation</a></div></section>
<?php if($errors):?><div class="accounting-message is-error"><?php foreach($errors as $error):?><p><?=htmlspecialchars($error)?></p><?php endforeach?></div><?php endif?>
<?php if(!empty($_SESSION['success'])):?><div class="accounting-message is-success"><?=htmlspecialchars($_SESSION['success'])?></div><?php unset($_SESSION['success']);endif?>
<section class="accounting-kpis"><article class="accounting-kpi"><span class="accounting-kpi-icon">⌛</span><div><small>Factures en attente</small><strong><?=count($invoices)?></strong></div></article><article class="accounting-kpi"><span class="accounting-kpi-icon">€</span><div><small>Total restant attendu</small><strong><?=number_format(array_sum(array_map(static fn(array $i):float=>(float)$i['balance_amount'],$invoices)),2,',',' ')?> €</strong></div></article></section>
<form method="post" action="/public/facturation/paiements-a-valider"><?=Csrf::field()?>
<section class="cda-panel accounting-panel"><div class="cda-panel-head"><div><span class="cda-eyebrow">Contrôle manuel</span><h3>Factures non soldées</h3></div><label class="select-all"><input type="checkbox" data-select-all> Tout sélectionner</label></div><div class="accounting-table-wrap"><table class="accounting-table"><thead><tr><th></th><th>Facture</th><th>Adhérent / compte</th><th>Émise</th><th>Échéance</th><th>Total</th><th>Déjà payé</th><th>Reste attendu</th></tr></thead><tbody><?php if(!$invoices):?><tr><td colspan="8" class="accounting-table-empty">Toutes les factures sont soldées.</td></tr><?php endif?><?php foreach($invoices as $invoice):?><tr><td><input type="checkbox" name="invoice_ids[]" value="<?=$invoice['id']?>" data-invoice-check></td><td><a href="/public/facturation/voir?id=<?=$invoice['id']?>"><strong><?=htmlspecialchars($invoice['document_number'])?></strong></a></td><td><?=htmlspecialchars($invoice['nom'].' '.$invoice['prenom'])?><small class="accounting-subline"><?=htmlspecialchars($invoice['client_account'])?></small></td><td><?=htmlspecialchars($invoice['issue_date']??'—')?></td><td><?=htmlspecialchars($invoice['due_date']??'—')?></td><td><?=number_format((float)$invoice['total_amount'],2,',',' ')?> €</td><td><?=number_format((float)$invoice['paid_amount'],2,',',' ')?> €</td><td><strong><?=number_format((float)$invoice['balance_amount'],2,',',' ')?> €</strong></td></tr><?php endforeach?></tbody></table></div><?php if($invoices):?><div class="payment-validation-bar"><div><strong><span data-selected-count>0</span> facture(s) sélectionnée(s)</strong><small>La validation soldera automatiquement le reste dû.</small></div><button class="cda-btn accounting-btn-primary" onclick="return confirm('Confirmer que les factures sélectionnées apparaissent comme payées sur le relevé bancaire ?')">Valider comme payées</button></div><?php endif?></section></form>
</main><script>document.addEventListener('DOMContentLoaded',()=>{const all=document.querySelector('[data-select-all]'),boxes=[...document.querySelectorAll('[data-invoice-check]')],count=document.querySelector('[data-selected-count]');const refresh=()=>{if(count)count.textContent=boxes.filter(x=>x.checked).length;if(all)all.checked=boxes.length>0&&boxes.every(x=>x.checked)};all?.addEventListener('change',()=>{boxes.forEach(x=>x.checked=all.checked);refresh()});boxes.forEach(x=>x.addEventListener('change',refresh));refresh()});</script>
