<?php $flash = get_flash(); $user = Auth::user(); ?>
<!doctype html>
<html lang="fa" dir="rtl">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?= e($title ?? app_config()['app_name']) ?></title>
    <link rel="stylesheet" href="public/assets/css/app.css">
</head>
<body>
<?php if (Auth::check()): ?>
<div class="layout">
    <aside class="sidebar">
        <h2><?= e(app_config()['app_name']) ?></h2>
        <div class="hint">پنل مدیریت</div>
        <?php
        $lastSection = null;
        foreach (Auth::menuItems() as $item):
            $itemPage = (string)$item['page'];
            $permission = Auth::pagePermission($itemPage);
            if ($permission !== null && !Auth::can($permission)) {
                continue;
            }
            $section = (string)($item['section'] ?? '');
            if ($section !== '' && $section !== $lastSection):
                $lastSection = $section;
        ?>
            <div class="sidebar-section"><?= e($section) ?></div>
        <?php endif; ?>
            <a class="<?= ($page ?? '') === $itemPage ? 'active' : '' ?>" href="<?= e(url($itemPage)) ?>"><?= e((string)$item['label']) ?></a>
        <?php endforeach; ?>
        <a href="<?= e(url('logout')) ?>">خروج</a>
    </aside>
    <main class="content">
        <div class="topbar">
            <div>
                <strong><?= e($title ?? '') ?></strong>
                <div class="muted">
                    کاربر: <?= e($user['display_name'] ?? $user['username'] ?? '-') ?>
                    <span class="role-pill <?= Auth::isAdmin() ? 'role-admin' : 'role-user' ?>"><?= Auth::isAdmin() ? 'ادمین' : 'کاربر محدود' ?></span>
                </div>
            </div>
            <div class="badge">پنل</div>
        </div>
        <?php if ($flash): ?>
            <div class="flash <?= e($flash['type']) ?>"><?= e($flash['message']) ?></div>
        <?php endif; ?>
<?php else: ?>
<div class="content login-wrap">
    <?php if ($flash): ?>
        <div class="flash <?= e($flash['type']) ?>"><?= e($flash['message']) ?></div>
    <?php endif; ?>
<?php endif; ?>
