<?php
$money = static fn($v): string => number_format((float)$v, 2, ',', ' ') . ' DH';
$modes = ['virement'=>'Virement','cheque'=>'Chèque','especes'=>'Espèces','carte'=>'Carte bancaire','autre'=>'Autre'];
?>
<div class="d-flex justify-content-between align-items-center mb-4"><div><h1 class="darlink-title mb-1">Règlements fournisseurs</h1><p class="text-muted mb-0">Journal des paiements actifs et annulés</p></div><a href="index.php?page=factures_fournisseurs" class="btn btn-outline-primary">Factures fournisseurs</a></div>
<form method="get" class="darlink-card p-3 mb-3"><input type="hidden" name="page" value="reglements_fournisseurs"><div class="row g-2 align-items-end">
<div class="col-md-3"><label class="form-label">Fournisseur</label><select name="fournisseur_id" class="form-select"><option value="0">Tous</option><?php foreach($fournisseurs as $f): ?><option value="<?= (int)$f['id'] ?>" <?= (int)$filters['fournisseur_id']===(int)$f['id']?'selected':'' ?>><?= htmlspecialchars($f['raison_sociale']) ?></option><?php endforeach; ?></select></div>
<div class="col-md-2"><label class="form-label">Mode</label><select name="mode_reglement" class="form-select"><option value="">Tous</option><?php foreach($modes as $k=>$v): ?><option value="<?= $k ?>" <?= $filters['mode_reglement']===$k?'selected':'' ?>><?= $v ?></option><?php endforeach; ?></select></div>
<div class="col-md-2"><label class="form-label">Du</label><input type="date" name="date_debut" class="form-control" value="<?= htmlspecialchars($filters['date_debut']) ?>"></div><div class="col-md-2"><label class="form-label">Au</label><input type="date" name="date_fin" class="form-control" value="<?= htmlspecialchars($filters['date_fin']) ?>"></div>
<div class="col-md-2"><label class="form-label">Statut</label><select name="etat" class="form-select"><option value="actifs" <?= $filters['etat']==='actifs'?'selected':'' ?>>Actifs</option><option value="annules" <?= $filters['etat']==='annules'?'selected':'' ?>>Annulés</option><option value="tous" <?= $filters['etat']==='tous'?'selected':'' ?>>Tous</option></select></div><div class="col-md-1"><button class="btn btn-primary w-100">Filtrer</button></div></div></form>
<div class="row g-3 mb-3"><div class="col-md-3"><div class="darlink-card p-3"><span class="text-muted">Total actif affiché</span><div class="h4 mb-0"><?= $money($totaux['total']) ?></div></div></div><?php foreach($totaux['par_mode'] as $mode=>$total): ?><div class="col-md-2"><div class="darlink-card p-3"><span class="text-muted"><?= htmlspecialchars($modes[$mode]??ucfirst($mode)) ?></span><div class="h5 mb-0"><?= $money($total) ?></div></div></div><?php endforeach; ?></div>
<div class="darlink-card darlink-table-card">
    <div class="table-responsive darlink-table-wrapper">
        <table class="table align-middle darlink-table darlink-table-modern" id="reglements-table">
            <thead>
            <tr>
                <th class="darlink-sortable" data-column="0">Date <span class="sort-indicator">↕</span></th>
                <th class="darlink-sortable" data-column="1">Facture <span class="sort-indicator">↕</span></th>
                <th class="darlink-sortable" data-column="2">Fournisseur <span class="sort-indicator">↕</span></th>
                <th class="darlink-sortable" data-column="3">Mode <span class="sort-indicator">↕</span></th>
                <th class="darlink-sortable" data-column="4">Référence <span class="sort-indicator">↕</span></th>
                <th class="darlink-sortable" data-column="5">Statut <span class="sort-indicator">↕</span></th>
                <th class="text-end darlink-sortable" data-column="6">Montant <span class="sort-indicator">↕</span></th>
                <th class="text-end">Actions</th>
            </tr>
            </thead>
            <tbody>
            <?php if (!$reglements): ?>
                <tr><td colspan="8" class="text-center text-muted py-4">Aucun règlement.</td></tr>
            <?php endif; ?>
            <?php foreach ($reglements as $r): ?>
                <tr>
                    <td data-sort-value="<?= date('Ymd', strtotime($r['date_reglement'])) ?>"><?= date('d/m/Y', strtotime($r['date_reglement'])) ?></td>
                    <td><a href="index.php?page=factures_fournisseurs_show&id=<?= (int)$r['facture_fournisseur_id'] ?>"><?= htmlspecialchars($r['numero_interne']) ?></a></td>
                    <td><?= htmlspecialchars($r['fournisseur_nom']) ?></td>
                    <td><?= htmlspecialchars($modes[$r['mode_reglement']] ?? $r['mode_reglement'] ?: '-') ?></td>
                    <td><?= htmlspecialchars($r['reference_reglement'] ?: '-') ?></td>
                    <td data-sort-value="<?= (int)$r['actif'] ? 'Actif' : 'Annulé' ?>"><?= (int)$r['actif'] ? '<span class="badge bg-success">Actif</span>' : '<span class="badge bg-danger">Annulé</span>' ?></td>
                    <td data-sort-value="<?= (float)$r['montant'] ?>" class="text-end"><strong><?= $money($r['montant']) ?></strong></td>
                    <td class="text-end"><?php if ((int)$r['actif']): ?><form method="post" action="index.php?page=reglements_fournisseurs_annuler" class="d-inline" onsubmit="return confirm('Annuler ce règlement ?');"><input type="hidden" name="id" value="<?= (int)$r['id'] ?>"><button class="btn btn-sm btn-outline-danger">Annuler</button></form><?php endif; ?></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
