<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="darlink-title mb-1"><i class="bi bi-tags me-2"></i>Articles</h1>
        <p class="text-muted mb-0"><?= (int)$totalArticles ?> article(s) <?= !empty($showAll) ? 'affiché(s)' : 'actif(s)' ?></p>
    </div>
    <div class="d-flex gap-2">
        <a href="index.php?page=articles_create" class="btn darlink-btn-primary"><i class="bi bi-plus-lg me-1"></i> Nouvel article</a>
        <a href="index.php?page=articles_import" class="btn btn-outline-success"><i class="bi bi-file-earmark-excel me-1"></i> Importer Excel</a>
    </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="articleSearch" placeholder="Référence, désignation, famille, marque, fournisseur..."></div>
        </div>
        <div class="darlink-table-toolbar-right d-flex gap-2">
            <a class="darlink-toolbar-btn" href="index.php?page=articles<?= !empty($showAll) ? '' : '&show=all' ?>"><i class="bi bi-eye"></i> <?= !empty($showAll) ? 'Actifs seulement' : 'Afficher tous' ?></a>
            <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="articlesTable">
            <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">Achat HT <span class="sort-indicator">↕</span></th>
                <th class="text-end darlink-sortable" data-column="3">Coef <span class="sort-indicator">↕</span></th>
                <th class="text-end darlink-sortable" data-column="4">Vente HT <span class="sort-indicator">↕</span></th>
                <th class="text-end darlink-sortable" data-column="5">TVA <span class="sort-indicator">↕</span></th>
                <th class="text-end darlink-sortable" data-column="6">Marge <span class="sort-indicator">↕</span></th>
                <th class="darlink-sortable" data-column="7">Fournisseur <span class="sort-indicator">↕</span></th>
                <th>Statut</th><th class="text-end">Actions</th>
            </tr></thead>
            <tbody>
            <?php if (empty($articles)): ?><tr><td colspan="10" class="text-center text-muted py-5">Aucun article enregistré pour le moment.</td></tr><?php endif; ?>
            <?php foreach ($articles as $article): $isActive = !empty($article['actif']); ?>
                <tr class="<?= $isActive ? '' : 'opacity-50' ?>" data-edit-url="index.php?page=articles_edit&id=<?= (int)$article['id'] ?>">
                    <td><strong class="darlink-table-code"><?= htmlspecialchars($article['reference']) ?></strong></td>
                    <td class="fw-semibold"><?= htmlspecialchars($article['designation']) ?><div class="small text-muted"><?= htmlspecialchars(trim(($article['famille'] ?? '').' '.($article['marque'] ?? ''))) ?></div></td>
                    <td class="text-end"><?= number_format((float)$article['prix_achat_ht'],2,',',' ') ?></td>
                    <td class="text-end"><?= number_format((float)$article['coefficient'],3,',',' ') ?></td>
                    <td class="text-end fw-semibold"><?= number_format((float)$article['prix_vente_ht'],2,',',' ') ?></td>
                    <td class="text-end"><?= number_format((float)$article['taux_tva'],2,',',' ') ?> %</td>
                    <td class="text-end"><span class="fw-semibold"><?= number_format((float)$article['marge_dh'],2,',',' ') ?></span><div class="small text-muted"><?= number_format((float)$article['marge_pourcentage'],2,',',' ') ?> %</div></td>
                    <td><?= !empty($article['fournisseur_principal_nom']) ? '<span class="darlink-badge darlink-badge-light">'.htmlspecialchars($article['fournisseur_principal_nom']).'</span>' : '<span class="text-muted">-</span>' ?></td>
                    <td><span class="darlink-badge <?= $isActive ? 'darlink-badge-success' : 'darlink-badge-light' ?>"><?= $isActive ? 'Actif' : 'Inactif' ?></span></td>
                    <td class="text-end darlink-actions">
                        <a class="darlink-icon-btn darlink-icon-btn-view" href="index.php?page=articles_achats&id=<?= (int)$article['id'] ?>" title="Fiche 360°"><i class="bi bi-eye"></i></a>
                        <a class="darlink-icon-btn darlink-icon-btn-edit" href="index.php?page=articles_edit&id=<?= (int)$article['id'] ?>" title="Modifier"><i class="bi bi-pencil"></i></a>
                        <a class="darlink-icon-btn" href="index.php?page=articles_duplicate&id=<?= (int)$article['id'] ?>" title="Dupliquer"><i class="bi bi-copy"></i></a>
                        <a class="darlink-icon-btn" href="index.php?page=commandes_fournisseurs_historique&article_id=<?= (int)$article['id'] ?>" title="Historique"><i class="bi bi-clock-history"></i></a>
                        <form method="post" action="index.php?page=articles_toggle_active" class="d-inline" onsubmit="return confirm('<?= $isActive ? 'Désactiver cet article ?' : 'Réactiver cet article ?' ?>');">
                            <input type="hidden" name="id" value="<?= (int)$article['id'] ?>"><input type="hidden" name="active" value="<?= $isActive ? 0 : 1 ?>">
                            <button type="submit" class="darlink-icon-btn border-0" title="<?= $isActive ? 'Désactiver' : 'Réactiver' ?>"><i class="bi <?= $isActive ? 'bi-slash-circle' : 'bi-check-circle' ?>"></i></button>
                        </form>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
