<?php
$valid = array_filter($rows, fn($row) => empty($row['errors']));
$errors = array_filter($rows, fn($row) => !empty($row['errors']));
?>
<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="darlink-title mb-1">Prévisualisation articles</h1>
        <p class="text-muted mb-0"><?= (int)$total ?> ligne(s) détectée(s), <?= count($valid) ?> valide(s), <?= count($errors) ?> erreur(s).</p>
    </div>
    <a href="index.php?page=articles_import" class="btn btn-outline-secondary">Recommencer</a>
</div>

<div class="darlink-card p-4 mb-4">
    <form method="post" action="index.php?page=articles_import_confirm">
        <input type="hidden" name="mode_import" value="<?= htmlspecialchars($mode) ?>">
        <div class="d-flex justify-content-between align-items-center">
            <div>
                <strong>Mode :</strong>
                <?= $mode === 'replace' ? 'Vider les articles puis importer' : 'Ajouter / mettre à jour' ?>
            </div>
            <button type="submit" class="btn darlink-btn-primary" <?= count($valid) === 0 ? 'disabled' : '' ?>>Confirmer l'import</button>
        </div>
    </form>
</div>

<div class="darlink-card p-4">
    <div class="table-responsive">
        <table class="table table-hover align-middle darlink-table">
            <thead>
            <tr>
                <th>Ligne</th>
                <th>Référence</th>
                <th>Désignation</th>
                <th class="text-end">Achat HT</th>
                <th class="text-end">Coef</th>
                <th class="text-end">Vente HT</th>
                <th class="text-end">TVA</th>
                <th class="text-end">Marge %</th>
                <th>Statut</th>
            </tr>
            </thead>
            <tbody>
            <?php foreach (array_slice($rows, 0, 50) as $row): ?>
                <tr>
                    <td><?= (int)$row['line'] ?></td>
                    <td><strong><?= htmlspecialchars($row['reference']) ?></strong></td>
                    <td><?= htmlspecialchars($row['designation']) ?></td>
                    <td class="text-end"><?= number_format((float)$row['prix_achat_ht'], 2, ',', ' ') ?></td>
                    <td class="text-end"><?= number_format((float)$row['coefficient'], 3, ',', ' ') ?></td>
                    <td class="text-end"><?= number_format((float)$row['prix_vente_ht'], 2, ',', ' ') ?></td>
                    <td class="text-end"><?= number_format((float)$row['taux_tva'], 2, ',', ' ') ?> %</td>
                    <td class="text-end"><?= number_format((float)$row['marge_pourcentage'], 2, ',', ' ') ?> %</td>
                    <td>
                        <?php if (empty($row['errors'])): ?>
                            <span class="badge text-bg-success">Valide</span>
                        <?php else: ?>
                            <span class="badge text-bg-danger"><?= htmlspecialchars(implode(', ', $row['errors'])) ?></span>
                        <?php endif; ?>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
        <?php if (count($rows) > 50): ?>
            <p class="text-muted small mb-0">Aperçu limité aux 50 premières lignes.</p>
        <?php endif; ?>
    </div>
</div>
