<?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="content-header">
    <h2><?= __('admin.maintenance.title') ?></h2>
</div>

<div class="admin-section">

    <!-- ── Status Card ─────────────────────────────────────────────────────── -->
    <div class="card mb-4">
        <div class="card-header">
            <h3 class="card-title"><?= __('admin.maintenance.status') ?></h3>
        </div>
        <div class="card-body">
            <?php if ($isActive): ?>
            <div class="alert alert-warning" role="status" style="display:flex;align-items:center;gap:.75rem;">
                <span style="font-size:1.25rem;">⚠</span>
                <div>
                    <strong><?= __('admin.maintenance.active_warning') ?></strong><br>
                    <span style="font-size:.9rem;"><?= __('admin.maintenance.active_text') ?></span>
                </div>
            </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" style="display:flex;align-items:center;gap:.75rem;">
                <span style="font-size:1.25rem;">✓</span>
                <div>
                    <strong><?= __('admin.maintenance.inactive_ok') ?></strong><br>
                    <span style="font-size:.9rem;"><?= __('admin.maintenance.inactive_text') ?></span>
                </div>
            </div>
            <?php endif; ?>

            <?php if ($backupFlagSet): ?>
            <div class="alert alert-warning" role="status" style="margin-top:1rem;display:flex;align-items:flex-start;gap:.75rem;">
                <span style="font-size:1.25rem;">ℹ</span>
                <div>
                    <strong><?= __('admin.maintenance.backup_flag') ?></strong> –
                    <code>storage/.maintenance</code> <?= __('admin.maintenance.backup_flag_description') ?>
                </div>
            </div>
            <?php endif; ?>
        </div>
    </div>

    <!-- ── Enable / Configure Form ──────────────────────────────────────────── -->
    <?php if (!$isActive): ?>
    <div class="card mb-4">
        <div class="card-header">
            <h3 class="card-title"><?= __('admin.maintenance.enable_title') ?></h3>
        </div>
        <div class="card-body">
            <p class="text-muted" style="margin-bottom:1.5rem;font-size:.9rem;">
                <?= __('admin.maintenance.enable_text') ?>
            </p>
            <form method="POST" action="/admin/maintenance/enable" id="maintenanceForm">
                <?= $csrf->field() ?>

                <fieldset class="maintenance-fieldset">
                    <legend><?= __('admin.maintenance.config_title') ?> & <?= __('admin.maintenance.config_message') ?></legend>

                    <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"><?= __('admin.maintenance.title_help') ?></p>
                    </div>

                    <div class="form-group">
                        <label for="maintenance_subtitle"><?= __('admin.maintenance.subtitle_label') ?></label>
                        <input type="text"
                               id="maintenance_subtitle"
                               name="maintenance_subtitle"
                               class="form-control"
                               maxlength="200"
                               placeholder="<?= __('admin.maintenance.subtitle_placeholder') ?>">
                        <p class="form-help"><?= __('admin.maintenance.subtitle_help') ?></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"><?= __('admin.maintenance.message_help') ?></p>
                    </div>
                </fieldset>

                <fieldset class="maintenance-fieldset">
                    <legend><?= __('admin.maintenance.contact_info_label') ?> & <?= __('admin.maintenance.return_info_label') ?></legend>

                    <div class="form-row">
                        <div class="form-group form-group--half">
                            <label for="maintenance_contact_info"><?= __('admin.maintenance.contact_info_label') ?></label>
                            <input type="text"
                                   id="maintenance_contact_info"
                                   name="maintenance_contact_info"
                                   class="form-control"
                                   maxlength="150"
                                   placeholder="<?= __('admin.maintenance.contact_info_placeholder') ?>">
                            <p class="form-help"><?= __('admin.maintenance.contact_info_help') ?></p>
                        </div>

                        <div class="form-group form-group--half">
                            <label for="maintenance_return_info"><?= __('admin.maintenance.return_info_label') ?></label>
                            <input type="text"
                                   id="maintenance_return_info"
                                   name="maintenance_return_info"
                                   class="form-control"
                                   maxlength="100"
                                   placeholder="<?= __('admin.maintenance.return_info_placeholder') ?>">
                            <p class="form-help"><?= __('admin.maintenance.return_info_help') ?></p>
                        </div>
                    </div>
                </fieldset>

                <fieldset class="maintenance-fieldset">
                    <legend><?= __('admin.common.settings') ?? 'Optionen' ?></legend>

                    <div class="maintenance-checkboxes">
                        <label class="maintenance-checkbox">
                            <input type="checkbox"
                                   id="maintenance_show_logo"
                                   name="maintenance_show_logo"
                                   value="1">
                            <span class="maintenance-checkbox__text">
                                <strong><?= __('admin.maintenance.show_logo_label') ?></strong>
                                <small><?= __('admin.maintenance.show_logo_help') ?></small>
                            </span>
                        </label>

                        <label class="maintenance-checkbox">
                            <input type="checkbox"
                                   id="maintenance_admin_hint"
                                   name="maintenance_admin_hint"
                                   value="1"
                                   checked="checked">
                            <span class="maintenance-checkbox__text">
                                <strong><?= __('admin.maintenance.admin_hint_label') ?></strong>
                                <small><?= __('admin.maintenance.admin_hint_help') ?></small>
                            </span>
                        </label>
                    </div>
                </fieldset>

                <div class="maintenance-actions">
                    <button type="button" class="btn btn-secondary" onclick="showMaintenancePreview()">
                        👁 <?= __('admin.maintenance.preview_btn') ?>
                    </button>
                    <button type="submit" class="btn btn-warning"
                            onclick="return confirm('<?= __('admin.maintenance.confirm_enable') ?>')">
                        <?= __('admin.maintenance.enable_btn') ?>
                    </button>
                </div>
            </form>
        </div>
    </div>
    <?php endif; ?>

    <!-- ── Active: Config + Disable ─────────────────────────────────────────── -->
    <?php if ($isActive): ?>
    <div class="card mb-4">
        <div class="card-header" style="display:flex;justify-content:space-between;align-items:center;">
            <h3 class="card-title"><?= __('admin.maintenance.current_config') ?></h3>
            <div style="display:flex;gap:.5rem;">
                <a href="/" target="_blank" class="btn btn-secondary btn-sm">
                    👁 <?= __('admin.maintenance.preview_btn') ?>
                </a>
                <form method="POST" action="/admin/maintenance/disable" style="margin:0;">
                    <?= $csrf->field() ?>
                    <button type="submit" class="btn btn-primary btn-sm"
                            onclick="return confirm('<?= __('admin.maintenance.confirm_disable') ?>')">
                        <?= __('admin.maintenance.disable_btn') ?>
                    </button>
                </form>
            </div>
        </div>
        <div class="card-body">
            <table class="table maintenance-config-table">
                <tr>
                    <th><?= __('admin.maintenance.config_title') ?></th>
                    <td><?= e($config['title'] ?? '–') ?></td>
                </tr>
                <?php if (!empty($config['subtitle'])): ?>
                <tr>
                    <th><?= __('admin.maintenance.subtitle_label') ?></th>
                    <td><?= e($config['subtitle']) ?></td>
                </tr>
                <?php endif; ?>
                <tr>
                    <th><?= __('admin.maintenance.config_message') ?></th>
                    <td><?= e($config['message'] ?? '–') ?></td>
                </tr>
                <?php if (!empty($config['contact_info'])): ?>
                <tr>
                    <th><?= __('admin.maintenance.contact_info_label') ?></th>
                    <td><?= e($config['contact_info']) ?></td>
                </tr>
                <?php endif; ?>
                <?php if (!empty($config['return_info'])): ?>
                <tr>
                    <th><?= __('admin.maintenance.return_info_label') ?></th>
                    <td><?= e($config['return_info']) ?></td>
                </tr>
                <?php endif; ?>
                <tr>
                    <th><?= __('admin.maintenance.config_site') ?></th>
                    <td><?= e($config['site_name'] ?? '–') ?></td>
                </tr>
            </table>
        </div>
    </div>

    <!-- Update configuration while active -->
    <div class="card">
        <div class="card-header">
            <h3 class="card-title"><?= __('admin.maintenance.update_config') ?></h3>
        </div>
        <div class="card-body">
            <p class="text-muted" style="margin-bottom:1.25rem;font-size:.9rem;">
                <?= __('admin.maintenance.update_hint') ?>
            </p>
            <form method="POST" action="/admin/maintenance/enable" id="maintenanceForm">
                <?= $csrf->field() ?>

                <fieldset class="maintenance-fieldset">
                    <legend><?= __('admin.maintenance.config_title') ?> & <?= __('admin.maintenance.config_message') ?></legend>

                    <div class="form-group">
                        <label for="maintenance_title_edit"><?= __('admin.maintenance.page_title_label') ?></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_subtitle_edit"><?= __('admin.maintenance.subtitle_label') ?></label>
                        <input type="text"
                               id="maintenance_subtitle_edit"
                               name="maintenance_subtitle"
                               class="form-control"
                               value="<?= e($config['subtitle'] ?? '') ?>"
                               maxlength="200">
                    </div>

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

                <fieldset class="maintenance-fieldset">
                    <legend><?= __('admin.maintenance.contact_info_label') ?> & <?= __('admin.maintenance.return_info_label') ?></legend>

                    <div class="form-row">
                        <div class="form-group form-group--half">
                            <label for="maintenance_contact_info_edit"><?= __('admin.maintenance.contact_info_label') ?></label>
                            <input type="text"
                                   id="maintenance_contact_info_edit"
                                   name="maintenance_contact_info"
                                   class="form-control"
                                   value="<?= e($config['contact_info'] ?? '') ?>"
                                   maxlength="150">
                        </div>

                        <div class="form-group form-group--half">
                            <label for="maintenance_return_info_edit"><?= __('admin.maintenance.return_info_label') ?></label>
                            <input type="text"
                                   id="maintenance_return_info_edit"
                                   name="maintenance_return_info"
                                   class="form-control"
                                   value="<?= e($config['return_info'] ?? '') ?>"
                                   maxlength="100">
                        </div>
                    </div>
                </fieldset>

                <fieldset class="maintenance-fieldset">
                    <legend><?= __('admin.common.settings') ?? 'Optionen' ?></legend>
                    <div class="maintenance-checkboxes">
                        <label class="maintenance-checkbox">
                            <input type="checkbox"
                                   id="maintenance_show_logo_edit"
                                   name="maintenance_show_logo"
                                   value="1"
                                   <?php if ($config['show_logo'] ?? false): ?>checked="checked"<?php endif; ?>>
                            <span class="maintenance-checkbox__text">
                                <strong><?= __('admin.maintenance.show_logo_label') ?></strong>
                            </span>
                        </label>

                        <label class="maintenance-checkbox">
                            <input type="checkbox"
                                   id="maintenance_admin_hint_edit"
                                   name="maintenance_admin_hint"
                                   value="1"
                                   <?php if ($config['admin_hint'] ?? true): ?>checked="checked"<?php endif; ?>>
                            <span class="maintenance-checkbox__text">
                                <strong><?= __('admin.maintenance.admin_hint_label') ?></strong>
                            </span>
                        </label>
                    </div>
                </fieldset>

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

