<?php
$summary = $summary ?? [];
$rows = $rows ?? [];
function valuation_state(float $valeurAchat): array {
    if ($valeurAchat >= 1000) return ['Valeur forte', 'text-bg-warning'];
    if ($valeurAchat > 0) return ['En stock', 'text-bg-success'];
    return ['Sans stock', 'text-bg-secondary'];
}
?>
<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="darlink-title mb-1">Valorisation du stock</h1>
        <p class="text-muted mb-0">Valeur d’achat, valeur de vente et marge théorique du stock</p>
    </div>
    <div class="d-flex gap-2">
        <a href="index.php?page=stock" class="btn btn-outline-secondary">Retour stock</a>
        <a href="index.php?page=stock_alerts" class="btn btn-outline-warning">Alertes stock</a>
        <a href="index.php?page=stock_history" class="btn btn-outline-primary">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">Valeur achat HT</div><div class="h4 mb-0"><?= number_format((float)($summary['valeur_achat_ht'] ?? 0), 2, ',', ' ') ?> MAD</div></div></div>
    <div class="col-md-3"><div class="darlink-card p-3"><div class="text-muted">Valeur vente HT</div><div class="h4 mb-0"><?= number_format((float)($summary['valeur_vente_ht'] ?? 0), 2, ',', ' ') ?> MAD</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="h4 mb-0 text-success"><?= number_format((float)($summary['marge_theorique_ht'] ?? 0), 2, ',', ' ') ?> MAD</div></div></div>
    <div class="col-md-3"><div class="darlink-card p-3"><div class="text-muted">Taux marge théorique</div><div class="h4 mb-0"><?= number_format((float)($summary['taux_marge_theorique'] ?? 0), 2, ',', ' ') ?> %</div></div></div>
</div>

<div class="row g-3 mb-4">
    <div class="col-md-4"><div class="darlink-card p-3"><div class="text-muted">Articles suivis</div><div class="h5 mb-0"><?= (int)($summary['total_articles'] ?? 0) ?></div></div></div>
    <div class="col-md-4"><div class="darlink-card p-3"><div class="text-muted">Articles avec stock</div><div class="h5 mb-0"><?= (int)($summary['articles_en_stock'] ?? 0) ?></div></div></div>
    <div class="col-md-4"><div class="darlink-card p-3"><div class="text-muted">Articles à forte valeur</div><div class="h5 mb-0 text-warning"><?= (int)($summary['articles_valeur_forte'] ?? 0) ?></div></div></div>
</div>

<div class="darlink-card p-4">
    <div class="row g-2 align-items-center mb-3">
        <div class="col-md-6">
            <input type="text" id="valuationSearch" class="form-control darlink-search" placeholder="Rechercher par référence, désignation, valeur...">
        </div>
        <div class="col-md-6 text-md-end text-muted small">
            Calcul : stock × prix d’achat HT / stock × prix de vente HT
        </div>
    </div>

    <div class="table-responsive">
        <table class="table table-hover align-middle darlink-table" id="valuationTable">
            <thead>
            <tr>
                <th>Référence</th>
                <th>Désignation</th>
                <th class="text-end">Stock</th>
                <th class="text-end">Achat HT</th>
                <th class="text-end">Vente HT</th>
                <th class="text-end">Valeur achat</th>
                <th class="text-end">Valeur vente</th>
                <th class="text-end">Marge théo.</th>
                <th>État</th>
                <th class="text-end">Actions</th>
            </tr>
            </thead>
            <tbody>
            <?php foreach ($rows as $row): ?>
                <?php [$stateLabel, $stateClass] = valuation_state((float)($row['valeur_achat_ht'] ?? 0)); ?>
                <tr>
                    <td><strong><?= htmlspecialchars($row['reference'] ?? '') ?></strong></td>
                    <td><?= htmlspecialchars($row['designation'] ?? '') ?></td>
                    <td class="text-end fw-bold"><?= number_format((float)($row['quantite'] ?? 0), 0, ',', ' ') ?> <?= htmlspecialchars($row['unite'] ?? '') ?></td>
                    <td class="text-end"><?= number_format((float)($row['prix_achat_ht'] ?? 0), 2, ',', ' ') ?></td>
                    <td class="text-end"><?= number_format((float)($row['prix_vente_ht'] ?? 0), 2, ',', ' ') ?></td>
                    <td class="text-end"><?= number_format((float)($row['valeur_achat_ht'] ?? 0), 2, ',', ' ') ?></td>
                    <td class="text-end"><?= number_format((float)($row['valeur_vente_ht'] ?? 0), 2, ',', ' ') ?></td>
                    <td class="text-end text-success"><?= number_format((float)($row['marge_theorique_ht'] ?? 0), 2, ',', ' ') ?></td>
                    <td><span class="badge <?= $stateClass ?>"><?= $stateLabel ?></span></td>
                    <td class="text-end">
                        <div class="btn-group btn-group-sm">
                            <a href="index.php?page=articles_achats&id=<?= (int)($row['article_id'] ?? 0) ?>" class="btn btn-outline-success">Prix</a>
                            <a href="index.php?page=stock_adjust&id=<?= (int)($row['article_id'] ?? 0) ?>" class="btn btn-outline-warning">Ajuster</a>
                            <a href="index.php?page=stock_show&id=<?= (int)($row['article_id'] ?? 0) ?>" class="btn btn-outline-primary">Historique</a>
                        </div>
                    </td>
                </tr>
            <?php endforeach; ?>
            <?php if (empty($rows)): ?>
                <tr><td colspan="10" class="text-center text-muted py-4">Aucun article à valoriser.</td></tr>
            <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function () {
    const input = document.getElementById('valuationSearch');
    const rows = document.querySelectorAll('#valuationTable tbody tr');
    if (!input) return;
    input.addEventListener('input', function () {
        const query = this.value.toLowerCase();
        rows.forEach(function (row) {
            row.style.display = row.textContent.toLowerCase().includes(query) ? '' : 'none';
        });
    });
});
</script>
