<?php
use function App\Core\e;
$section = (string) ($section ?? 'sayfa-ekle');
$page = $page ?? null;
$isEdit = !empty($page['id']);
?>
<div class="panel zpanel zpanel-app">
    <?php require BASE_PATH . '/app/Views/zpanel/_sidebar.php'; ?>
    <section class="panel-body zpanel-main" data-zpanel-page>
        <div class="zpanel-head">
            <div><p class="eyebrow">Sayfa Yonetimi</p><h1><?= $isEdit ? 'Sayfa Duzenle' : 'Sayfa Ekle' ?></h1><p class="muted">Icerik, SEO ve sayfa gorunurluk seceneklerini tek ekrandan duzenle.</p></div>
            <div class="zpanel-head-actions">
                <?php if ($isEdit && ($page['status'] ?? '') === 'published'): ?><a class="pill" href="/sayfa/<?= e($page['slug'] ?? '') ?>" target="_blank" rel="noopener noreferrer">Goruntule</a><?php endif; ?>
                <a class="pill" href="/zpanel/sayfalar">Listeye don</a>
            </div>
        </div>
        <?php if (!empty($loadError)): ?><div class="zpanel-alert"><?= e($loadError) ?></div><?php endif; ?>
        <section class="zpanel-card">
            <form class="zpanel-form page-editor" data-zpanel-form action="<?= $isEdit ? '/api/zpanel/pages/update' : '/api/zpanel/pages/create' ?>" method="post" data-redirect-on-success="true">
                <input type="hidden" name="_csrf" value="<?= e($csrfToken ?? '') ?>">
                <?php if ($isEdit): ?><input type="hidden" name="id" value="<?= e($page['id']) ?>"><?php endif; ?>
                <div class="page-editor-layout">
                    <div class="page-editor-fields">
                        <label>Baslik<input name="title" required maxlength="180" value="<?= e($page['title'] ?? '') ?>"></label>
                        <label>Slug<input name="slug" required maxlength="190" value="<?= e($page['slug'] ?? '') ?>"></label>
                        <label>SEO title<input name="seo_title" maxlength="190" value="<?= e($page['seo_title'] ?? '') ?>"></label>
                        <label>SEO description<input name="seo_description" maxlength="255" value="<?= e($page['seo_description'] ?? '') ?>"></label>
                        <section class="page-section-editor wide" data-page-section-editor>
                            <div class="page-section-editor-head">
                                <div>
                                    <h2>Bu Sayfada / Bolumler</h2>
                                    <p class="muted">Public sayfadaki sol "Bu Sayfada" listesi bu bolum basliklarindan olusur.</p>
                                </div>
                                <div class="page-section-actions">
                                    <button class="pill" type="button" data-page-sections-parse>Icerikten al</button>
                                    <button class="pill primary" type="button" data-page-section-add>Bolum ekle</button>
                                </div>
                            </div>
                            <div class="page-section-list" data-page-section-list></div>
                        </section>
                        <details class="wide page-raw-content">
                            <summary>HTML icerigi gelismis olarak duzenle</summary>
                            <label>Icerik<textarea name="content" rows="18" required data-page-content><?= e($page['content'] ?? '') ?></textarea></label>
                        </details>
                    </div>
                    <aside class="page-editor-options" aria-label="Sayfa secenekleri">
                        <h2>Sayfa Secenekleri</h2>
                        <label>Dil<select name="language"><?php foreach (['tr','en','de','fr'] as $lang): ?><option value="<?= e($lang) ?>" <?= ($page['language'] ?? 'tr') === $lang ? 'selected' : '' ?>><?= e($lang) ?></option><?php endforeach; ?></select></label>
                        <label>Durum<select name="status"><?php foreach (['draft','published','hidden','trashed'] as $status): ?><option value="<?= e($status) ?>" <?= ($page['status'] ?? 'draft') === $status ? 'selected' : '' ?>><?= e($status) ?></option><?php endforeach; ?></select></label>
                        <label>Siralama<input type="number" name="sort_order" min="0" max="999999" step="1" value="<?= e((string) ($page['sort_order'] ?? 0)) ?>"></label>
                        <label class="check-row"><input type="checkbox" name="show_in_header" value="1" <?= !empty($page['show_in_header']) ? 'checked' : '' ?>> Header'da goster</label>
                        <label class="check-row"><input type="checkbox" name="show_in_menu" value="1" <?= !empty($page['show_in_menu']) ? 'checked' : '' ?>> Menude goster</label>
                        <label class="check-row"><input type="checkbox" name="show_in_footer" value="1" <?= !isset($page['show_in_footer']) || !empty($page['show_in_footer']) ? 'checked' : '' ?>> Footer'da goster</label>
                        <?php if ($isEdit): ?>
                            <div class="page-editor-meta">
                                <span>ID #<?= e($page['id'] ?? '') ?></span>
                                <span>Guncelleme: <?= e($page['updated_at'] ?? '-') ?></span>
                            </div>
                        <?php endif; ?>
                    </aside>
                </div>
                <div class="page-editor-submit">
                    <button class="pill primary" type="submit"><?= $isEdit ? 'Kaydet' : 'Olustur' ?></button>
                </div>
            </form>
            <?php if ($isEdit): ?>
                <div class="page-editor-danger">
                    <form data-zpanel-form action="/api/zpanel/pages/trash" method="post" data-confirm="Bu sayfa cope tasinsin mi?" data-redirect-on-success="true">
                        <input type="hidden" name="_csrf" value="<?= e($csrfToken ?? '') ?>">
                        <input type="hidden" name="id" value="<?= e($page['id']) ?>">
                        <button class="pill" type="submit">Cope al</button>
                    </form>
                    <form data-zpanel-form action="/api/zpanel/pages/delete" method="post" data-confirm="Bu sayfa kalici olarak silinsin mi? Bu islem geri alinamaz." data-redirect-on-success="true">
                        <input type="hidden" name="_csrf" value="<?= e($csrfToken ?? '') ?>">
                        <input type="hidden" name="id" value="<?= e($page['id']) ?>">
                        <button class="pill danger" type="submit">Kalici sil</button>
                    </form>
                </div>
            <?php endif; ?>
        </section>
    </section>
</div>
