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

<?php $isEdit = !empty($payment['id']); ?>

<section class="page-heading">
    <div>
        <p class="eyebrow">Paiements</p>
        <h1><?= $isEdit ? 'Modifier paiement' : 'Ajouter paiement' ?></h1>
    </div>
    <a class="button-link secondary" href="<?= url('/payments') ?>">Retour</a>
</section>

<?php if (!empty($errors)): ?>
    <div class="alert alert-error">
        <?php foreach ($errors as $error): ?>
            <div><?= e($error) ?></div>
        <?php endforeach; ?>
    </div>
<?php endif; ?>

<form class="form-panel" method="post" action="<?= url($isEdit ? '/payments/update' : '/payments/store') ?>">
    <input type="hidden" name="_token" value="<?= csrf_token() ?>">
    <?php if ($isEdit): ?>
        <input type="hidden" name="id" value="<?= (int) $payment['id'] ?>">
    <?php endif; ?>

    <div class="form-grid">
        <label>
            Reservation
            <select name="reservation_id" required>
                <option value="">Choisir</option>
                <?php foreach ($reservations as $reservation): ?>
                    <option value="<?= (int) $reservation['id'] ?>" <?= (int) $payment['reservation_id'] === (int) $reservation['id'] ? 'selected' : '' ?>>
                        #<?= (int) $reservation['id'] ?> - <?= e($reservation['property_title']) ?> - <?= e($reservation['first_name'] . ' ' . $reservation['last_name']) ?> - <?= number_format((float) $reservation['total_amount'], 2, ',', ' ') ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </label>

        <label>
            Montant
            <input type="number" name="amount" min="0" step="0.01" value="<?= e((string) $payment['amount']) ?>" required>
        </label>

        <label>
            Mode de paiement
            <select name="payment_method">
                <?php foreach (payment_methods() as $key => $label): ?>
                    <option value="<?= e($key) ?>" <?= $payment['payment_method'] === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </label>

        <label>
            Statut
            <select name="status">
                <?php foreach (payment_statuses() as $key => $label): ?>
                    <option value="<?= e($key) ?>" <?= $payment['status'] === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </label>

        <label>
            Date paiement
            <input type="date" name="payment_date" value="<?= e($payment['payment_date']) ?>">
        </label>
    </div>

    <label class="full-field">
        Notes
        <textarea name="notes" rows="5"><?= e($payment['notes']) ?></textarea>
    </label>

    <div class="form-actions">
        <button type="submit"><?= $isEdit ? 'Enregistrer' : 'Ajouter' ?></button>
    </div>
</form>

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