</div>

<!-- Preview Modal -->
<div class="maintenance-preview-modal" id="maintenancePreviewModal" style="display:none;">
    <div class="maintenance-preview-modal__backdrop" onclick="closeMaintenancePreview()"></div>
    <div class="maintenance-preview-modal__container">
        <div class="maintenance-preview-modal__header">
            <strong><?= __('admin.maintenance.preview_btn') ?></strong>
            <button type="button" onclick="closeMaintenancePreview()" class="maintenance-preview-modal__close">&times;</button>
        </div>
        <div class="maintenance-preview-modal__body">
            <iframe id="maintenancePreviewFrame" class="maintenance-preview-modal__frame"></iframe>
        </div>
    </div>
</div>

<style>
/* Maintenance admin page styles */
.maintenance-fieldset {
    border: 1px solid var(--border-color, #e2e8f0);
    border-radius: 8px;
    padding: 1.25rem 1.25rem 0.75rem;
    margin-bottom: 1.25rem;
    background: #fafbfc;
}
.maintenance-fieldset legend {
    font-size: .85rem;
    font-weight: 600;
    color: #4a5568;
    padding: 0 .5rem;
    text-transform: uppercase;
    letter-spacing: .03em;
}

.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
}
@media (max-width: 640px) {
    .form-row { grid-template-columns: 1fr; }
}

