<?php
$summary = $summary ?? [];
$valuation = $valuation ?? [];
$movementSummary = $movementSummary ?? [];
$lowStock = $lowStock ?? [];
$recentMovements = $recentMovements ?? [];

function sf_money(float $amount): string { return number_format($amount, 2, ',', ' ') . ' MAD'; }
function sf_qty(float $qty): string { return number_format($qty, 0, ',', ' '); }
?>
<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="darlink-title mb-1">Finalisation stock avancé</h1>
        <p class="text-muted mb-0">Synthèse du cycle stock avancé — Phoenix P3.4.7</p>
    </div>
    <div class="d-flex gap-2 flex-wrap justify-content-end">
        <a href="index.php?page=stock_dashboard" class="btn darlink-btn-primary">Tableau stock</a>
        <a href="index.php?page=stock_inventory" class="btn btn-outline-primary">Inventaire</a>
        <a href="index.php?page=stock_adjust" class="btn btn-outline-warning">Ajustement</a>
        <a href="index.php?page=stock_history" class="btn btn-outline-secondary">Historique</a>
    </div>
</div>

<div class="row g-3 mb-4">
    <div class="col-md-3"><div class="darlink-card p-3"><div class="text-muted">Articles suivis</div><div class="h4 mb-0"><?= (int)($summary['total_articles'] ?? 0) ?></div></div></div>
    <div class="col-md-3"><div class="darlink-card p-3"><div class="text-muted">Valeur achat HT</div><div class="h4 mb-0"><?= sf_money((float)($valuation['valeur_achat_ht'] ?? $summary['valeur_stock'] ?? 0)) ?></div></div></div>
    <div class="col-md-3"><div class="darlink-card p-3"><div class="text-muted">Ruptures</div><div class="h4 mb-0 text-danger"><?= (int)($summary['ruptures'] ?? 0) ?></div></div></div>
    <div class="col-md-3"><div class="darlink-card p-3"><div class="text-muted">Alertes stock bas</div><div class="h4 mb-0 text-warning"><?= (int)($summary['alertes'] ?? 0) ?></div></div></div>
</div>

<div class="row g-4 mb-4">
    <div class="col-12">
        <div class="darlink-card p-4 h-100">
            <h5 class="mb-3">Synthèse 30 derniers jours</h5>
            <div class="row g-3">
                <div class="col-6"><div class="border rounded p-3"><div class="text-muted">Entrées</div><div class="h5 text-success mb-0"><?= sf_qty((float)($movementSummary['entrees'] ?? 0)) ?></div></div></div>
                <div class="col-6"><div class="border rounded p-3"><div class="text-muted">Sorties</div><div class="h5 text-danger mb-0"><?= sf_qty((float)($movementSummary['sorties'] ?? 0)) ?></div></div></div>
                <div class="col-6"><div class="border rounded p-3"><div class="text-muted">Inventaires</div><div class="h5 mb-0"><?= sf_qty((float)($movementSummary['inventaires'] ?? 0)) ?></div></div></div>
                <div class="col-6"><div class="border rounded p-3"><div class="text-muted">Ajustements</div><div class="h5 mb-0"><?= sf_qty((float)($movementSummary['ajustements'] ?? 0)) ?></div></div></div>
            </div>
        </div>
    </div>
</div>

<div class="row g-4 mb-4">
    <div class="col-lg-6">
        <div class="darlink-card p-4 h-100">
            <div class="d-flex justify-content-between align-items-center mb-3">
                <h5 class="mb-0">Alertes à surveiller</h5>
                <a href="index.php?page=stock_alerts" class="btn btn-sm btn-outline-warning">Voir alertes</a>
            </div>
            <div class="table-responsive">
                <table class="table table-hover align-middle darlink-table mb-0">
                    <thead><tr><th>Article</th><th class="text-end">Stock</th><th class="text-end">Mini</th><th class="text-end">Besoin</th></tr></thead>
                    <tbody>
                    <?php foreach ($lowStock as $row): ?>
                        <tr>
                            <td><strong><?= htmlspecialchars($row['reference'] ?? '') ?></strong><br><span class="text-muted small"><?= htmlspecialchars($row['designation'] ?? '') ?></span></td>
                            <td class="text-end"><?= sf_qty((float)($row['quantite'] ?? 0)) ?></td>
                            <td class="text-end"><?= sf_qty((float)($row['stock_minimum'] ?? 0)) ?></td>
                            <td class="text-end fw-bold"><?= sf_qty((float)($row['besoin_reassort'] ?? 0)) ?></td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (empty($lowStock)): ?><tr><td colspan="4" class="text-center text-muted py-4">Aucune alerte stock.</td></tr><?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="col-lg-6">
        <div class="darlink-card p-4 h-100">
            <div class="d-flex justify-content-between align-items-center mb-3">
                <h5 class="mb-0">Accès rapides</h5>
            </div>
            <div class="d-grid gap-2">
                <a href="index.php?page=stock_inventory" class="btn btn-outline-primary text-start">Inventaire complet</a>
                <a href="index.php?page=stock_adjust" class="btn btn-outline-warning text-start">Ajustement manuel</a>
                <a href="index.php?page=stock_alerts" class="btn btn-outline-warning text-start">Stock minimum / alertes</a>
                <a href="index.php?page=stock_valuation" class="btn btn-outline-success text-start">Valorisation du stock</a>
                <a href="index.php?page=stock_history" class="btn btn-outline-secondary text-start">Historique stock avancé</a>
                <a href="index.php?page=stock_dashboard" class="btn darlink-btn-primary text-start">Tableau de bord stock</a>
            </div>
        </div>
    </div>
</div>
