<?php
function stock_type_label(string $type): string {
    return match ($type) {
        'ENT' => 'Entrée',
        'SOR', 'SORT' => 'Sortie',
        'INV' => 'Inventaire',
        'ADJ' => 'Ajustement',
        default => $type ?: '-',
    };
}
function stock_type_badge(string $type): string {
    return match ($type) {
        'ENT' => 'text-bg-success',
        'SOR', 'SORT' => 'text-bg-danger',
        'INV' => 'text-bg-primary',
        'ADJ' => 'text-bg-warning',
        default => 'text-bg-light border',
    };
}
?>
<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="darlink-title mb-1">Historique stock avancé</h1>
        <p class="text-muted mb-0">Contrôle complet des entrées, sorties, réceptions, inventaires et ajustements.</p>
    </div>
    <div class="d-flex gap-2">
        <a href="index.php?page=stock" class="btn btn-outline-secondary">Retour au stock</a>
        <a href="index.php?page=stock_adjust" class="btn darlink-btn-primary">Ajuster un article</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 small">Mouvements affichés</div><div class="h4 mb-0"><?= (int)($summary['total_mouvements'] ?? 0) ?></div></div></div>
    <div class="col-md-3"><div class="darlink-card p-3"><div class="text-muted small">Entrées</div><div class="h4 mb-0 text-success"><?= number_format((float)($summary['entrees'] ?? 0), 2, ',', ' ') ?></div><div class="small text-muted"><?= number_format((float)($summary['valeur_entrees_ht'] ?? 0), 2, ',', ' ') ?> HT</div></div></div>
    <div class="col-md-3"><div class="darlink-card p-3"><div class="text-muted small">Sorties</div><div class="h4 mb-0 text-danger"><?= number_format((float)($summary['sorties'] ?? 0), 2, ',', ' ') ?></div><div class="small text-muted"><?= number_format((float)($summary['valeur_sorties_ht'] ?? 0), 2, ',', ' ') ?> HT</div></div></div>
    <div class="col-md-3"><div class="darlink-card p-3"><div class="text-muted small">Contrôles</div><div class="h4 mb-0"><?= (int)($summary['inventaires'] ?? 0) ?> INV / <?= (int)($summary['ajustements'] ?? 0) ?> ADJ</div></div></div>
</div>

<div class="darlink-card p-4 mb-4">
    <form method="get" class="row g-3 align-items-end">
        <input type="hidden" name="page" value="stock_history">
        <div class="col-md-3">
            <label class="form-label">Article</label>
            <input type="text" name="article" class="form-control" placeholder="Référence ou désignation" value="<?= htmlspecialchars($filters['article'] ?? '') ?>">
        </div>
        <div class="col-md-2">
            <label class="form-label">Type</label>
            <select name="type" class="form-select">
                <option value="">Tous</option>
                <?php foreach ($types as $type): ?>
                    <option value="<?= htmlspecialchars($type) ?>" <?= ($filters['type'] ?? '') === $type ? 'selected' : '' ?>><?= htmlspecialchars(stock_type_label((string)$type)) ?> (<?= htmlspecialchars($type) ?>)</option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-2">
            <label class="form-label">Origine</label>
            <select name="origine" class="form-select">
                <option value="">Toutes</option>
                <?php foreach ($origins as $origine): ?>
                    <option value="<?= htmlspecialchars($origine) ?>" <?= ($filters['origine'] ?? '') === $origine ? 'selected' : '' ?>><?= htmlspecialchars($origine) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-2">
            <label class="form-label">Sens</label>
            <select name="sens" class="form-select">
                <option value="">Tous</option>
                <option value="entree" <?= ($filters['sens'] ?? '') === 'entree' ? 'selected' : '' ?>>Entrées seulement</option>
                <option value="sortie" <?= ($filters['sens'] ?? '') === 'sortie' ? 'selected' : '' ?>>Sorties seulement</option>
            </select>
        </div>
        <div class="col-md-1">
            <label class="form-label">Du</label>
            <input type="date" name="date_debut" class="form-control" value="<?= htmlspecialchars($filters['date_debut'] ?? '') ?>">
        </div>
        <div class="col-md-1">
            <label class="form-label">Au</label>
            <input type="date" name="date_fin" class="form-control" value="<?= htmlspecialchars($filters['date_fin'] ?? '') ?>">
        </div>
        <div class="col-md-1 d-grid gap-2">
            <button type="submit" class="btn darlink-btn-primary">Filtrer</button>
            <a href="index.php?page=stock_history" class="btn btn-outline-secondary btn-sm">Reset</a>
        </div>
    </form>
</div>

<div class="darlink-card p-4">
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h5 class="mb-0">Mouvements stock</h5>
        <span class="text-muted small"><?= count($movements) ?> mouvement(s) affiché(s), limite 1000</span>
    </div>
    <div class="table-responsive">
        <table class="table table-hover align-middle darlink-table">
            <thead>
                <tr>
                    <th>Date</th>
                    <th>Article</th>
                    <th>Type</th>
                    <th class="text-end">Quantité</th>
                    <th class="text-end">Prix achat HT</th>
                    <th class="text-end">Valeur HT</th>
                    <th>Origine</th>
                    <th>Commentaire</th>
                    <th class="text-end">Action</th>
                </tr>
            </thead>
            <tbody>
            <?php if (empty($movements)): ?>
                <tr><td colspan="9" class="text-center text-muted py-5">Aucun mouvement trouvé.</td></tr>
            <?php endif; ?>
            <?php foreach ($movements as $m): ?>
                <?php
                    $qte = (float)($m['quantite'] ?? 0);
                    $valeur = (float)($m['valeur_mouvement_ht'] ?? 0);
                    $type = (string)($m['type_mouvement'] ?? '');
                ?>
                <tr>
                    <td><?= htmlspecialchars($m['date_mouvement'] ?? '') ?></td>
                    <td>
                        <strong><?= htmlspecialchars($m['reference'] ?? '') ?></strong><br>
                        <span class="text-muted small"><?= htmlspecialchars($m['designation'] ?? '') ?></span>
                    </td>
                    <td><span class="badge <?= htmlspecialchars(stock_type_badge($type)) ?>"><?= htmlspecialchars(stock_type_label($type)) ?></span><br><span class="text-muted small"><?= htmlspecialchars($type) ?></span></td>
                    <td class="text-end <?= $qte < 0 ? 'text-danger' : 'text-success' ?>"><?= number_format($qte, 2, ',', ' ') ?> <?= htmlspecialchars($m['unite'] ?? '') ?></td>
                    <td class="text-end"><?= number_format((float)($m['prix_achat_ht'] ?? 0), 2, ',', ' ') ?></td>
                    <td class="text-end <?= $valeur < 0 ? 'text-danger' : 'text-success' ?>"><?= number_format($valeur, 2, ',', ' ') ?></td>
                    <td><?= htmlspecialchars(trim(($m['origine'] ?? '') . ' #' . ($m['origine_id'] ?? ''), ' #')) ?: '-' ?></td>
                    <td><?= htmlspecialchars($m['commentaire'] ?? '-') ?></td>
                    <td class="text-end"><a href="index.php?page=stock_show&id=<?= (int)($m['article_id'] ?? 0) ?>" class="btn btn-sm btn-outline-primary">Article</a></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
