<?php
$pageTitle = 'Edit Page';
require_once dirname(__DIR__) . '/includes/header.php';

$id = (int)($_GET['id'] ?? 0);
if (!$id) redirect(ADMIN_URL.'/pages/pages.php');
$page = db()->query("SELECT * FROM pages WHERE id=$id")->fetch_assoc();
if (!$page) { flash('Page not found','error'); redirect(ADMIN_URL.'/pages/pages.php'); }

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $title        = trim($_POST['title'] ?? '');
    $slug         = trim($_POST['slug'] ?? '') ?: slug($title);
    $meta_title   = trim($_POST['meta_title'] ?? '');
    $meta_desc    = trim($_POST['meta_desc'] ?? '');
    $hero_heading = trim($_POST['hero_heading'] ?? '');
    $hero_sub     = trim($_POST['hero_sub'] ?? '');
    $hero_bg      = handleUpload('hero_bg','pages') ?? trim($_POST['hero_bg_url'] ?? $page['hero_bg_image']);
    $content      = $_POST['content'] ?? '';
    $status       = $_POST['status'] ?? 'published';

    $st = db()->prepare("UPDATE pages SET title=?,slug=?,meta_title=?,meta_desc=?,hero_heading=?,hero_subheading=?,hero_bg_image=?,content=?,status=? WHERE id=?");
    $st->bind_param('ssssssssi', $title,$slug,$meta_title,$meta_desc,$hero_heading,$hero_sub,$hero_bg,$content,$status,$id);
    // Fix bind: 10 params
    $st = db()->prepare("UPDATE pages SET title=?,slug=?,meta_title=?,meta_desc=?,hero_heading=?,hero_subheading=?,hero_bg_image=?,content=?,status=? WHERE id=?");
    $st->bind_param('sssssssssi', $title,$slug,$meta_title,$meta_desc,$hero_heading,$hero_sub,$hero_bg,$content,$status,$id);

    if ($st->execute()) {
        flash('Page updated!','success');
        redirect(ADMIN_URL.'/pages/page-edit.php?id='.$id);
    } else {
        flash('Error: '.db()->error,'error');
    }
}
?>
<div class="page-header">
    <h1>Edit Page: <?= e($page['title']) ?></h1>
    <div class="page-header-actions" style="display:flex;gap:8px">
        <a href="pages.php" class="btn btn-secondary"><i class="fas fa-arrow-left"></i> Back</a>
        <a href="<?= SITE_URL.'/'.e($page['slug']) ?>.php" target="_blank" class="btn btn-secondary"><i class="fas fa-eye"></i> View</a>
        <button onclick="confirmDelete('pages.php?delete=<?= $id ?>','<?= e(addslashes($page['title'])) ?>')" class="btn btn-danger"><i class="fas fa-trash"></i></button>
    </div>
</div>

<form method="POST" enctype="multipart/form-data">
<div style="display:grid;grid-template-columns:1fr 300px;gap:20px;align-items:start">
<div style="display:flex;flex-direction:column;gap:20px">
    <div class="card">
        <div class="card-header"><i class="fas fa-file-alt" style="color:var(--brand)"></i><h2>Page Info</h2></div>
        <div class="card-body">
            <div class="form-group"><label class="form-label">Title</label>
                <input type="text" name="title" class="form-control" value="<?= e($page['title']) ?>" required></div>
            <div class="form-group"><label class="form-label">Slug</label>
                <input type="text" name="slug" class="form-control" value="<?= e($page['slug']) ?>"></div>
        </div>
    </div>
    <div class="card">
        <div class="card-header"><i class="fas fa-mountain" style="color:var(--brand)"></i><h2>Hero Section</h2></div>
        <div class="card-body">
            <div class="form-group"><label class="form-label">Hero Heading</label>
                <input type="text" name="hero_heading" class="form-control" value="<?= e($page['hero_heading']) ?>"></div>
            <div class="form-group"><label class="form-label">Hero Subheading</label>
                <textarea name="hero_sub" class="form-control" rows="2"><?= e($page['hero_subheading']) ?></textarea></div>
            <div class="form-row">
                <div class="form-group">
                    <label class="form-label">New Background Image</label>
                    <input type="file" name="hero_bg" class="form-control" accept="image/*">
                </div>
                <div class="form-group">
                    <label class="form-label">Or URL/Path</label>
                    <input type="text" name="hero_bg_url" class="form-control" value="<?= e($page['hero_bg_image']) ?>">
                </div>
            </div>
        </div>
    </div>
    <div class="card">
        <div class="card-header"><i class="fas fa-align-left" style="color:var(--brand)"></i><h2>Page Content</h2></div>
        <div class="card-body">
            <div id="editor"><?= $page['content'] ?></div>
            <input type="hidden" name="content" id="content-input">
        </div>
    </div>
    <div class="card">
        <div class="card-header"><i class="fas fa-search" style="color:var(--brand)"></i><h2>SEO / Meta</h2></div>
        <div class="card-body">
            <div class="form-group"><label class="form-label">Meta Title</label>
                <input type="text" name="meta_title" class="form-control" value="<?= e($page['meta_title']) ?>"></div>
            <div class="form-group" style="margin:0"><label class="form-label">Meta Description</label>
                <textarea name="meta_desc" class="form-control" rows="3"><?= e($page['meta_desc']) ?></textarea></div>
        </div>
    </div>
</div>
<div style="display:flex;flex-direction:column;gap:16px">
    <div class="card">
        <div class="card-header"><i class="fas fa-cog"></i><h2>Publish</h2></div>
        <div class="card-body">
            <div class="form-group"><label class="form-label">Status</label>
                <select name="status" class="form-select">
                    <option value="published" <?= $page['status']==='published'?'selected':'' ?>>Published</option>
                    <option value="draft" <?= $page['status']==='draft'?'selected':'' ?>>Draft</option>
                </select>
            </div>
            <button type="submit" class="btn btn-primary" style="width:100%"><i class="fas fa-save"></i> Update Page</button>
        </div>
    </div>
</div>
</div>
</form>

<link href="https://cdn.quilljs.com/1.3.7/quill.snow.css" rel="stylesheet">
<script src="https://cdn.quilljs.com/1.3.7/quill.min.js"></script>
<script>
var quill=new Quill('#editor',{theme:'snow',modules:{toolbar:[[{header:[1,2,3,false]}],['bold','italic','underline'],[{list:'ordered'},{list:'bullet'}],['link','image','blockquote'],['clean']]}});
document.querySelector('form').addEventListener('submit',function(){document.getElementById('content-input').value=quill.root.innerHTML;});
</script>
<?php require_once dirname(__DIR__) . '/includes/footer.php'; ?>
