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

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

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

<section class="stat-grid compact-stats">
    <article class="stat-card">
        <span>Total paye</span>
        <strong><?= number_format($summary['paid'], 2, ',', ' ') ?></strong>
    </article>
    <article class="stat-card">
        <span>En attente</span>
        <strong><?= number_format($summary['pending'], 2, ',', ' ') ?></strong>
    </article>
</section>

<form class="filter-panel reservation-filter" method="get" action="<?= url('/payments') ?>">
    <label>
        Du
        <input type="date" name="from" value="<?= e($from) ?>">
    </label>
    <label>
        Au
        <input type="date" name="to" value="<?= e($to) ?>">
    </label>
    <label>
        Statut
        <select name="status">
            <option value="">Tous</option>
            <?php foreach (payment_statuses() as $key => $label): ?>
                <option value="<?= e($key) ?>" <?= $status === $key ? 'selected' : '' ?>><?= e($label) ?></option>
            <?php endforeach; ?>
        </select>
    </label>
    <button type="submit">Filtrer</button>
</form>

<section class="table-panel">
    <table>
        <thead>
            <tr>
                <th>Reservation</th>
                <th>Client</th>
                <th>Bien</th>
                <th>Montant</th>
                <th>Mode</th>
                <th>Statut</th>
                <th>Date</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($payments as $payment): ?>
                <tr>
                    <td>#<?= (int) $payment['reservation_id'] ?></td>
                    <td><?= e($payment['first_name'] . ' ' . $payment['last_name']) ?></td>
                    <td><?= e($payment['property_title']) ?></td>
                    <td><?= number_format((float) $payment['amount'], 2, ',', ' ') ?></td>
                    <td><?= e(payment_methods()[$payment['payment_method']] ?? $payment['payment_method']) ?></td>
                    <td>
                        <span class="status-pill payment-<?= e($payment['status']) ?>">
                            <?= e(payment_statuses()[$payment['status']] ?? $payment['status']) ?>
                        </span>
                    </td>
                    <td><?= e($payment['payment_date'] ?: '-') ?></td>
                    <td class="actions">
                        <a href="<?= url('/payments/show?id=' . $payment['id']) ?>">Voir</a>
                        <a href="<?= url('/payments/receipt?id=' . $payment['id']) ?>">Recu</a>
                        <?php if (Auth::can('payments.edit')): ?>
                            <a href="<?= url('/payments/edit?id=' . $payment['id']) ?>">Modifier</a>
                        <?php endif; ?>
                        <?php if (Auth::can('payments.delete')): ?>
                            <form method="post" action="<?= url('/payments/delete') ?>" onsubmit="return confirm('Supprimer ce paiement ?');">
                                <input type="hidden" name="_token" value="<?= csrf_token() ?>">
                                <input type="hidden" name="id" value="<?= (int) $payment['id'] ?>">
                                <button class="link-danger" type="submit">Supprimer</button>
                            </form>
                        <?php endif; ?>
                    </td>
                </tr>
            <?php endforeach; ?>
            <?php if (empty($payments)): ?>
                <tr>
                    <td colspan="8">Aucun paiement trouve.</td>
                </tr>
            <?php endif; ?>
        </tbody>
    </table>
</section>

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

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