<?php
$formatMoney = static fn($value): string => number_format((float)$value, 2, ',', ' ') . ' DH';
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="darlink-title mb-1">Finalisation fournisseurs</h1>
        <p class="text-muted mb-0">Synthèse finale du cycle fournisseurs : facture, règlement, échéancier et tableau de bord.</p>
    </div>
    <div class="d-flex gap-2">
        <a href="index.php?page=factures_fournisseurs_dashboard" class="btn btn-outline-primary" role="button">Tableau fournisseurs</a>
        <a href="index.php?page=factures_fournisseurs" class="btn btn-primary" role="button">Factures fournisseurs</a>
    </div>
</div>

<div class="alert alert-success d-flex justify-content-between align-items-center">
    <div>
        <strong><?= htmlspecialchars($version ?? 'Phoenix P3.3.5') ?> — Cycle fournisseurs verrouillé</strong><br>
        <span>Le bloc fournisseurs est stabilisé et prêt à servir de base pour la prochaine Beta.</span>
    </div>
    <span class="badge bg-success">VALIDÉ</span>
</div>

<div class="row g-3 mb-4">
    <div class="col-md-3">
        <div class="darlink-card p-3 h-100">
            <div class="text-muted small">Total dû fournisseurs</div>
            <div class="h4 mb-0"><?= $formatMoney($total_du ?? 0) ?></div>
            <div class="small text-muted"><?= (int)($nb_a_payer ?? 0) ?> facture(s) à payer</div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="darlink-card p-3 h-100">
            <div class="text-muted small">Factures en retard</div>
            <div class="h4 mb-0 text-danger"><?= $formatMoney($total_retard ?? 0) ?></div>
            <div class="small text-muted">Suivi dans l’échéancier</div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="darlink-card p-3 h-100">
            <div class="text-muted small">Réglé ce mois</div>
            <div class="h4 mb-0 text-success"><?= $formatMoney($total_regle_mois ?? 0) ?></div>
            <div class="small text-muted">Historique règlements</div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="darlink-card p-3 h-100">
            <div class="text-muted small">État du module</div>
            <div class="h4 mb-0 text-success">Stable</div>
            <div class="small text-muted">Aucun SQL supplémentaire</div>
        </div>
    </div>
</div>

<div class="row g-3 mb-4">
    <div class="col-md-7">
        <div class="darlink-card p-4 h-100">
            <h5 class="mb-3">Règles métier verrouillées</h5>
            <div class="table-responsive">
                <table class="table table-hover align-middle darlink-table">
                    <thead>
                    <tr>
                        <th>Règle</th>
                        <th>État</th>
                        <th>Détail</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach (($rules ?? []) as $rule): ?>
                        <tr>
                            <td><strong><?= htmlspecialchars($rule['titre']) ?></strong></td>
                            <td><span class="badge bg-success"><?= htmlspecialchars($rule['etat']) ?></span></td>
                            <td><?= htmlspecialchars($rule['detail']) ?></td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="col-md-5">
        <div class="darlink-card p-4 h-100">
            <h5 class="mb-3">Cycle fournisseur final</h5>
            <div class="list-group list-group-flush">
                <div class="list-group-item px-0 d-flex justify-content-between"><span>1. Commande fournisseur</span><span class="badge bg-success">OK</span></div>
                <div class="list-group-item px-0 d-flex justify-content-between"><span>2. Réception fournisseur</span><span class="badge bg-success">OK</span></div>
                <div class="list-group-item px-0 d-flex justify-content-between"><span>3. Facture fournisseur</span><span class="badge bg-success">OK</span></div>
                <div class="list-group-item px-0 d-flex justify-content-between"><span>4. Règlement fournisseur</span><span class="badge bg-success">OK</span></div>
                <div class="list-group-item px-0 d-flex justify-content-between"><span>5. Échéancier fournisseurs</span><span class="badge bg-success">OK</span></div>
                <div class="list-group-item px-0 d-flex justify-content-between"><span>6. Tableau de bord fournisseurs</span><span class="badge bg-success">OK</span></div>
            </div>
        </div>
    </div>
</div>

<div class="darlink-card p-4">
    <h5 class="mb-3">Accès rapides</h5>
    <div class="row g-3">
        <?php foreach (($links ?? []) as $link): ?>
            <div class="col-md-4">
                <a href="index.php?page=<?= htmlspecialchars($link['page']) ?>" class="text-decoration-none">
                    <div class="border rounded p-3 h-100">
                        <strong><?= htmlspecialchars($link['label']) ?></strong>
                        <div class="small text-muted mt-1"><?= htmlspecialchars($link['description']) ?></div>
                    </div>
                </a>
            </div>
        <?php endforeach; ?>
    </div>
</div>
