<?php
declare(strict_types=1);require __DIR__.'/_bootstrap.php';
$editId=(int)($_GET['edit']??0);$edit=null;if($editId){$s=db()->prepare('SELECT * FROM rooms WHERE id=?');$s->execute([$editId]);$edit=$s->fetch();}
if(request_is_post()){
 verify_csrf();$id=(int)($_POST['id']??0);
 try{
  $name=admin_post_string('name');if($name==='')throw new RuntimeException('Oda adı zorunlu.');$roomType=admin_post_string('room_type','single');if(!in_array($roomType,['single','couple','hammam','wet_area','vip'],true))throw new RuntimeException('Geçersiz oda türü.');$capacity=min(50,max(1,(int)($_POST['capacity']??1)));
  $values=[$name,$roomType,$capacity,admin_post_string('notes'),!empty($_POST['is_active'])?1:0,(int)($_POST['sort_order']??0)];$pdo=db();$pdo->beginTransaction();try{
   if($id){$values[]=$id;$pdo->prepare('UPDATE rooms SET name=?,room_type=?,capacity=?,notes=?,is_active=?,sort_order=?,updated_at=NOW() WHERE id=?')->execute($values);}else{$pdo->prepare('INSERT INTO rooms(name,room_type,capacity,notes,is_active,sort_order,created_at,updated_at) VALUES(?,?,?,?,?,?,NOW(),NOW())')->execute($values);$id=(int)$pdo->lastInsertId();$pdo->prepare('INSERT INTO room_services(room_id,service_id) SELECT ?,id FROM services')->execute([$id]);}
   $pdo->commit();
  }catch(Throwable $e){if($pdo->inTransaction())$pdo->rollBack();throw $e;}
  audit_log('save','rooms',$id);flash('success','Oda kaydedildi.');redirect('/admin/rooms.php');
 }catch(Throwable $e){flash('error',$e->getMessage());redirect('/admin/rooms.php'.($id?'?edit='.$id:''));}
}
$items=db()->query('SELECT r.*,(SELECT COUNT(*) FROM room_services rs WHERE rs.room_id=r.id) service_count FROM rooms r ORDER BY r.sort_order,r.id')->fetchAll();admin_header('Odalar');?>
<div class="split-admin"><div class="panel" style="margin-top:0"><div class="panel-head"><h2>Bakım odaları</h2></div><div class="table-wrap"><table><thead><tr><th>Oda</th><th>Tür</th><th>Kapasite</th><th>Durum</th><th></th></tr></thead><tbody><?php foreach($items as $r):?><tr><td><b><?=e($r['name'])?></b><br><span class="muted"><?=$r['service_count']?> hizmet</span></td><td><?=e($r['room_type'])?></td><td><?=$r['capacity']?></td><td><?=admin_status($r['is_active']?'active':'inactive')?></td><td><a class="button small secondary" href="?edit=<?=$r['id']?>">Düzenle</a></td></tr><?php endforeach;?></tbody></table></div></div><form class="panel" style="margin-top:0" method="post"><div class="panel-head"><h2><?=$edit?'Odayı Düzenle':'Yeni Oda'?></h2></div><div class="panel-body"><?=csrf_field()?><input type="hidden" name="id" value="<?=e($edit['id']??0)?>"><label>Oda adı<input name="name" value="<?=e($edit['name']??'')?>" required></label><label>Oda türü<select name="room_type"><?php foreach(['single'=>'Tekli','couple'=>'Çift','hammam'=>'Hamam','wet_area'=>'Islak Alan','vip'=>'VIP'] as $k=>$v):?><option value="<?=$k?>" <?=($edit['room_type']??'single')===$k?'selected':''?>><?=$v?></option><?php endforeach;?></select></label><label>Kapasite<input type="number" min="1" max="20" name="capacity" value="<?=e($edit['capacity']??1)?>"></label><label>Not<textarea name="notes"><?=e($edit['notes']??'')?></textarea></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();?>
