<?php
declare(strict_types=1);require __DIR__.'/_bootstrap.php';
$editId=(int)($_GET['edit']??0);$edit=null;if($editId){$s=db()->prepare('SELECT * FROM therapists WHERE id=?');$s->execute([$editId]);$edit=$s->fetch();}
if(request_is_post()){
 verify_csrf();$id=(int)($_POST['id']??0);$action=(string)($_POST['action']??'save');
 try{
  if($action==='delete'&&$id){db()->prepare('UPDATE therapists SET is_active=0 WHERE id=?')->execute([$id]);flash('success','Terapist pasife alındı.');redirect('/admin/therapists.php');}
  $name=admin_post_string('name');if($name==='')throw new RuntimeException('Ad zorunlu.');
  $gender=admin_post_string('gender','unspecified');if(!in_array($gender,['female','male','unspecified'],true))throw new RuntimeException('Geçersiz cinsiyet seçimi.');
  $photo=upload_image('photo','therapists')?:admin_post_string('existing_photo');$values=[$name,admin_post_string('title'),admin_post_string('nationality'),$gender,admin_post_string('languages'),admin_post_string('bio'),$photo,!empty($_POST['is_active'])?1:0,(int)($_POST['sort_order']??0)];
  $pdo=db();$pdo->beginTransaction();try{
   if($id){$values[]=$id;$pdo->prepare('UPDATE therapists SET name=?,title=?,nationality=?,gender=?,languages=?,bio=?,photo=?,is_active=?,sort_order=?,updated_at=NOW() WHERE id=?')->execute($values);}else{$pdo->prepare('INSERT INTO therapists(name,title,nationality,gender,languages,bio,photo,is_active,sort_order,created_at,updated_at) VALUES(?,?,?,?,?,?,?,?,?,NOW(),NOW())')->execute($values);$id=(int)$pdo->lastInsertId();$pdo->prepare('INSERT INTO therapist_services(therapist_id,service_id) SELECT ?,id FROM services')->execute([$id]);$pdo->prepare('INSERT INTO therapist_schedules(therapist_id,weekday,start_time,end_time,is_off) SELECT ?,weekday,open_time,close_time,is_closed FROM business_hours')->execute([$id]);}
   $pdo->commit();
  }catch(Throwable $e){if($pdo->inTransaction())$pdo->rollBack();throw $e;}
  audit_log('save','therapists',$id);flash('success','Terapist kaydedildi.');redirect('/admin/therapists.php');
 }catch(Throwable $e){flash('error',$e->getMessage());redirect('/admin/therapists.php'.($id?'?edit='.$id:''));}
}
$items=db()->query('SELECT t.*,(SELECT COUNT(*) FROM therapist_services ts WHERE ts.therapist_id=t.id) service_count FROM therapists t ORDER BY t.sort_order,t.id')->fetchAll();admin_header('Terapistler');?>
<div class="split-admin"><div class="panel" style="margin-top:0"><div class="panel-head"><h2>Terapist kadrosu</h2></div><div class="table-wrap"><table><thead><tr><th>Ad</th><th>Uzmanlık</th><th>Dil</th><th>Durum</th><th></th></tr></thead><tbody><?php foreach($items as $t):?><tr><td><b><?=e($t['name'])?></b><br><span class="muted"><?=e($t['nationality'])?></span></td><td><?=e($t['title'])?><br><span class="muted"><?=$t['service_count']?> hizmet</span></td><td><?=e($t['languages'])?></td><td><?=admin_status($t['is_active']?'active':'inactive')?></td><td><a class="button small secondary" href="?edit=<?=$t['id']?>">Düzenle</a></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?'Terapisti Düzenle':'Yeni Terapist'?></h2></div><div class="panel-body"><?=csrf_field()?><input type="hidden" name="id" value="<?=e($edit['id']??0)?>"><label>Ad<input name="name" value="<?=e($edit['name']??'')?>" required></label><label>Unvan<input name="title" value="<?=e($edit['title']??'')?>"></label><label>Uyruk<input name="nationality" value="<?=e($edit['nationality']??'')?>"></label><label>Cinsiyet<select name="gender"><option value="unspecified">Belirtilmedi</option><option value="female" <?=($edit['gender']??'')==='female'?'selected':''?>>Kadın</option><option value="male" <?=($edit['gender']??'')==='male'?'selected':''?>>Erkek</option></select></label><label>Diller<input name="languages" value="<?=e($edit['languages']??'')?>"></label><label>Biyografi<textarea name="bio" rows="5"><?=e($edit['bio']??'')?></textarea></label><label>Fotoğraf<input type="file" name="photo" accept="image/*"><input type="hidden" name="existing_photo" value="<?=e($edit['photo']??'')?>"></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><div class="actions"><button class="button" name="action" value="save">Kaydet</button><?php if($edit):?><button class="button danger" name="action" value="delete" data-confirm="Terapisti pasife almak istiyor musunuz?">Pasife Al</button><?php endif;?></div></div></form></div>
<?php admin_footer();?>
