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

<div class="d-flex justify-content-between align-items-center mb-4">
    <h5 class="m-0">Liste des Clients</h5>
    <a href="/clients/create" class="btn btn-primary"><i class="bi bi-plus-lg"></i> Nouveau Client</a>
</div>

<div class="table-card">
    <div class="row mb-3">
        <div class="col-md-4">
            <form method="GET" class="d-flex gap-2">
                <input type="text" name="search" class="form-control" placeholder="Rechercher..." value="<?= $search ?? '' ?>">
                <button type="submit" class="btn btn-outline-secondary"><i class="bi bi-search"></i></button>
            </form>
        </div>
    </div>

    <table class="table table-hover" id="clientsTable">
        <thead>
            <tr>
                <th>Code</th>
                <th>Societe</th>
                <th>Contact</th>
                <th>Email</th>
                <th>Telephone</th>
                <th>Ville</th>
                <th>Statut</th>
                <th class="text-center">Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($clients as $c): ?>
                <tr>
                    <td><span class="badge bg-secondary"><?= $c['code'] ?></span></td>
                    <td><a href="/clients/view/<?= $c['id'] ?>" class="fw-bold text-decoration-none"><?= $c['company_name'] ?></a></td>
                    <td><?= $c['contact_name'] ?: '-' ?></td>
                    <td><?= $c['email'] ?: '-' ?></td>
                    <td><?= $c['phone'] ?: '-' ?></td>
                    <td><?= $c['city'] ?: '-' ?></td>
                    <td>
                        <?php if ($c['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="/clients/view/<?= $c['id'] ?>" class="btn btn-outline-primary" title="Voir"><i class="bi bi-eye"></i></a>
                            <a href="/clients/edit/<?= $c['id'] ?>" class="btn btn-outline-warning" title="Modifier"><i class="bi bi-pencil"></i></a>
                            <button class="btn btn-outline-danger" onclick="deleteClient(<?= $c['id'] ?>)" title="Supprimer"><i class="bi bi-trash"></i></button>
                        </div>
                    </td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <?= $pager->links() ?>
</div>

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

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

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