<?php
/**
 * Admin view – Wartungsmodus
 *
 * Variables:
 *   bool   $isActive       – user-controlled maintenance mode active?
 *   array  $config         – current config from storage/.maintenance_mode
 *   bool   $backupFlagSet  – backup/restore .maintenance flag active?
 *   object $auth
 *   string $csrf
 */
ob_start(); ?>

<div class="admin-section">

    <!-- ── Status Card ─────────────────────────────────────────────────────── -->
    <div class="card mb-4">
        <div class="card-header">
            <h2 class="card-title"><?= __('admin.maintenance.status') ?></h2>
        </div>
        <div class="card-body">
            <?php if ($isActive): ?>
            <div class="alert alert-warning" role="status">
                <strong><?= __('admin.maintenance.active_warning') ?></strong>
                <?= __('admin.maintenance.active_text') ?>
            </div>
            <?php if (!empty($config['activated_at'])): ?>
            <p class="text-muted" style="font-size:.875rem;margin-top:.5rem;">
                <?= __('admin.maintenance.activated_at', ['date' => e($config['activated_at'])]) ?>
            </p>
            <?php endif; ?>
            <?php else: ?>
            <div class="alert alert-success" role="status">
                <strong><?= __('admin.maintenance.inactive_ok') ?></strong>
                <?= __('admin.maintenance.inactive_text') ?>
            </div>
            <?php endif; ?>

            <?php if ($backupFlagSet): ?>
            <div class="alert alert-warning mt-2" role="status" style="margin-top:1rem;">
                <strong><?= __('admin.maintenance.backup_flag') ?></strong> –
                <code>storage/.maintenance</code> wurde von BackupService gesetzt (läuft gerade eine Wiederherstellung oder ein Update?).
                Dieser Zustand wird automatisch nach Abschluss der Operation aufgehoben.
            </div>
            <?php endif; ?>
        </div>
    </div>

    <!-- ── Enable Form ─────────────────────────────────────────────────────── -->
    <?php if (!$isActive): ?>
    <div class="card mb-4">
        <div class="card-header">
            <h2 class="card-title"><?= __('admin.maintenance.enable_title') ?></h2>
        </div>
        <div class="card-body">
            <p style="color:#6b7280;margin-bottom:1.25rem;">
                <?= __('admin.maintenance.enable_text') ?>
            </p>
            <form method="POST" action="/admin/maintenance/enable">
                <?= $csrf->field() ?>

                <div class="form-group">
                    <label for="maintenance_title"><?= __('admin.maintenance.page_title_label') ?></label>
                    <input type="text"
                           id="maintenance_title"
                           name="maintenance_title"
                           class="form-control"
                           value="<?= __('admin.maintenance.page_title_default') ?>"
                           maxlength="120"
                           placeholder="<?= __('admin.maintenance.page_title_default') ?>">
                    <p class="form-help">Wird als Überschrift auf der Wartungsseite angezeigt.</p>
                </div>

                <div class="form-group">
                    <label for="maintenance_message"><?= __('admin.maintenance.message_label') ?></label>
                    <textarea id="maintenance_message"
                              name="maintenance_message"
                              class="form-control"
                              rows="3"
                              maxlength="500"
                              placeholder="<?= __('admin.maintenance.message_default') ?>"><?= __('admin.maintenance.message_default') ?></textarea>
                    <p class="form-help">Kurze Info für Besucher (max. 500 Zeichen).</p>
                </div>

                <button type="submit" class="btn btn-warning"
                        onclick="return confirm('Wartungsmodus aktivieren? Das Frontend wird sofort gesperrt.')">
                    <?= __('admin.maintenance.enable_btn') ?>
                </button>
            </form>
        </div>
    </div>
    <?php endif; ?>

    <!-- ── Disable Form ────────────────────────────────────────────────────── -->
    <?php if ($isActive): ?>
    <div class="card mb-4">
        <div class="card-header">
            <h2 class="card-title"><?= __('admin.maintenance.current_config') ?></h2>
        </div>
        <div class="card-body">
            <table class="table" style="margin-bottom:1.5rem;">
                <tr>
                    <th style="width:160px;"><?= __('admin.maintenance.config_title') ?></th>
                    <td><?= e($config['title'] ?? '–') ?></td>
                </tr>
                <tr>
                    <th><?= __('admin.maintenance.config_message') ?></th>
                    <td><?= e($config['message'] ?? '–') ?></td>
                </tr>
                <tr>
                    <th><?= __('admin.maintenance.config_site') ?></th>
                    <td><?= e($config['site_name'] ?? '–') ?></td>
                </tr>
            </table>

            <form method="POST" action="/admin/maintenance/disable">
                <?= $csrf->field() ?>
                <button type="submit" class="btn btn-primary"
                        onclick="return confirm('Wartungsmodus deaktivieren? Die Website wird sofort wieder öffentlich.')">
                    <?= __('admin.maintenance.disable_btn') ?>
                </button>
            </form>
        </div>
    </div>

    <!-- Update title/message while active -->
    <div class="card">
        <div class="card-header">
            <h2 class="card-title"><?= __('admin.maintenance.update_config') ?></h2>
        </div>
        <div class="card-body">
            <p style="color:#6b7280;margin-bottom:1.25rem;">
                <?= __('admin.maintenance.update_hint') ?>
            </p>
            <form method="POST" action="/admin/maintenance/enable">
                <?= $csrf->field() ?>

                <div class="form-group">
                    <label for="maintenance_title_edit"><?= __('admin.maintenance.config_title') ?></label>
                    <input type="text"
                           id="maintenance_title_edit"
                           name="maintenance_title"
                           class="form-control"
                           value="<?= e($config['title'] ?? __('admin.maintenance.page_title_default')) ?>"
                           maxlength="120">
                </div>

                <div class="form-group">
                    <label for="maintenance_message_edit"><?= __('admin.maintenance.config_message') ?></label>
                    <textarea id="maintenance_message_edit"
                              name="maintenance_message"
                              class="form-control"
                              rows="3"
                              maxlength="500"><?= e($config['message'] ?? '') ?></textarea>
                </div>

                <button type="submit" class="btn btn-secondary"><?= __('admin.maintenance.update_btn') ?></button>
            </form>
        </div>
    </div>
    <?php endif; ?>

</div>

<?php $content = ob_get_clean(); include dirname(__DIR__) . '/layouts/admin.php'; ?>
