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

function dash_money(float $amount): string { return number_format($amount, 2, ',', ' ') . ' MAD'; }
function dash_qty(float $qty): string { return number_format($qty, 0, ',', ' '); }
function dash_badge_stock(array $row): array {
    $state = (string)($row['etat_stock'] ?? '');
    if ($state === 'rupture') return ['Rupture', 'text-bg-danger'];
    if ($state === 'alerte') return ['Stock bas', 'text-bg-warning'];
    return ['OK', 'text-bg-success'];
}
function dash_badge_move(string $type): string {
    return match ($type) {
        'ENT' => 'text-bg-success',
        'SORT' => 'text-bg-danger',
        'INV' => 'text-bg-info',
        'ADJ' => 'text-bg-warning',
        default => 'text-bg-secondary',
    };
}
?>
<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="darlink-title mb-1">Tableau de bord stock</h1>
        <p class="text-muted mb-0">Vue rapide du stock, des alertes, de la valorisation et des derniers mouvements</p>
    </div>
    <div class="d-flex gap-2 flex-wrap justify-content-end">
        <a href="index.php?page=stock" class="btn btn-outline-secondary">Liste 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-primary">Historique</a>
        <a href="index.php?page=stock_finalisation" class="btn darlink-btn-primary">Cycle stock</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"><?= dash_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-3 mb-4">
    <div class="col-md-3"><div class="darlink-card p-3"><div class="text-muted">Valeur vente HT</div><div class="h5 mb-0"><?= dash_money((float)($valuation['valeur_vente_ht'] ?? 0)) ?></div></div></div>
    <div class="col-md-3"><div class="darlink-card p-3"><div class="text-muted">Marge théorique HT</div><div class="h5 mb-0 text-success"><?= dash_money((float)($valuation['marge_theorique_ht'] ?? 0)) ?></div></div></div>
    <div class="col-md-3"><div class="darlink-card p-3"><div class="text-muted">Entrées 30 jours</div><div class="h5 mb-0 text-success"><?= dash_qty((float)($movementSummary['entrees'] ?? 0)) ?></div></div></div>
    <div class="col-md-3"><div class="darlink-card p-3"><div class="text-muted">Sorties 30 jours</div><div class="h5 mb-0 text-danger"><?= dash_qty((float)($movementSummary['sorties'] ?? 0)) ?></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 à traiter</h5>
                <a href="index.php?page=stock_alerts" class="btn btn-sm btn-outline-warning">Voir toutes</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>État</th><th class="text-end">Action</th></tr></thead>
                    <tbody>
                    <?php foreach ($lowStock as $row): ?>
                        <?php [$label, $class] = dash_badge_stock($row); ?>
                        <tr>
                            <td><strong><?= htmlspecialchars($row['reference'] ?? '') ?></strong><br><span class="text-muted small"><?= htmlspecialchars($row['designation'] ?? '') ?></span></td>
                            <td class="text-end"><?= dash_qty((float)($row['quantite'] ?? 0)) ?></td>
                            <td class="text-end"><?= dash_qty((float)($row['stock_minimum'] ?? 0)) ?></td>
                            <td><span class="badge <?= $class ?>"><?= $label ?></span></td>
                            <td class="text-end"><a class="btn btn-sm btn-outline-warning" href="index.php?page=stock_adjust&id=<?= (int)($row['article_id'] ?? 0) ?>">Ajuster</a></td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (empty($lowStock)): ?>
                        <tr><td colspan="5" 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">Articles à forte valeur</h5>
                <a href="index.php?page=stock_valuation" class="btn btn-sm btn-outline-success">Valorisation</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">Valeur achat</th><th class="text-end">Marge théo.</th></tr></thead>
                    <tbody>
                    <?php foreach ($topValue as $row): ?>
                        <tr>
                            <td><strong><?= htmlspecialchars($row['reference'] ?? '') ?></strong><br><span class="text-muted small"><?= htmlspecialchars($row['designation'] ?? '') ?></span></td>
                            <td class="text-end"><?= dash_qty((float)($row['quantite'] ?? 0)) ?></td>
                            <td class="text-end"><?= dash_money((float)($row['valeur_achat_ht'] ?? 0)) ?></td>
                            <td class="text-end text-success"><?= dash_money((float)($row['marge_theorique_ht'] ?? 0)) ?></td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (empty($topValue)): ?>
                        <tr><td colspan="4" class="text-center text-muted py-4">Aucune valorisation disponible.</td></tr>
                    <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<div class="darlink-card p-4">
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h5 class="mb-0">Derniers mouvements stock</h5>
        <a href="index.php?page=stock_history" class="btn btn-sm btn-outline-primary">Historique complet</a>
    </div>
    <div class="table-responsive">
        <table class="table table-hover align-middle darlink-table mb-0">
            <thead><tr><th>Date</th><th>Article</th><th>Type</th><th>Origine</th><th class="text-end">Quantité</th><th class="text-end">Achat HT</th></tr></thead>
            <tbody>
            <?php foreach ($recentMovements as $move): ?>
                <tr>
                    <td><?= htmlspecialchars(substr((string)($move['date_mouvement'] ?? ''), 0, 16)) ?></td>
                    <td><strong><?= htmlspecialchars($move['reference'] ?? '') ?></strong><br><span class="text-muted small"><?= htmlspecialchars($move['designation'] ?? '') ?></span></td>
                    <td><span class="badge <?= dash_badge_move((string)($move['type_mouvement'] ?? '')) ?>"><?= htmlspecialchars($move['type_mouvement'] ?? '') ?></span></td>
                    <td><?= htmlspecialchars($move['origine'] ?? '') ?></td>
                    <td class="text-end fw-bold"><?= dash_qty((float)($move['quantite'] ?? 0)) ?></td>
                    <td class="text-end"><?= dash_money((float)($move['prix_achat_ht'] ?? 0)) ?></td>
                </tr>
            <?php endforeach; ?>
            <?php if (empty($recentMovements)): ?>
                <tr><td colspan="6" class="text-center text-muted py-4">Aucun mouvement stock récent.</td></tr>
            <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
