<?php require dirname(__DIR__) . '/layouts/app.php'; ?>

<section class="page-heading">
    <div>
        <p class="eyebrow">Gestion locative</p>
        <h1>Clients</h1>
    </div>
    <?php if (Auth::can('clients.create')): ?>
        <a class="button-link" href="<?= url('/clients/create') ?>">Ajouter</a>
    <?php endif; ?>
</section>

<?php if (!empty($notice)): ?>
    <div class="alert alert-success"><?= e($notice) ?></div>
<?php endif; ?>

<form class="filter-panel client-filter" method="get" action="<?= url('/clients') ?>">
    <label>
        Recherche
        <input type="text" name="search" value="<?= e($search) ?>" placeholder="Nom, telephone, email, CIN">
    </label>
    <button type="submit">Filtrer</button>
</form>

<section class="table-panel">
    <table>
        <thead>
            <tr>
                <th>Client</th>
                <th>Telephone</th>
                <th>Email</th>
                <th>Identite</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($clients as $client): ?>
                <tr>
                    <td><?= e($client['first_name'] . ' ' . $client['last_name']) ?></td>
                    <td><?= e($client['phone'] ?: '-') ?></td>
                    <td><?= e($client['email'] ?: '-') ?></td>
                    <td><?= e($client['identity_number'] ?: '-') ?></td>
                    <td class="actions">
                        <a href="<?= url('/clients/show?id=' . $client['id']) ?>">Voir</a>
                        <?php if (Auth::can('clients.edit')): ?>
                            <a href="<?= url('/clients/edit?id=' . $client['id']) ?>">Modifier</a>
                        <?php endif; ?>
                        <?php if (Auth::can('clients.delete')): ?>
                            <form method="post" action="<?= url('/clients/delete') ?>" onsubmit="return confirm('Supprimer ce client ?');">
                                <input type="hidden" name="_token" value="<?= csrf_token() ?>">
                                <input type="hidden" name="id" value="<?= (int) $client['id'] ?>">
                                <button class="link-danger" type="submit">Supprimer</button>
                            </form>
                        <?php endif; ?>
                    </td>
                </tr>
            <?php endforeach; ?>
            <?php if (empty($clients)): ?>
                <tr>
                    <td colspan="5">Aucun client trouve.</td>
                </tr>
            <?php endif; ?>
        </tbody>
    </table>
</section>

<?php require dirname(__DIR__) . '/partials/pagination.php'; ?>

<?php require dirname(__DIR__) . '/layouts/app_footer.php'; ?>
