<?php
$annule = (int)$encaissement['actif'] !== 1;
$dateEncaissement = trim((string)($encaissement['date_encaissement'] ?? ''));
$dateAffichee = $dateEncaissement;
if ($dateEncaissement !== '') {
    $date = \DateTimeImmutable::createFromFormat('!Y-m-d', $dateEncaissement);
    if ($date instanceof \DateTimeImmutable) {
        $dateAffichee = $date->format('d/m/Y');
    }
}
$referenceReglement = trim((string)($encaissement['reference_reglement'] ?? ''));
?>
<div class="d-flex justify-content-between mb-4 no-print"><a href="index.php?page=encaissements" class="btn btn-outline-secondary">← Journal</a><button onclick="window.print()" class="btn darlink-btn-primary"><i class="bi bi-printer me-1"></i>Imprimer le reçu</button></div>
<div class="darlink-card p-5 mx-auto receipt" style="max-width:760px">
    <div class="text-center border-bottom pb-4 mb-4"><h1 class="h3">REÇU D’ENCAISSEMENT</h1><div class="text-muted">Reçu n° ENC-<?= str_pad((string)$encaissement['id'], 6, '0', STR_PAD_LEFT) ?></div><?php if ($annule): ?><div class="badge text-bg-danger mt-2">REÇU ANNULÉ</div><?php endif; ?></div>
    <div class="row g-4"><div class="col-6"><div class="text-muted small">CLIENT</div><strong><?= htmlspecialchars($encaissement['client_nom']) ?></strong></div><div class="col-6 text-end"><div class="text-muted small">DATE D’ENCAISSEMENT</div><strong><?= htmlspecialchars($dateAffichee) ?></strong></div>
    <div class="col-6"><div class="text-muted small">FACTURE</div><strong><?= htmlspecialchars($encaissement['facture_numero']) ?></strong></div><div class="col-6 text-end"><div class="text-muted small">MODE DE RÈGLEMENT</div><strong><?= htmlspecialchars($encaissement['mode_reglement']) ?></strong></div>
    <?php if ($referenceReglement !== ''): ?><div class="col-12"><div class="text-muted small">RÉFÉRENCE</div><?= htmlspecialchars($referenceReglement) ?></div><?php endif; ?></div>
    <div class="text-center bg-light border rounded p-4 my-4"><div class="text-muted">MONTANT REÇU</div><div class="display-6 fw-bold <?= $annule?'text-decoration-line-through text-muted':'text-success' ?>"><?= number_format((float)$encaissement['montant'], 2, ',', ' ') ?> MAD</div></div>
    <?php if (!empty($encaissement['observations'])): ?><div><strong>Observations :</strong> <?= nl2br(htmlspecialchars($encaissement['observations'])) ?></div><?php endif; ?>
    <div class="text-center text-muted small mt-5">Document généré par Darlink ERP Phoenix</div>
</div>
<style>@media print{.darlink-sidebar,.darlink-topbar,.no-print{display:none!important}.receipt{box-shadow:none!important;border:0!important;max-width:none!important}.darlink-main{margin:0!important}}</style>
