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

<section class="page-heading">
    <div>
        <p class="eyebrow">Configuration</p>
        <h1>Parametres application</h1>
    </div>
</section>

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

<?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('/settings/update') ?>">
    <input type="hidden" name="_token" value="<?= csrf_token() ?>">
    <input type="hidden" name="booking_sync_token" value="<?= e($settings['booking_sync_token'] ?? '') ?>">

    <div class="form-grid">
        <label>
            Nom societe
            <input type="text" name="company_name" value="<?= e($settings['company_name']) ?>" required>
        </label>

        <label>
            Telephone
            <input type="text" name="company_phone" value="<?= e($settings['company_phone']) ?>">
        </label>

        <label>
            Email
            <input type="email" name="company_email" value="<?= e($settings['company_email']) ?>">
        </label>

        <label>
            Ville
            <input type="text" name="company_city" value="<?= e($settings['company_city']) ?>">
        </label>
    </div>

    <label class="full-field">
        Adresse
        <input type="text" name="company_address" value="<?= e($settings['company_address']) ?>">
    </label>

    <section class="permissions-panel">
        <h2>Parametres administrateur</h2>
        <div class="form-grid">
            <label>
                Nom administrateur
                <input type="text" name="admin_name" value="<?= e($adminAccount['name'] ?? '') ?>" required>
            </label>

            <label>
                Email administrateur
                <input type="email" name="admin_email" value="<?= e($adminAccount['email'] ?? '') ?>" required>
            </label>

            <label>
                Mot de passe actuel
                <input type="password" name="admin_current_password">
            </label>

            <label>
                Nouveau mot de passe
                <input type="password" name="admin_new_password">
            </label>

            <label>
                Confirmer le nouveau mot de passe
                <input type="password" name="admin_new_password_confirmation">
            </label>
        </div>
    </section>

    <div class="form-actions">
        <button type="submit">Enregistrer</button>
    </div>
</form>

<section class="form-panel">
    <div>
        <p class="eyebrow">Booking.com / Airbnb</p>
        <h2>Synchronisation automatique</h2>
        <p class="hint">Collez les liens iCal Booking et Airbnb de chaque bien. Le bouton Sync importe les nouvelles reservations; le lien cron peut etre appele automatiquement par cPanel.</p>
    </div>

    <form method="post" action="<?= url('/booking-sync/settings') ?>">
        <input type="hidden" name="_token" value="<?= csrf_token() ?>">

        <?php if (empty($bookingFeeds)): ?>
            <p class="hint">Ajoutez d'abord des biens pour configurer Booking/Airbnb.</p>
        <?php else: ?>
            <div class="form-grid">
                <?php foreach ($bookingFeeds as $feed): ?>
                    <?php foreach ($channelSources as $sourceKey => $sourceLabel): ?>
                        <?php $sourceFeed = $feed['sources'][$sourceKey] ?? ['feed_url' => '', 'is_active' => 1, 'last_synced_at' => '', 'last_status' => 'never', 'last_message' => '']; ?>
                        <label>
                            <?= e($feed['title']) ?> - <?= e($sourceLabel) ?>
                            <input type="url" name="feed_url[<?= e($sourceKey) ?>][<?= (int) $feed['property_id'] ?>]" value="<?= e($sourceFeed['feed_url'] ?? '') ?>" placeholder="https://.../calendar.ics">
                            <span class="hint">
                                <input type="checkbox" name="is_active[<?= e($sourceKey) ?>][<?= (int) $feed['property_id'] ?>]" <?= ((int) ($sourceFeed['is_active'] ?? 1) === 1) ? 'checked' : '' ?>>
                                Actif
                                <?php if (!empty($sourceFeed['last_synced_at'])): ?>
                                    - Dernier sync: <?= e($sourceFeed['last_synced_at']) ?> (<?= e($sourceFeed['last_status'] ?? 'never') ?>)
                                <?php endif; ?>
                            </span>
                            <?php if (!empty($sourceFeed['last_message'])): ?>
                                <span class="hint"><?= e($sourceFeed['last_message']) ?></span>
                            <?php endif; ?>
                        </label>
                    <?php endforeach; ?>
                <?php endforeach; ?>
            </div>

            <label class="full-field">
                URL cron automatique
                <input type="text" readonly value="<?= e($bookingCronUrl ?? '') ?>">
            </label>

            <div class="form-actions">
                <button type="submit">Enregistrer Booking/Airbnb</button>
            </div>
        <?php endif; ?>
    </form>

    <?php if (!empty($bookingFeeds)): ?>
        <form method="post" action="<?= url('/booking-sync/run') ?>">
            <input type="hidden" name="_token" value="<?= csrf_token() ?>">
            <div class="form-actions">
                <button class="secondary-button" type="submit">Sync Booking/Airbnb maintenant</button>
            </div>
        </form>
    <?php endif; ?>
</section>

<?php if (($user['role'] ?? '') === 'admin'): ?>
    <section class="form-panel danger-zone">
        <div>
            <p class="eyebrow">Administration</p>
            <h2>Reset data</h2>
            <p class="hint">Cette action supprime les donnees de gestion: biens, photos, clients, reservations, paiements, depenses, location longue duree, notifications et historique. Le compte admin et les parametres restent conserves.</p>
        </div>
        <form method="post" action="<?= url('/settings/reset-data') ?>" onsubmit="return confirm('Confirmer reset data ? Cette action est irreversible.');">
            <input type="hidden" name="_token" value="<?= csrf_token() ?>">
            <label>
                Confirmation
                <input name="reset_confirmation" placeholder="Tapez RESET" required>
            </label>
            <div class="form-actions">
                <button class="danger-button" type="submit">Reset data</button>
            </div>
        </form>
    </section>
<?php endif; ?>

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