<?= $this->extend('layout/main') ?>
<?= $this->section('content') ?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h5 class="m-0">Produits et Services</h5>
    <a href="/products/create" class="btn btn-primary"><i class="bi bi-plus-lg"></i> Nouveau Produit</a>
</div>

<div class="table-card">
    <table class="table table-hover" id="productsTable">
        <thead>
            <tr>
                <th>Reference</th>
                <th>Designation</th>
                <th>Categorie</th>
                <th class="text-end">Prix HT</th>
                <th class="text-end">TVA</th>
                <th>Statut</th>
                <th class="text-center">Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($products as $p): ?>
                <tr>
                    <td><code><?= $p['reference'] ?></code></td>
                    <td><?= $p['designation'] ?></td>
                    <td><?= $p['category'] ?: '-' ?></td>
                    <td class="text-end"><?= number_format($p['unit_price'], 3, ',', ' ') ?> TND</td>
                    <td class="text-end"><?= ($p['vat_rate'] * 100) ?>%</td>
                    <td>
                        <?php if ($p['is_active']): ?>
                            <span class="badge bg-success">Actif</span>
                        <?php else: ?>
                            <span class="badge bg-secondary">Inactif</span>
                        <?php endif; ?>
                    </td>
                    <td class="text-center">
                        <div class="btn-group btn-group-sm">
                            <a href="/products/edit/<?= $p['id'] ?>" class="btn btn-outline-warning"><i class="bi bi-pencil"></i></a>
                            <button class="btn btn-outline-danger" onclick="deleteProduct(<?= $p['id'] ?>)"><i class="bi bi-trash"></i></button>
                        </div>
                    </td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <?= $pager->links() ?>
</div>

<?= $this->endSection() ?>

<?= $this->section('scripts') ?>
<script>
function deleteProduct(id) {
    Swal.fire({
        title: 'Confirmer?',
        text: 'Le produit sera supprime.',
        icon: 'warning',
        showCancelButton: true,
        confirmButtonText: 'Oui',
        cancelButtonText: 'Annuler'
    }).then((result) => {
        if (result.isConfirmed) {
            fetch('/products/delete/' + id, {
                method: 'POST',
                headers: { 'X-Requested-With': 'XMLHttpRequest' }
            })
            .then(r => r.json())
            .then(data => {
                if (data.success) location.reload();
            });
        }
    });
}

$(document).ready(function() {
    $('#productsTable').DataTable({
        paging: false,
        info: false,
        searching: false,
        language: { url: '//cdn.datatables.net/plug-ins/1.13.7/i18n/fr-FR.json' }
    });
});
</script>
<?= $this->endSection() ?>
