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

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

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

<form class="filter-panel" method="get" action="<?= url('/properties') ?>">
    <label>
        Ville
        <input type="text" name="city" value="<?= e($city) ?>" placeholder="Casablanca">
    </label>
    <label>
        Quartier
        <input type="text" name="district" value="<?= e($district) ?>" placeholder="Maarif">
    </label>
    <label>
        Emplacement
        <input type="text" name="address" value="<?= e($address) ?>" placeholder="Adresse">
    </label>
    <label>
        Type
        <select name="type">
            <option value="">Tous</option>
            <?php foreach (property_types() as $key => $label): ?>
                <option value="<?= e($key) ?>" <?= $type === $key ? 'selected' : '' ?>><?= e($label) ?></option>
            <?php endforeach; ?>
        </select>
    </label>
    <label>
        Statut
        <select name="status">
            <option value="">Tous</option>
            <?php foreach (property_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="property-grid">
    <?php foreach ($properties as $property): ?>
        <article class="property-card">
            <div class="property-photo">
                <?php if (!empty($property['photo_path'])): ?>
                    <img src="<?= url('/' . $property['photo_path']) ?>" alt="<?= e($property['title']) ?>">
                <?php else: ?>
                    <span><?= e(substr($property['title'], 0, 1)) ?></span>
                <?php endif; ?>
            </div>
            <div class="property-body">
                <div class="property-title-row">
                    <h2><?= e($property['title']) ?></h2>
                    <span class="status-pill status-<?= e($property['status']) ?>">
                        <?= e(property_statuses()[$property['status']] ?? $property['status']) ?>
                    </span>
                </div>
                <p><?= e(property_types()[$property['type']] ?? $property['type']) ?> - <?= e($property['city']) ?><?= $property['district'] ? ', ' . e($property['district']) : '' ?></p>
                <strong><?= number_format((float) $property['price_per_night'], 2, ',', ' ') ?> / nuit</strong>
                <div class="actions">
                    <a href="<?= url('/properties/show?id=' . $property['id']) ?>">Voir</a>
                    <?php if (Auth::can('properties.edit')): ?>
                        <a href="<?= url('/properties/edit?id=' . $property['id']) ?>">Modifier</a>
                    <?php endif; ?>
                    <?php if (Auth::can('properties.delete')): ?>
                        <form method="post" action="<?= url('/properties/delete') ?>" onsubmit="return confirm('Supprimer ce bien ?');">
                            <input type="hidden" name="_token" value="<?= csrf_token() ?>">
                            <input type="hidden" name="id" value="<?= (int) $property['id'] ?>">
                            <button class="link-danger" type="submit">Supprimer</button>
                        </form>
                    <?php endif; ?>
                </div>
            </div>
        </article>
    <?php endforeach; ?>

    <?php if (empty($properties)): ?>
        <article class="empty-state">Aucun bien trouve.</article>
    <?php endif; ?>
</section>

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

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