<?php
declare(strict_types=1);require __DIR__.'/_bootstrap.php';$user=admin_user();
if(request_is_post()){
 verify_csrf();$name=admin_post_string('name');$email=mb_strtolower(admin_post_string('email'));$new=(string)($_POST['new_password']??'');$current=(string)($_POST['current_password']??'');
 if($name===''||!filter_var($email,FILTER_VALIDATE_EMAIL)){flash('error','Ad ve geçerli e-posta zorunlu.');redirect('/admin/account.php');}$dupe=db()->prepare('SELECT COUNT(*) FROM users WHERE email=? AND id<>?');$dupe->execute([$email,$user['id']]);if((int)$dupe->fetchColumn()>0){flash('error','Bu e-posta başka bir hesapta kullanılıyor.');redirect('/admin/account.php');}$s=db()->prepare('SELECT password_hash FROM users WHERE id=?');$s->execute([$user['id']]);$hash=$s->fetchColumn();if(!password_verify($current,$hash)){flash('error','Mevcut şifre yanlış.');redirect('/admin/account.php');}
 if($new!==''&&strlen($new)<10){flash('error','Yeni şifre en az 10 karakter olmalıdır.');redirect('/admin/account.php');}
 if($new!=='')db()->prepare('UPDATE users SET name=?,email=?,password_hash=?,must_change_password=0,updated_at=NOW() WHERE id=?')->execute([$name,$email,password_hash($new,PASSWORD_DEFAULT),$user['id']]);else db()->prepare('UPDATE users SET name=?,email=?,must_change_password=0,updated_at=NOW() WHERE id=?')->execute([$name,$email,$user['id']]);
 flash('success','Hesap bilgileri güncellendi.');redirect('/admin/account.php');
}
admin_header('Hesabım');?>
<form method="post"><div class="panel"><div class="panel-head"><h2>Yönetici hesabı</h2><button class="button">Güncelle</button></div><div class="panel-body form-grid"><?=csrf_field()?><div><label>Ad</label><input name="name" value="<?=e($user['name'])?>" required></div><div><label>E-posta</label><input type="email" name="email" value="<?=e($user['email'])?>" required></div><div><label>Mevcut şifre</label><input type="password" name="current_password" required></div><div><label>Yeni şifre</label><input type="password" name="new_password" minlength="10"><span class="help">Değiştirmeyecekseniz boş bırakın.</span></div></div></div></form>
<?php admin_footer();?>
