<?php
$pageTitle = 'Edit Photo';
require_once dirname(__DIR__) . '/includes/header.php';
$id = (int)($_GET['id'] ?? 0);
if (!$id) redirect(ADMIN_URL.'/pages/gallery.php');
$p = db()->query("SELECT * FROM gallery WHERE id=$id")->fetch_assoc();
if (!$p) { flash('Photo not found.','error'); redirect(ADMIN_URL.'/pages/gallery.php'); }

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $title = trim($_POST['title'] ?? '');
    $cat   = trim($_POST['category'] ?? 'general');
    $sort  = (int)($_POST['sort_order'] ?? 0);
    $img   = handleUpload('photo','gallery') ?? trim($_POST['image_url'] ?? $p['image']);
    $st = db()->prepare("UPDATE gallery SET title=?,image=?,category=?,sort_order=? WHERE id=?");
    $st->bind_param('sssii', $title,$img,$cat,$sort,$id);
    $st->execute();
    flash('Photo updated!','success');
    redirect(ADMIN_URL.'/pages/gallery.php');
}
?>
<div class="page-header">
    <a href="gallery.php" class="btn btn-secondary btn-sm"><i class="fas fa-arrow-left"></i></a>
    <h1>Edit Photo</h1>
</div>
<div class="card">
    <div class="card-header"><i class="fas fa-image" style="color:var(--brand)"></i><h2>Edit Gallery Photo</h2></div>
    <div class="card-body">
        <form method="POST" enctype="multipart/form-data">
            <div style="text-align:center;margin-bottom:20px">
                <img id="imgPreview" src="<?=e(SITE_URL.'/'.$p['image'])?>" style="max-height:200px;max-width:100%;border-radius:10px;border:1px solid var(--border)">
            </div>
            <div class="form-group">
                <label class="form-label">Replace Image</label>
                <input type="file" name="photo" class="form-control" accept="image/*" onchange="previewImg(this)">
            </div>
            <div class="form-group">
                <label class="form-label">Or Image URL</label>
                <input type="text" name="image_url" class="form-control" value="<?=e($p['image'])?>">
            </div>
            <div class="form-row">
                <div class="form-group">
                    <label class="form-label">Title / Caption</label>
                    <input type="text" name="title" class="form-control" value="<?=e($p['title'])?>">
                </div>
                <div class="form-group">
                    <label class="form-label">Category</label>
                    <input type="text" name="category" class="form-control" value="<?=e($p['category'])?>">
                </div>
            </div>
            <div class="form-group" style="max-width:200px">
                <label class="form-label">Sort Order</label>
                <input type="number" name="sort_order" class="form-control" value="<?=(int)$p['sort_order']?>">
            </div>
            <div style="display:flex;gap:10px">
                <button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> Save Changes</button>
                <a href="gallery.php" class="btn btn-secondary">Cancel</a>
            </div>
        </form>
    </div>
</div>
<script>
function previewImg(i){const r=new FileReader();r.onload=e=>{document.getElementById('imgPreview').src=e.target.result};r.readAsDataURL(i.files[0]);}
</script>
<?php require_once dirname(__DIR__) . '/includes/footer.php'; ?>
