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

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

<section class="page-heading">
    <div>
        <p class="eyebrow">Biens immobiliers</p>
        <h1><?= $isEdit ? 'Modifier bien' : 'Ajouter bien' ?></h1>
    </div>
    <a class="button-link secondary" href="<?= url('/properties') ?>">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" enctype="multipart/form-data" action="<?= url($isEdit ? '/properties/update' : '/properties/store') ?>">
    <input type="hidden" name="_token" value="<?= csrf_token() ?>">
    <?php if ($isEdit): ?>
        <input type="hidden" name="id" value="<?= (int) $property['id'] ?>">
    <?php endif; ?>

    <div class="form-grid">
        <label>
            Titre
            <input type="text" name="title" value="<?= e($property['title']) ?>" required>
        </label>

        <label>
            Type
            <select name="type">
                <?php foreach (property_types() as $key => $label): ?>
                    <option value="<?= e($key) ?>" <?= $property['type'] === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </label>

        <label>
            Ville
            <input type="text" name="city" value="<?= e($property['city']) ?>" required>
        </label>

        <label>
            Quartier
            <input type="text" name="district" value="<?= e($property['district']) ?>">
        </label>

        <label>
            Adresse
            <input type="text" name="address" value="<?= e($property['address']) ?>">
        </label>

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

        <label>
            Prix par nuit
            <input type="number" name="price_per_night" min="0" step="0.01" value="<?= e((string) $property['price_per_night']) ?>">
        </label>

        <label>
            Chambres
            <input type="number" name="bedrooms" min="0" value="<?= e((string) $property['bedrooms']) ?>">
        </label>

        <label>
            Salles de bain
            <input type="number" name="bathrooms" min="0" value="<?= e((string) $property['bathrooms']) ?>">
        </label>

        <label>
            Capacite
            <input type="number" name="max_guests" min="1" value="<?= e((string) $property['max_guests']) ?>">
        </label>
    </div>

    <label class="full-field">
        Description
        <textarea name="description" rows="5"><?= e($property['description']) ?></textarea>
    </label>

    <label class="full-field">
        Photos
        <input type="file" name="photos[]" accept="image/jpeg,image/png,image/webp" multiple>
    </label>

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

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