<?php
declare(strict_types=1);
require_once dirname(__DIR__).'/core/bootstrap.php';
require_admin();
require_admin_access(basename($_SERVER['PHP_SELF']));
header('Cache-Control: private, no-store, max-age=0');
header('Pragma: no-cache');
if ((admin_user()['must_change_password'] ?? 0) && basename($_SERVER['PHP_SELF']) !== 'account.php') { redirect('/admin/account.php'); }

function admin_header(string $title): void {
 $user=admin_user();$current=basename($_SERVER['PHP_SELF']);
 $links=[
  'index.php'=>'Genel Bakış','reservations.php'=>'Rezervasyonlar','services.php'=>'Hizmetler','gallery.php'=>'Galeri','therapists.php'=>'Terapistler','rooms.php'=>'Odalar',
  'schedule.php'=>'Çalışma Takvimi','posts.php'=>'Makaleler','pages.php'=>'Kurumsal Sayfalar','automation.php'=>'Yapay Zekâ Merkezi',
  'settings.php'=>'Site Ayarları','seo.php'=>'SEO ve AMP','users.php'=>'Yöneticiler','account.php'=>'Hesabım'
 ];
 echo '<!doctype html><html lang="tr"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>'.e($title).' | Nekrut Yönetim</title><link rel="stylesheet" href="'.asset('css/admin.css').'"></head><body><div class="admin-shell"><aside class="sidebar"><img src="'.e(url(setting('logo_light','/assets/img/logo-light.svg'))).'" alt="Nekrut">';
 echo '<small>Yönetim</small>';
 foreach($links as $file=>$label)if(admin_can_access($file,$user))echo '<a class="'.($current===$file?'active':'').'" href="'.url('/admin/'.$file).'">'.e($label).'</a>';
 echo '<small>Site</small><a href="'.url('/').'" target="_blank">Siteyi Görüntüle ↗</a><a href="'.url('/admin/logout.php').'">Çıkış Yap</a></aside><main class="admin-main"><header class="admin-top"><div style="display:flex;align-items:center;gap:12px"><button class="button secondary mobile-admin-toggle" data-admin-toggle type="button">☰</button><h1>'.e($title).'</h1></div><div class="user-menu"><span>'.e($user['name']).'</span><a href="'.url('/admin/account.php').'">Hesabım</a></div></header><div class="admin-content">';
 if($m=flash('success'))echo '<div class="flash success">'.e($m).'</div>';if($m=flash('error'))echo '<div class="flash error">'.e($m).'</div>';
}
function admin_footer(): void { echo '</div></main></div><script src="'.asset('js/admin.js').'" defer></script></body></html>'; }
function admin_status(string $status): string { return '<span class="badge '.e($status).'">'.e(ucfirst(str_replace('_',' ',$status))).'</span>'; }
function admin_post_string(string $key,string $default=''): string { return trim((string)($_POST[$key]??$default)); }
