<?php $title = 'Edit Page - ' . htmlspecialchars($page->title); ?>
<div class="d-sm-flex align-items-center justify-content-between mb-4">
    <h1 class="h3 mb-0 text-white font-weight-bold"><i class="fas fa-edit mr-2"></i>Edit Page: <span style="color: #4e73df;"><?= htmlspecialchars($page->title) ?></span></h1>
    <a href="<?= $baseUrl ?>/cms" class="glass-btn"><i class="fas fa-arrow-left mr-1"></i>Back to Pages</a>
</div>

<form method="POST" action="<?= $baseUrl ?>/cms/<?= $page->slug ?>/update">
    <div class="glass-card mb-4">
        <div class="row">
            <div class="col-md-6 form-group">
                <label style="color: rgba(255,255,255,0.7);">Page Title</label>
                <input type="text" name="title" class="form-control glass-input" value="<?= htmlspecialchars($page->title) ?>" required>
            </div>
            <div class="col-md-3 form-group">
                <label style="color: rgba(255,255,255,0.7);">Status</label>
                <select name="status" class="form-control glass-input">
                    <option value="draft" <?= $page->status === 'draft' ? 'selected' : '' ?>>Draft</option>
                    <option value="published" <?= $page->status === 'published' ? 'selected' : '' ?>>Published</option>
                </select>
            </div>
            <div class="col-md-3 form-group">
                <label style="color: rgba(255,255,255,0.7);">Slug</label>
                <input type="text" class="form-control glass-input" value="<?= htmlspecialchars($page->slug) ?>" disabled>
            </div>
        </div>

        <?php if ($page->slug === 'home'): ?>
        <div class="row">
            <div class="col-md-6 form-group">
                <label style="color: rgba(255,255,255,0.7);">Hero Title</label>
                <input type="text" name="hero_title" class="form-control glass-input" value="<?= htmlspecialchars($heroTitle) ?>">
            </div>
            <div class="col-md-6 form-group">
                <label style="color: rgba(255,255,255,0.7);">Hero Subtitle</label>
                <textarea name="hero_subtitle" class="form-control glass-input" rows="2"><?= htmlspecialchars($heroSubtitle) ?></textarea>
            </div>
        </div>
        <?php endif; ?>

        <div class="form-group">
            <label style="color: rgba(255,255,255,0.7);">Page Content</label>
            <textarea name="content" id="cmsEditor" class="form-control glass-input" rows="20"><?= htmlspecialchars($page->content ?? '') ?></textarea>
            <small style="color: rgba(255,255,255,0.4);">For the home page, this content appears between the hero section and announcements.</small>
        </div>
    </div>

    <div class="glass-card mb-4">
        <h5 class="font-weight-bold mb-3" style="color: #4e73df;">SEO Settings</h5>
        <div class="row">
            <div class="col-md-6 form-group">
                <label style="color: rgba(255,255,255,0.7);">Meta Title</label>
                <input type="text" name="meta_title" class="form-control glass-input" value="<?= htmlspecialchars($page->meta_title ?? '') ?>" placeholder="Leave blank to use page title">
            </div>
            <div class="col-md-6 form-group">
                <label style="color: rgba(255,255,255,0.7);">Meta Description</label>
                <textarea name="meta_description" class="form-control glass-input" rows="2"><?= htmlspecialchars($page->meta_description ?? '') ?></textarea>
            </div>
        </div>
    </div>

    <div class="text-right">
        <a href="<?= $baseUrl ?>/cms" class="glass-btn mr-2">Cancel</a>
        <button type="submit" class="glass-btn glass-btn-primary"><i class="fas fa-save mr-1"></i>Save Page</button>
    </div>
</form>

<!-- TinyMCE -->
<script src="https://cdn.tiny.cloud/1/no-api-key/tinymce/6/tinymce.min.js" referrerpolicy="origin"></script>
<script>
tinymce.init({
    selector: '#cmsEditor',
    height: 500,
    menubar: false,
    plugins: 'advlist autolink lists link image charmap preview anchor searchreplace visualblocks code fullscreen insertdatetime media table code help wordcount',
    toolbar: 'undo redo | blocks | bold italic underline strikethrough | forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | removeformat | code',
    content_style: 'body { font-family: Nunito, sans-serif; font-size: 14px; color: #333; }',
    branding: false,
    promotion: false,
});
</script>
