<?php
function stock_state(float $qte, float $mini): array {
    if ($qte <= 0) return ['Rupture', 'darlink-badge-danger'];
    if ($mini > 0 && $qte <= $mini) return ['Alerte', 'darlink-badge-warning'];
    return ['OK', 'darlink-badge-success'];
}
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="darlink-title mb-1">
            <i class="bi bi-box-seam me-2"></i>Stock
        </h1>
        <p class="text-muted mb-0"><?= (int) $summary['total_articles'] ?> article(s) suivis</p>
    </div>

    <div class="d-flex gap-2 flex-wrap">
        <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_entry" class="btn btn-outline-primary">+ Entrée</a>
        <a href="index.php?page=stock_adjust" class="btn btn-outline-primary">Ajustement</a>
        <a href="index.php?page=stock_alerts" class="btn btn-outline-warning">Alertes</a>
        <a href="index.php?page=stock_valuation" class="btn btn-outline-success">Valorisation</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-stat">
            <span>Valeur du stock</span>
            <strong><?= number_format((float)$summary['valeur_stock'], 2, ',', ' ') ?> MAD</strong>
        </div>
    </div>

    <div class="col-md-3">
        <div class="darlink-stat">
            <span>Articles</span>
            <strong><?= (int)$summary['total_articles'] ?></strong>
        </div>
    </div>

    <div class="col-md-3">
        <div class="darlink-stat">
            <span>Ruptures</span>
            <strong class="text-danger"><?= (int)$summary['ruptures'] ?></strong>
        </div>
    </div>

    <div class="col-md-3">
        <div class="darlink-stat">
            <span>Alertes</span>
            <strong class="text-warning"><?= (int)$summary['alertes'] ?></strong>
        </div>
    </div>
</div>

<div class="darlink-card darlink-table-card">
    <div class="darlink-table-toolbar">
        <div class="darlink-table-toolbar-left">
            <div class="darlink-table-search">
                <i class="bi bi-search"></i>
                <input type="text" id="stockSearch" placeholder="Rechercher un article en stock...">
            </div>
        </div>

        <div class="darlink-table-toolbar-right">
            <span class="text-muted small">Valeur = stock × prix d’achat HT</span>

            <button type="button" class="darlink-toolbar-btn" onclick="location.reload();">
                <i class="bi bi-arrow-clockwise"></i> Actualiser
            </button>
        </div>
    </div>

    <div class="table-responsive darlink-table-wrapper">
        <table class="table align-middle darlink-table darlink-table-modern" id="stockTable">
            <thead>
            <tr>
                <th class="darlink-sortable" data-column="0">Référence <span class="sort-indicator">↕</span></th>
                <th class="darlink-sortable" data-column="1">Désignation <span class="sort-indicator">↕</span></th>
                <th class="text-end darlink-sortable" data-column="2">Stock <span class="sort-indicator">↕</span></th>
                <th class="text-end darlink-sortable" data-column="3">Mini <span class="sort-indicator">↕</span></th>
                <th class="text-end darlink-sortable" data-column="4">Achat HT <span class="sort-indicator">↕</span></th>
                <th class="text-end darlink-sortable" data-column="5">Valeur <span class="sort-indicator">↕</span></th>
                <th class="darlink-sortable" data-column="6">État <span class="sort-indicator">↕</span></th>
                <th class="text-end">Actions</th>
            </tr>
            </thead>

            <tbody>
            <?php if (empty($stocks)): ?>
                <tr>
                    <td colspan="8" class="text-center text-muted py-5">
                        Aucun article en stock.
                    </td>
                </tr>
            <?php endif; ?>

            <?php foreach ($stocks as $row): ?>
                <?php [$stateLabel, $stateClass] = stock_state((float)$row['quantite'], (float)$row['stock_minimum']); ?>

                <tr data-edit-url="index.php?page=stock_show&id=<?= (int)$row['article_id'] ?>">
                    <td>
                        <strong class="darlink-table-code">
                            <?= htmlspecialchars($row['reference']) ?>
                        </strong>
                    </td>

                    <td class="fw-semibold">
                        <?= htmlspecialchars($row['designation']) ?>
                    </td>

                    <td class="text-end fw-bold">
                        <?= number_format((float)$row['quantite'], 0, ',', ' ') ?>
                        <?= htmlspecialchars($row['unite'] ?? '') ?>
                    </td>

                    <td class="text-end">
                        <?= number_format((float)$row['stock_minimum'], 0, ',', ' ') ?>
                    </td>

                    <td class="text-end">
                        <?= number_format((float)$row['prix_achat_ht'], 2, ',', ' ') ?>
                    </td>

                    <td class="text-end fw-semibold">
                        <?= number_format((float)$row['valeur_stock'], 2, ',', ' ') ?>
                    </td>

                    <td>
                        <span class="darlink-badge <?= $stateClass ?>">
                            <?= $stateLabel ?>
                        </span>
                    </td>

                    <td class="text-end darlink-actions">
                        <a href="index.php?page=stock_alerts&id=<?= (int)$row['article_id'] ?>"
                           class="darlink-icon-btn darlink-icon-btn-view"
                           title="Mini">
                            <i class="bi bi-bell"></i>
                        </a>

                        <a href="index.php?page=stock_adjust&id=<?= (int)$row['article_id'] ?>"
                           class="darlink-icon-btn darlink-icon-btn-edit"
                           title="Ajuster">
                            <i class="bi bi-sliders"></i>
                        </a>

                        <a href="index.php?page=stock_show&id=<?= (int)$row['article_id'] ?>"
                           class="darlink-icon-btn darlink-icon-btn-order"
                           title="Historique">
                            <i class="bi bi-clock-history"></i>
                        </a>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
