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

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

<section class="page-heading">
    <div>
        <p class="eyebrow">Reservations</p>
        <h1><?= $isEdit ? 'Modifier reservation' : 'Ajouter reservation' ?></h1>
    </div>
    <a class="button-link secondary" href="<?= url('/reservations') ?>">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 ? '/reservations/update' : '/reservations/store') ?>">
    <input type="hidden" name="_token" value="<?= csrf_token() ?>">
    <?php if ($isEdit): ?>
        <input type="hidden" name="id" value="<?= (int) $reservation['id'] ?>">
    <?php endif; ?>

    <div class="form-grid">
        <label>
            Bien
            <select name="property_id" required>
                <option value="">Choisir</option>
                <?php foreach ($properties as $property): ?>
                    <option value="<?= (int) $property['id'] ?>" <?= (int) $reservation['property_id'] === (int) $property['id'] ? 'selected' : '' ?>>
                        <?= e($property['title']) ?> - <?= number_format((float) $property['price_per_night'], 2, ',', ' ') ?>/nuit
                    </option>
                <?php endforeach; ?>
            </select>
        </label>

        <label>
            Client
            <select name="client_id" required>
                <option value="">Choisir</option>
                <?php foreach ($clients as $client): ?>
                    <option value="<?= (int) $client['id'] ?>" <?= (int) $reservation['client_id'] === (int) $client['id'] ? 'selected' : '' ?>>
                        <?= e($client['first_name'] . ' ' . $client['last_name']) ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </label>

        <label>
            Source
            <select name="source">
                <?php foreach (reservation_sources() as $key => $label): ?>
                    <option value="<?= e($key) ?>" <?= ($reservation['source'] ?? 'private') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </label>

        <label>
            Agent responsable
            <select name="agent_id">
                <option value="">Aucun</option>
                <?php foreach ($agents as $agent): ?>
                    <option value="<?= (int) $agent['id'] ?>" <?= (int) ($reservation['agent_id'] ?? 0) === (int) $agent['id'] ? 'selected' : '' ?>>
                        <?= e($agent['name']) ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </label>

        <label>
            Arrivee
            <input type="date" name="start_date" value="<?= e($reservation['start_date']) ?>" required>
        </label>

        <label>
            Depart
            <input type="date" name="end_date" value="<?= e($reservation['end_date']) ?>" required>
        </label>

        <label>
            Heure arrivee
            <input type="time" name="checkin_time" value="<?= e(normalize_time_value($reservation['checkin_time'] ?? '')) ?>">
        </label>

        <label>
            Heure depart
            <input type="time" name="checkout_time" value="<?= e(normalize_time_value($reservation['checkout_time'] ?? '')) ?>">
        </label>

        <label>
            Remise accordee
            <input type="number" name="discount_amount" min="0" step="0.01" value="<?= e((string) ($reservation['discount_amount'] ?? 0)) ?>">
        </label>

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

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

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

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