.maintenance-checkboxes {
    display: flex;
    flex-direction: column;
    gap: .75rem;
}
.maintenance-checkbox {
    display: flex;
    align-items: flex-start;
    gap: .65rem;
    cursor: pointer;
    padding: .5rem .75rem;
    border-radius: 6px;
    transition: background .15s;
}
.maintenance-checkbox:hover { background: #f0f4f8; }
.maintenance-checkbox input[type="checkbox"] {
    width: 18px;
    height: 18px;
    cursor: pointer;
    flex-shrink: 0;
    margin-top: 2px;
}
.maintenance-checkbox__text {
    display: flex;
    flex-direction: column;
    gap: .15rem;
}
.maintenance-checkbox__text strong { font-size: .9rem; }
.maintenance-checkbox__text small { font-size: .8rem; color: #6b7280; }

.maintenance-actions {
    display: flex;
    gap: .75rem;
    margin-top: 1rem;
    padding-top: 1rem;
    border-top: 1px solid var(--border-color, #e2e8f0);
}

.maintenance-config-table th {
    width: 180px;
    font-weight: 600;
    color: #4a5568;
    font-size: .875rem;
}
.maintenance-config-table td {
    font-size: .875rem;
}

/* Preview Modal */
.maintenance-preview-modal {
    position: fixed;
    inset: 0;
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: center;
}
.maintenance-preview-modal__backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,.5);
}
.maintenance-preview-modal__container {
    position: relative;
    background: #fff;
    border-radius: 12px;
    width: 90%;
    max-width: 800px;
    height: 80vh;
    display: flex;
    flex-direction: column;
    box-shadow: 0 20px 60px rgba(0,0,0,.3);
    overflow: hidden;
}
.maintenance-preview-modal__header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: .75rem 1rem;
    border-bottom: 1px solid #e2e8f0;
    background: #f8fafc;
}
.maintenance-preview-modal__close {
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: #64748b;
    padding: 0 .25rem;
    line-height: 1;
}
.maintenance-preview-modal__close:hover { color: #1a202c; }
.maintenance-preview-modal__body { flex: 1; }
.maintenance-preview-modal__frame {
    width: 100%;
    height: 100%;
    border: none;
}
</style>

<script>
function showMaintenancePreview() {
    var form = document.getElementById('maintenanceForm');
    if (!form) return;

    // Build preview URL with form data
    var params = new URLSearchParams();
    params.set('preview', '1');

    var titleEl = form.querySelector('[name="maintenance_title"]');
    var subtitleEl = form.querySelector('[name="maintenance_subtitle"]');
    var messageEl = form.querySelector('[name="maintenance_message"]');
    var contactEl = form.querySelector('[name="maintenance_contact_info"]');
    var returnEl = form.querySelector('[name="maintenance_return_info"]');
    var logoEl = form.querySelector('[name="maintenance_show_logo"]');
    var hintEl = form.querySelector('[name="maintenance_admin_hint"]');

    if (titleEl) params.set('title', titleEl.value);
    if (subtitleEl) params.set('subtitle', subtitleEl.value);
    if (messageEl) params.set('message', messageEl.value);
    if (contactEl) params.set('contact_info', contactEl.value);
    if (returnEl) params.set('return_info', returnEl.value);
    if (logoEl && logoEl.checked) params.set('show_logo', '1');
    if (hintEl && hintEl.checked) params.set('admin_hint', '1');

    var modal = document.getElementById('maintenancePreviewModal');
    var frame = document.getElementById('maintenancePreviewFrame');
    frame.src = '/admin/maintenance/preview?' + params.toString();
    modal.style.display = 'flex';
}

function closeMaintenancePreview() {
    var modal = document.getElementById('maintenancePreviewModal');
    modal.style.display = 'none';
    var frame = document.getElementById('maintenancePreviewFrame');
    frame.src = 'about:blank';
}

document.addEventListener('keydown', function(e) {
    if (e.key === 'Escape') closeMaintenancePreview();
});
</script>

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