<?php
require_once __DIR__ . '/_init.php';

function web_nav_icon($href){
    $map = [
        'index.php'=>'🏠','analytics.php'=>'📊','search.php'=>'🔎','users.php'=>'👥','orders.php'=>'📦','payments-report.php'=>'📄','accounting.php'=>'📒','sponsor.php'=>'🤝','reactions.php'=>'🔥','forcedjoin-buttons.php'=>'🎨','tickets.php'=>'🎧','canned-replies.php'=>'⚡','referrals.php'=>'👥','fraud.php'=>'🛡️','left-penalty.php'=>'🚪','gift-codes.php'=>'🎁',
        'keyboards.php'=>'⌨️','channel-buttons.php'=>'🎛️','channels.php'=>'📣','settings.php'=>'⚙️','texts.php'=>'📝','broadcast.php'=>'📢',
        'backups.php'=>'🛡️','health.php'=>'🩺','logs.php'=>'📜','admins.php'=>'🔐'
    ];
    return $map[$href] ?? '•';
}
function web_asset_url($file){
    $path = __DIR__ . '/' . ltrim($file, '/');
    $v = is_file($path) ? (string)filemtime($path) : '1';
    return web_h($file . '?v=' . $v);
}
function web_header($title='پنل وب'){
    $self = basename($_SERVER['SCRIPT_NAME'] ?? 'index.php');
    $nav = web_nav_items();
    echo '<!doctype html><html lang="fa" dir="rtl"><head><meta charset="utf-8">';
    echo '<meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover">';
    echo '<meta name="theme-color" content="#2563eb"><meta name="mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-capable" content="yes">';
    echo '<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">';
    echo '<title>'.web_h($title).'</title>';
    echo '<link rel="manifest" href="manifest.webmanifest">';
    echo '<link rel="icon" type="image/png" sizes="192x192" href="assets/icon-192.png">';
    echo '<style>'; echo file_get_contents(__DIR__.'/style.css'); echo '</style>';
    echo '</head><body class="web-pro"><div class="mobile-scrim" data-close-menu></div><div class="app">';
    echo '<aside id="sideNav" aria-label="منوی پنل"><div class="brand"><span class="brand-mark">ب</span><span><b>پنل ربات تلگرام</b><small>Web Panel Pro</small></span></div><nav>';
    foreach($nav as $href=>$item){
        [$label,$perm] = $item;
        if(!web_can($perm)) continue;
        $cls=$self===$href?'active':'';
        echo '<a class="'.$cls.'" href="'.$href.'"><span class="nav-ico">'.web_h(web_nav_icon($href)).'</span><span>'.web_h($label).'</span></a>';
    }
    echo '</nav><div class="side-foot"><div class="admin-pill">ادمین: '.web_h($_SESSION['web_admin_id'] ?? '-').'</div><a class="logout-link" href="logout.php">خروج امن</a></div></aside>';
    echo '<main><header class="topbar"><div class="title-wrap"><button class="icon-btn mobile-menu" type="button" data-open-menu aria-label="باز کردن منو">☰</button><div><h1>'.web_h($title).'</h1><div class="muted">پنل موبایل‌پسند مدیریت ربات</div></div></div>';
    echo '<form class="global-search" action="search.php" method="get"><input name="q" type="search" inputmode="search" placeholder="جستجوی کاربر، سفارش، تیکت..." value="'.web_h($_GET['q'] ?? '').'"><button type="submit">جستجو</button></form>';
    echo '<div class="top-actions"><button class="icon-btn" type="button" data-theme-toggle title="تغییر تم">🌓</button><a class="icon-btn" href="index.php" title="خانه">⌂</a><a class="icon-btn danger-ghost" href="logout.php" title="خروج">⎋</a></div></header>';
    foreach(web_take_flash() as $f){ echo '<div class="alert '.web_h($f['type']).'">'.web_h($f['msg']).'</div>'; }
}
function web_footer(){
    $bottom = [
        'index.php'=>['داشبورد','dashboard','🏠'],
        'users.php'=>['کاربران','users','👥'],
        'orders.php'=>['سفارش‌ها','orders','📦'],
        'tickets.php'=>['تیکت','tickets','🎧'],
        'settings.php'=>['تنظیمات','settings','⚙️'],
    ];
    $self = basename($_SERVER['SCRIPT_NAME'] ?? 'index.php');
    echo '<nav class="bottom-nav" aria-label="منوی سریع موبایل">';
    foreach($bottom as $href=>$item){ [$label,$perm,$icon] = $item; if(!web_can($perm)) continue; $cls=$self===$href?'active':''; echo '<a class="'.$cls.'" href="'.$href.'"><span>'.$icon.'</span><b>'.web_h($label).'</b></a>'; }
    echo '</nav>';
    echo '</main></div><script src="'.web_asset_url('mobile.js').'" defer></script><script>if("serviceWorker" in navigator){window.addEventListener("load",function(){navigator.serviceWorker.register("sw.js").catch(function(){});});}</script></body></html>';
}
function web_card($title, $value, $note=''){
    echo '<div class="card stat"><b>'.web_h($title).'</b><strong>'.web_h($value).'</strong>'.($note!==''?'<span>'.web_h($note).'</span>':'').'</div>';
}
function web_table_start($heads){
    echo '<div class="table-wrap"><table data-pro-table="1"><thead><tr>';
    foreach($heads as $h) echo '<th>'.web_h($h).'</th>';
    echo '</tr></thead><tbody>';
}
function web_table_end(){ echo '</tbody></table></div>'; }
if (!function_exists('web_csrf_input')) { function web_csrf_input(){ echo '<input type="hidden" name="csrf" value="'.web_h(web_csrf()).'">'; } }
?>
