<?php
declare(strict_types=1);require __DIR__.'/_bootstrap.php';
$editId=(int)($_GET['edit']??0);$edit=null;if($editId){$q=db()->prepare('SELECT * FROM gallery_items WHERE id=?');$q->execute([$editId]);$edit=$q->fetch();}
if(request_is_post()){
 verify_csrf();try{
  $action=admin_post_string('action','save');$id=(int)($_POST['id']??0);
  if($action==='toggle'&&$id){db()->prepare('UPDATE gallery_items SET is_active=1-is_active,updated_at=NOW() WHERE id=?')->execute([$id]);flash('success','Galeri öğesi durumu değiştirildi.');redirect('/admin/gallery.php');}
  $title=admin_post_string('title');if($title==='')throw new RuntimeException('Görsel başlığı zorunlu.');$image=upload_image('image_path','gallery')?:admin_post_string('existing_image');if($image==='')throw new RuntimeException('Galeri görseli zorunlu.');
  $values=[$title,admin_post_string('category','Wellness Alanları'),$image,admin_post_string('alt_text',$title),!empty($_POST['is_active'])?1:0,(int)($_POST['sort_order']??0)];
  if($id){$values[]=$id;db()->prepare('UPDATE gallery_items SET title=?,category=?,image_path=?,alt_text=?,is_active=?,sort_order=?,updated_at=NOW() WHERE id=?')->execute($values);}else{db()->prepare('INSERT INTO gallery_items(title,category,image_path,alt_text,is_active,sort_order,created_at,updated_at) VALUES(?,?,?,?,?,?,NOW(),NOW())')->execute($values);$id=(int)db()->lastInsertId();}
  audit_log('save','gallery_items',$id);flash('success','Galeri öğesi kaydedildi.');redirect('/admin/gallery.php');
 }catch(Throwable $e){flash('error',$e->getMessage());redirect('/admin/gallery.php'.(!empty($_POST['id'])?'?edit='.(int)$_POST['id']:''));}
}
$items=db()->query('SELECT * FROM gallery_items ORDER BY sort_order,id')->fetchAll();admin_header('Galeri');?>
<div class="split-admin"><div class="panel" style="margin-top:0"><div class="panel-head"><h2>Spa ve tesis görselleri</h2></div><div class="table-wrap"><table><thead><tr><th>Görsel</th><th>Başlık</th><th>Kategori</th><th>Durum</th><th></th></tr></thead><tbody><?php foreach($items as $g):?><tr><td><img src="<?=e(url($g['image_path']))?>" alt="" style="width:84px;height:62px;object-fit:cover;border-radius:8px"></td><td><b><?=e($g['title'])?></b><br><span class="muted"><?=e($g['alt_text'])?></span></td><td><?=e($g['category'])?></td><td><?=admin_status($g['is_active']?'active':'inactive')?></td><td><div class="actions"><a class="button small secondary" href="?edit=<?=$g['id']?>">Düzenle</a><form method="post"><?=csrf_field()?><input type="hidden" name="action" value="toggle"><button class="button small" name="id" value="<?=$g['id']?>"><?=$g['is_active']?'Kapat':'Aç'?></button></form></div></td></tr><?php endforeach;?></tbody></table></div></div>
<form class="panel" style="margin-top:0" method="post" enctype="multipart/form-data"><div class="panel-head"><h2><?=$edit?'Görseli Düzenle':'Yeni Görsel'?></h2></div><div class="panel-body"><?=csrf_field()?><input type="hidden" name="id" value="<?=e($edit['id']??0)?>"><input type="hidden" name="existing_image" value="<?=e($edit['image_path']??'')?>"><label>Başlık<input name="title" value="<?=e($edit['title']??'')?>" required></label><label>Kategori<input name="category" value="<?=e($edit['category']??'Wellness Alanları')?>"></label><label>Alternatif metin<input name="alt_text" value="<?=e($edit['alt_text']??'')?>"></label><label>Görsel<input type="file" name="image_path" accept="image/jpeg,image/png,image/webp,image/avif" <?=$edit?'':'required'?>></label><label>Sıra<input type="number" name="sort_order" value="<?=e($edit['sort_order']??0)?>"></label><label class="checkrow"><input type="checkbox" name="is_active" value="1" <?=!$edit||!empty($edit['is_active'])?'checked':''?>> Aktif</label><button class="button" type="submit">Kaydet</button></div></form></div>
<?php admin_footer();?>
