<?php
// includes/layout.php - نسخه کامل با سایدبار دسته‌بندی شده

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

require_once __DIR__ . '/settings.php';

if (!isLoggedIn()) {
    header('Location: login.php');
    exit;
}

$user = getCurrentUser();
$role = getUserRole($user['role_id']);
$currentPage = basename($_SERVER['PHP_SELF']);
$currentDir = basename(dirname($_SERVER['PHP_SELF']));

// ===== دریافت تنظیمات سامانه =====
$settings = getSettings();
$siteName = $settings['site_name'] ?? 'سامانه مدیریت اساتید';
$institutionName = $settings['institution_name'] ?? 'دانشگاه علوم و فناوری';
$logoPath = !empty($settings['logo']) ? '../' . $settings['logo'] : '';
$footerText = $settings['footer_text'] ?? '';
$footerDeveloper = $settings['footer_developer'] ?? '';
$footerInstitution = $settings['footer_institution'] ?? $institutionName;

// ===== دریافت تم انتخاب شده =====
$selectedTheme = $settings['theme'] ?? 'blue';

// ===== مسیرهای مطلق =====
$baseUrl = '/teacher/';
$adminUrl = '/teacher/admin/';
$teacherUrl = '/teacher/teacher/';
?>
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= $pageTitle ?? 'داشبورد' ?> | <?= htmlspecialchars($siteName) ?></title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <link rel="stylesheet" href="<?= $baseUrl ?>assets/css/style.css">
    
    <!-- ===== تم‌ها ===== -->
    <link rel="stylesheet" href="assets/css/themes.css">
    
    <!-- ===== jQuery ===== -->
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
    
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@300;400;500;700;900&display=swap');
        * { font-family: 'Vazirmatn', sans-serif; margin: 0; padding: 0; box-sizing: border-box; }
        body { background: #f0f2f5; min-height: 100vh; display: flex; flex-direction: column; }
        .sidebar { position: fixed; right: 0; top: 0; height: 100vh; width: 270px; background: linear-gradient(180deg, #0f0c29, #1a1a3e, #16213e, #0f3460); padding: 0; transition: all 0.3s ease; z-index: 1000; overflow-y: auto; box-shadow: 4px 0 30px rgba(0,0,0,0.2); }
        .sidebar::-webkit-scrollbar { width: 4px; }
        .sidebar::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.15); border-radius: 10px; }
        .sidebar-logo { padding: 25px 25px 20px; border-bottom: 1px solid rgba(255,255,255,0.05); text-align: center; }
        .sidebar-logo .logo-icon { width: 60px; height: 60px; border-radius: 18px; display: flex; align-items: center; justify-content: center; margin: 0 auto 12px; overflow: hidden; box-shadow: 0 10px 30px rgba(102,126,234,0.3); }
        .sidebar-logo .logo-icon img { width: 100%; height: 100%; object-fit: contain; }
        .sidebar-logo .logo-icon .default-logo { width: 100%; height: 100%; background: linear-gradient(135deg, #667eea, #764ba2); display: flex; align-items: center; justify-content: center; font-size: 28px; color: white; }
        .sidebar-logo h2 { color: white; font-weight: 700; font-size: 18px; }
        .sidebar-logo h2 span { color: #667eea; }
        .sidebar-logo small { color: rgba(255,255,255,0.25); font-size: 11px; display: block; margin-top: 3px; }
        .sidebar-menu { padding: 15px 12px 30px; }
        .sidebar-menu .menu-label { color: rgba(255,255,255,0.15); font-size: 10px; text-transform: uppercase; letter-spacing: 1.5px; padding: 15px 15px 8px; font-weight: 700; }
        .sidebar-menu a { display: flex; align-items: center; padding: 10px 16px; margin: 2px 0; color: rgba(255,255,255,0.45); text-decoration: none; border-radius: 12px; transition: all 0.3s ease; font-size: 14px; gap: 12px; position: relative; }
        .sidebar-menu a:hover { background: rgba(255,255,255,0.05); color: white; transform: translateX(-5px); }
        .sidebar-menu a.active { background: linear-gradient(135deg, rgba(102,126,234,0.25), rgba(118,75,162,0.15)); color: white; border: 1px solid rgba(102,126,234,0.2); }
        .sidebar-menu a.active::before { content: ''; position: absolute; right: 0; top: 50%; transform: translateY(-50%); width: 3px; height: 24px; background: linear-gradient(180deg, #667eea, #764ba2); border-radius: 0 4px 4px 0; }
        .sidebar-menu a i { width: 20px; font-size: 15px; text-align: center; }
        .sidebar-menu a .badge { margin-right: auto; background: rgba(244,67,54,0.8); color: white; padding: 1px 9px; border-radius: 20px; font-size: 10px; font-weight: 700; animation: pulse 2s infinite; }
        @keyframes pulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.1); } }
        .main-content { margin-right: 270px; padding: 20px 25px; min-height: 100vh; display: flex; flex-direction: column; flex: 1; }
        .content-wrapper { flex: 1; }
        .top-header { background: white; border-radius: 20px; padding: 15px 25px; display: flex; align-items: center; justify-content: space-between; box-shadow: 0 2px 15px rgba(0,0,0,0.04); margin-bottom: 25px; }
        .top-header .page-title h1 { font-size: 20px; font-weight: 700; color: #1a1a2e; }
        .top-header .page-title small { color: #999; font-size: 12px; }
        .top-header .user-info { display: flex; align-items: center; gap: 18px; }
        .top-header .user-info .notification { position: relative; width: 38px; height: 38px; background: #f5f6fa; border-radius: 50%; display: flex; align-items: center; justify-content: center; cursor: pointer; transition: all 0.3s ease; color: #555; }
        .top-header .user-info .notification:hover { background: #e8e9f0; }
        .top-header .user-info .notification .dot { position: absolute; top: 6px; right: 6px; width: 8px; height: 8px; background: #ef4444; border-radius: 50%; border: 2px solid white; }
        .top-header .user-info .user-avatar { width: 40px; height: 40px; border-radius: 50%; background: linear-gradient(135deg, #667eea, #764ba2); display: flex; align-items: center; justify-content: center; color: white; font-weight: 700; font-size: 16px; }
        .top-header .user-info .user-name { font-weight: 600; color: #1a1a2e; font-size: 14px; }
        .top-header .user-info .user-role { color: #999; font-size: 11px; }
        .top-header .user-info .user-text { text-align: left; }
        .content-wrapper { background: white; border-radius: 20px; padding: 25px; box-shadow: 0 2px 15px rgba(0,0,0,0.04); min-height: 400px; }
        .btn { padding: 8px 20px; border: none; border-radius: 12px; font-size: 14px; font-weight: 500; cursor: pointer; transition: all 0.3s ease; text-decoration: none; display: inline-flex; align-items: center; gap: 8px; }
        .btn-primary { background: linear-gradient(135deg, #667eea, #764ba2); color: white; }
        .btn-primary:hover { transform: translateY(-2px); box-shadow: 0 8px 25px rgba(102,126,234,0.3); }
        .btn-success { background: #10b981; color: white; }
        .btn-success:hover { background: #059669; transform: translateY(-2px); }
        .btn-danger { background: #ef4444; color: white; }
        .btn-danger:hover { background: #dc2626; transform: translateY(-2px); }
        .btn-warning { background: #f59e0b; color: white; }
        .btn-warning:hover { background: #d97706; transform: translateY(-2px); }
        .btn-sm { padding: 5px 14px; font-size: 12px; }
        .form-group { margin-bottom: 18px; }
        .form-group label { display: block; font-weight: 600; color: #374151; margin-bottom: 5px; font-size: 14px; }
        .form-group input, .form-group select, .form-group textarea { width: 100%; padding: 11px 16px; border: 2px solid #e5e7eb; border-radius: 12px; font-size: 14px; transition: all 0.3s ease; font-family: 'Vazirmatn', sans-serif; background: white; }
        .form-group input:focus, .form-group select:focus, .form-group textarea:focus { outline: none; border-color: #667eea; box-shadow: 0 0 0 4px rgba(102,126,234,0.08); }
        .alert { padding: 14px 20px; border-radius: 14px; margin-bottom: 20px; font-size: 14px; display: flex; align-items: center; gap: 12px; }
        .alert-success { background: #d1fae5; color: #065f46; border: 1px solid #a7f3d0; }
        .alert-danger { background: #fecaca; color: #991b1b; border: 1px solid #fca5a5; }
        .badge-success { background: #d1fae5; color: #065f46; padding: 4px 12px; border-radius: 20px; font-size: 12px; display: inline-flex; align-items: center; gap: 5px; }
        .badge-warning { background: #fef3c7; color: #92400e; padding: 4px 12px; border-radius: 20px; font-size: 12px; display: inline-flex; align-items: center; gap: 5px; }
        .badge-danger { background: #fecaca; color: #991b1b; padding: 4px 12px; border-radius: 20px; font-size: 12px; display: inline-flex; align-items: center; gap: 5px; }
        .badge-info { background: #dbeafe; color: #1e40af; padding: 4px 12px; border-radius: 20px; font-size: 12px; display: inline-flex; align-items: center; gap: 5px; }
        @keyframes fadeInUp { from { opacity: 0; transform: translateY(15px); } to { opacity: 1; transform: translateY(0); } }
        .fade-in { animation: fadeInUp 0.5s ease-out; }
        .hamburger { display: none; cursor: pointer; font-size: 22px; color: #1a1a2e; background: none; border: none; }
        .sidebar-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.4); z-index: 999; backdrop-filter: blur(4px); }
        .sidebar-overlay.active { display: block; }
        .footer-bar { margin-top: 30px; padding: 15px 20px; background: white; border-radius: 16px; box-shadow: 0 2px 10px rgba(0,0,0,0.04); text-align: center; font-size: 12px; color: #999; border-top: 1px solid #f0f0f0; }
        .footer-bar .footer-institution { color: #667eea; font-weight: 500; }
        .footer-bar .footer-developer { color: #555; }
        @media (max-width: 768px) { .sidebar { right: -280px; width: 280px; } .sidebar.open { right: 0; } .main-content { margin-right: 0; padding: 15px; } .top-header { flex-direction: column; gap: 12px; } .top-header .user-info { width: 100%; justify-content: space-between; } .hamburger { display: block; } }
        ::-webkit-scrollbar { width: 6px; height: 6px; }
        ::-webkit-scrollbar-track { background: #f1f1f1; border-radius: 10px; }
        ::-webkit-scrollbar-thumb { background: #c1c7cd; border-radius: 10px; }
        ::-webkit-scrollbar-thumb:hover { background: #a0a7ae; }
    </style>
</head>
<body class="theme-<?= $selectedTheme ?>">

<div class="sidebar" id="sidebar">
    <div class="sidebar-logo">
        <div class="logo-icon">
            <?php if (!empty($logoPath) && file_exists($logoPath)): ?>
                <img src="<?= htmlspecialchars($logoPath) ?>" alt="<?= htmlspecialchars($siteName) ?>">
            <?php else: ?>
                <div class="default-logo"><i class="fas fa-graduation-cap"></i></div>
            <?php endif; ?>
        </div>
        <h2><?= htmlspecialchars($siteName) ?></h2>
        <small><?= htmlspecialchars($institutionName) ?></small>
    </div>
    <div class="sidebar-menu">
        
        <?php if ($role['level'] <= 5): ?>
            
            <!-- ============================================================
                 دسته: 📊 داشبورد
                 ============================================================ -->
            <div class="menu-label">📊 داشبورد</div>
            <a href="<?= $adminUrl ?>dashboard.php" class="<?= ($currentPage == 'dashboard.php') ? 'active' : '' ?>">
                <i class="fas fa-chart-pie"></i> داشبورد
            </a>
            
            <!-- ============================================================
                 دسته: 👤 مدیریت کاربران
                 ============================================================ -->
            <div class="menu-label">👤 مدیریت کاربران</div>
            <a href="<?= $adminUrl ?>teachers.php" class="<?= in_array($currentPage, ['teachers.php', 'teacher-edit.php']) ? 'active' : '' ?>">
                <i class="fas fa-users"></i> مدرسین
            </a>
            
            <?php if ($role['level'] == 1): ?>
                <a href="<?= $adminUrl ?>users.php" class="<?= ($currentPage == 'users.php') ? 'active' : '' ?>">
                    <i class="fas fa-users-cog"></i> کاربران
                </a>
                <a href="<?= $adminUrl ?>roles.php" class="<?= ($currentPage == 'roles.php') ? 'active' : '' ?>">
                    <i class="fas fa-user-tag"></i> نقش‌ها
                </a>
                <a href="<?= $adminUrl ?>permissions.php" class="<?= ($currentPage == 'permissions.php') ? 'active' : '' ?>">
                    <i class="fas fa-key"></i> دسترسی‌ها
                </a>
            <?php endif; ?>
            
            <!-- ============================================================
                 دسته: 📚 آموزشی
                 ============================================================ -->
            <div class="menu-label">📚 آموزشی</div>
            <?php if (in_array($role['level'], [1,2,3])): ?>
                <a href="<?= $adminUrl ?>courses.php" class="<?= ($currentPage == 'courses.php') ? 'active' : '' ?>">
                    <i class="fas fa-book"></i> دروس
                </a>
                <a href="<?= $adminUrl ?>course-sessions.php" class="<?= ($currentPage == 'course-sessions.php') ? 'active' : '' ?>">
                    <i class="fas fa-calendar-check"></i> ثبت جلسات
                </a>
            <?php endif; ?>
            
            <?php if ($role['level'] == 1): ?>
                <a href="<?= $adminUrl ?>terms.php" class="<?= ($currentPage == 'terms.php') ? 'active' : '' ?>">
                    <i class="fas fa-calendar-alt"></i> ترم‌ها
                </a>
            <?php endif; ?>
            
            <!-- ============================================================
                 دسته: 💰 مالی
                 ============================================================ -->
            <div class="menu-label">💰 مالی</div>
            <?php if (in_array($role['level'], [1,2,5])): ?>
                <a href="<?= $adminUrl ?>transactions.php" class="<?= ($currentPage == 'transactions.php') ? 'active' : '' ?>">
                    <i class="fas fa-coins"></i> تراکنش‌ها
                </a>
                <a href="<?= $adminUrl ?>report-payment.php" class="<?= ($currentPage == 'report-payment.php') ? 'active' : '' ?>">
                    <i class="fas fa-file-invoice"></i> گزارش پرداخت
                </a>
            <?php endif; ?>
            
            <?php if (in_array($role['level'], [1,2,3,5])): ?>
                <a href="<?= $adminUrl ?>payment-calculate.php" class="<?= ($currentPage == 'payment-calculate.php') ? 'active' : '' ?>">
                    <i class="fas fa-file-invoice"></i> فیش پرداخت
                </a>
            <?php endif; ?>
            
            <?php if ($role['level'] == 1): ?>
                <a href="<?= $adminUrl ?>salary-settings.php" class="<?= ($currentPage == 'salary-settings.php') ? 'active' : '' ?>">
                    <i class="fas fa-cog"></i> تنظیمات حق التدریس
                </a>
            <?php endif; ?>
            
            <!-- ============================================================
                 دسته: 📋 برنامه‌ریزی (جدید)
                 ============================================================ -->
            <div class="menu-label">📋 برنامه‌ریزی</div>
            
            <?php if (in_array($role['level'], [1, 2, 3])): ?>
                <a href="<?= $adminUrl ?>availability-review.php" class="<?= ($currentPage == 'availability-review.php') ? 'active' : '' ?>">
                    <i class="fas fa-clock"></i> بررسی زمان‌بندی
                </a>
                <a href="<?= $adminUrl ?>announcement.php" class="<?= ($currentPage == 'announcement.php') ? 'active' : '' ?>">
                    <i class="fas fa-bullhorn"></i> اعلان‌ها
                </a>
            <?php endif; ?>
            
            <!-- ============================================================
                 دسته: 📁 ابزارها
                 ============================================================ -->
            <?php if (in_array($role['level'], [1,2,5])): ?>
                <div class="menu-label">📁 ابزارها</div>
                <a href="<?= $adminUrl ?>excel-import.php" class="<?= ($currentPage == 'excel-import.php') ? 'active' : '' ?>">
                    <i class="fas fa-file-upload"></i> آپلود اکسل
                </a>
                <a href="<?= $adminUrl ?>excel-export.php" class="<?= ($currentPage == 'excel-export.php') ? 'active' : '' ?>">
                    <i class="fas fa-file-download"></i> خروجی اکسل
                </a>
            <?php endif; ?>
            
            <!-- ============================================================
                 دسته: 💬 ارتباطات
                 ============================================================ -->
            <div class="menu-label">💬 ارتباطات</div>
            <a href="<?= $baseUrl ?>tickets.php" class="<?= ($currentPage == 'tickets.php') ? 'active' : '' ?>">
                <i class="fas fa-ticket-alt"></i> تیکت‌ها
                <?php
                $unreadCount = getUnreadTicketCount($_SESSION['user_id']);
                if ($unreadCount > 0):
                ?>
                <span class="badge"><?= $unreadCount ?></span>
                <?php endif; ?>
            </a>
            
            <!-- ============================================================
                 دسته: ⚙️ تنظیمات
                 ============================================================ -->
            <?php if ($role['level'] == 1): ?>
                <div class="menu-label">⚙️ تنظیمات</div>
                <a href="<?= $adminUrl ?>settings.php" class="<?= ($currentPage == 'settings.php') ? 'active' : '' ?>">
                    <i class="fas fa-sliders-h"></i> تنظیمات سامانه
                </a>
            <?php endif; ?>
            
        <?php else: ?>
            
            <!-- ============================================================
                 پنل مدرس (با داشبورد و گواهی و برنامه‌ریزی)
                 ============================================================ -->
            <div class="menu-label">👤 پنل مدرس</div>
            
            <a href="<?= $teacherUrl ?>dashboard.php" class="<?= ($currentPage == 'dashboard.php') ? 'active' : '' ?>">
                <i class="fas fa-chart-pie"></i> داشبورد
            </a>
            
            <a href="<?= $teacherUrl ?>profile.php" class="<?= ($currentPage == 'profile.php') ? 'active' : '' ?>">
                <i class="fas fa-user"></i> پروفایل
            </a>
            
            <a href="<?= $teacherUrl ?>certificate-request.php" class="<?= ($currentPage == 'certificate-request.php') ? 'active' : '' ?>">
                <i class="fas fa-file-invoice"></i> درخواست گواهی
            </a>
            
            <a href="<?= $teacherUrl ?>availability.php" class="<?= ($currentPage == 'availability.php') ? 'active' : '' ?>">
                <i class="fas fa-calendar-alt"></i> برنامه‌ریزی تدریس
            </a>
            
            <div class="menu-label">💬 ارتباطات</div>
            <a href="<?= $baseUrl ?>tickets.php" class="<?= ($currentPage == 'tickets.php') ? 'active' : '' ?>">
                <i class="fas fa-ticket-alt"></i> تیکت‌ها
                <?php
                $unreadCount = getUnreadTicketCount($_SESSION['user_id']);
                if ($unreadCount > 0):
                ?>
                <span class="badge"><?= $unreadCount ?></span>
                <?php endif; ?>
            </a>
            
        <?php endif; ?>
        
        <!-- ============================================================
             دسته: 🔓 خروج
             ============================================================ -->
        <div class="menu-label">🔓 سیستم</div>
        <a href="<?= $baseUrl ?>logout.php">
            <i class="fas fa-sign-out-alt"></i> خروج
        </a>
    </div>
</div>

<div class="sidebar-overlay" id="sidebarOverlay" onclick="toggleSidebar()"></div>

<div class="main-content">
    <div class="top-header">
        <div style="display: flex; align-items: center; gap: 15px;">
            <button class="hamburger" onclick="toggleSidebar()"><i class="fas fa-bars"></i></button>
            <div class="page-title">
                <h1><?= $pageTitle ?? 'داشبورد' ?></h1>
                <small><?= $pageSubtitle ?? 'خوش آمدید' ?></small>
            </div>
        </div>
        <div class="user-info">
            <div class="notification"><i class="fas fa-bell"></i><span class="dot"></span></div>
            <div class="user-text">
                <div class="user-name"><?= htmlspecialchars($user['name'] . ' ' . $user['family']) ?></div>
                <div class="user-role"><?= htmlspecialchars($role['name']) ?></div>
            </div>
            <div class="user-avatar"><?= mb_substr($user['name'], 0, 1, 'UTF-8') ?></div>
        </div>
    </div>
    <div class="content-wrapper">
        <?php include $content ?? ''; ?>
    </div>
    
    <!-- ===== فوتر ===== -->
    <div class="footer-bar">
        <?php if (!empty($footerText)): ?>
            <div class="footer-text" style="margin-bottom:4px;"><?= htmlspecialchars($footerText) ?></div>
        <?php endif; ?>
        <div>
            <span class="footer-institution"><?= htmlspecialchars($footerInstitution) ?></span>
            <?php if (!empty($footerDeveloper)): ?>
                <span class="footer-developer"> | توسعه: <?= htmlspecialchars($footerDeveloper) ?></span>
            <?php endif; ?>
            <span class="footer-copy"> | <?= date('Y') ?></span>
        </div>
    </div>
</div>

<script>
function toggleSidebar() {
    document.getElementById('sidebar').classList.toggle('open');
    document.getElementById('sidebarOverlay').classList.toggle('active');
}
document.addEventListener('click', function(e) {
    const sidebar = document.getElementById('sidebar');
    const overlay = document.getElementById('sidebarOverlay');
    const hamburger = document.querySelector('.hamburger');
    if (!sidebar.contains(e.target) && !hamburger.contains(e.target)) {
        sidebar.classList.remove('open');
        overlay.classList.remove('active');
    }
});
</script>
</body>
</html>