<?php
session_start();

// Must be logged in as sub-admin
if (!isset($_SESSION['is_subadmin']) || $_SESSION['is_subadmin'] !== true) {
    header('Location: admin_login.php');
    exit;
}

// Load permission helper
require_once __DIR__ . '/helpers/permission_helper.php';

$perms = $_SESSION['subadmin_permissions'] ?? [];
$displayName = $_SESSION['subadmin_display_name'] ?? ($_SESSION['username'] ?? 'Sub-admin');

// Helper function that uses the new permission hierarchy
function can($key, $action = 'view') {
    global $perms;
    // If permission is stored as boolean (old format), treat true as 'editor'
    if (isset($perms[$key])) {
        if (is_bool($perms[$key])) {
            $perms[$key] = $perms[$key] ? 'editor' : 'none';
        }
    }
    return PermissionHelper::can($key, $action, $perms);
}

function hasAccess($key) {
    global $perms;
    // Convert old boolean format to new hierarchy
    if (isset($perms[$key])) {
        if (is_bool($perms[$key])) {
            $perms[$key] = $perms[$key] ? 'editor' : 'none';
        }
    }
    return PermissionHelper::hasAccess($key, $perms);
}

function getLevel($key) {
    global $perms;
    // Convert old boolean format to new hierarchy
    if (isset($perms[$key])) {
        if (is_bool($perms[$key])) {
            $perms[$key] = $perms[$key] ? 'editor' : 'none';
        }
    }
    return PermissionHelper::getLevel($key, $perms);
}

?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Sub-admin Dashboard</title>
<link rel="stylesheet" href="styles.css">
<style>
.container { max-width:1100px; margin:24px auto; padding:16px; }
.card { background:#fff; padding:16px; border-radius:8px; box-shadow:0 4px 12px rgba(0,0,0,0.06); }
.grid { display:grid; grid-template-columns:repeat(auto-fit, minmax(220px, 1fr)); gap:12px; }
.link-card { 
    padding:20px; 
    border-radius:8px; 
    background:#f9f9f9; 
    text-align:center;
    position: relative;
    border-left: 4px solid #ddd;
    transition: all 0.3s ease;
}
.link-card:hover {
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    transform: translateY(-2px);
}
.link-card.viewer {
    border-left-color: #2196F3;
    background: #E3F2FD;
}
.link-card.editor {
    border-left-color: #FF9800;
    background: #FFF3E0;
}
.link-card.administrator {
    border-left-color: #4CAF50;
    background: #E8F5E9;
}
.link-card a {
    text-decoration: none;
    color: inherit;
    display: block;
    font-weight: 600;
    margin-bottom: 8px;
}
.permission-level {
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    padding: 4px 8px;
    border-radius: 4px;
    display: inline-block;
}
.level-viewer {
    background: #2196F3;
    color: white;
}
.level-editor {
    background: #FF9800;
    color: white;
}
.level-administrator {
    background: #4CAF50;
    color: white;
}
.btn { padding:8px 12px; border-radius:6px; background:#F07E2D; color:#fff; text-decoration:none; display:inline-block; }
.header-flex { display: flex; justify-content: space-between; align-items: center; }
</style>
</head>
<body>
<div class="container">
    <div class="header-flex">
        <div>
            <h2 style="margin:0;">Sub-admin Dashboard</h2>
            <p style="margin:4px 0; font-size:14px; color:#666;">Welcome, <?php echo htmlspecialchars($displayName); ?></p>
        </div>
        <a href="logout_admin.php" class="btn" style="background:#e74c3c; margin:0;">Logout</a>
    </div>

    <div class="card" style="margin-top:20px;">
        <h3>Your Access & Permission Levels</h3>
        <p style="font-size:13px; color:#666; margin-bottom:16px;">
            <strong>Viewer:</strong> View only | 
            <strong>Editor:</strong> View, Create, Edit (no delete) | 
            <strong>Administrator:</strong> Full access (View, Add, Edit, Delete)
        </p>
        <div class="grid">
            <?php if (hasAccess('manage_site_title')): ?>
                <div class="link-card <?php echo getLevel('manage_site_title'); ?>">
                    <a href="admin_dashboard.php">📝 Site Title</a>
                    <div class="permission-level level-<?php echo getLevel('manage_site_title'); ?>"><?php echo ucfirst(getLevel('manage_site_title')); ?></div>
                </div>
            <?php endif; ?>
            
            <?php if (hasAccess('manage_logo')): ?>
                <div class="link-card <?php echo getLevel('manage_logo'); ?>">
                    <a href="admin_dashboard.php">🎨 Logo</a>
                    <div class="permission-level level-<?php echo getLevel('manage_logo'); ?>"><?php echo ucfirst(getLevel('manage_logo')); ?></div>
                </div>
            <?php endif; ?>
            
            <?php if (hasAccess('manage_slider_images')): ?>
                <div class="link-card <?php echo getLevel('manage_slider_images'); ?>">
                    <a href="admin_manage_slider.php">🖼️ Slider Images</a>
                    <div class="permission-level level-<?php echo getLevel('manage_slider_images'); ?>"><?php echo ucfirst(getLevel('manage_slider_images')); ?></div>
                </div>
            <?php endif; ?>
            
            <?php if (hasAccess('manage_colors')): ?>
                <div class="link-card <?php echo getLevel('manage_colors'); ?>">
                    <a href="admin_dashboard.php">🎭 Header/Footer Colors</a>
                    <div class="permission-level level-<?php echo getLevel('manage_colors'); ?>"><?php echo ucfirst(getLevel('manage_colors')); ?></div>
                </div>
            <?php endif; ?>
            
            <?php if (hasAccess('manage_categories')): ?>
                <div class="link-card <?php echo getLevel('manage_categories'); ?>">
                    <a href="admin_manage_category_carousel.php">📂 Categories</a>
                    <div class="permission-level level-<?php echo getLevel('manage_categories'); ?>"><?php echo ucfirst(getLevel('manage_categories')); ?></div>
                </div>
            <?php endif; ?>
            
            <?php if (hasAccess('manage_pages')): ?>
                <div class="link-card <?php echo getLevel('manage_pages'); ?>">
                    <a href="admin_manage_pages.php">📄 Pages</a>
                    <div class="permission-level level-<?php echo getLevel('manage_pages'); ?>"><?php echo ucfirst(getLevel('manage_pages')); ?></div>
                </div>
            <?php endif; ?>
            
            <?php if (hasAccess('manage_social')): ?>
                <div class="link-card <?php echo getLevel('manage_social'); ?>">
                    <a href="admin_dashboard.php">📱 Social Media</a>
                    <div class="permission-level level-<?php echo getLevel('manage_social'); ?>"><?php echo ucfirst(getLevel('manage_social')); ?></div>
                </div>
            <?php endif; ?>
            
            <?php if (hasAccess('manage_blogs')): ?>
                <div class="link-card <?php echo getLevel('manage_blogs'); ?>">
                    <a href="admin_blogs.php">✍️ Blogs</a>
                    <div class="permission-level level-<?php echo getLevel('manage_blogs'); ?>"><?php echo ucfirst(getLevel('manage_blogs')); ?></div>
                </div>
            <?php endif; ?>
            
            <?php if (hasAccess('manage_products')): ?>
                <div class="link-card <?php echo getLevel('manage_products'); ?>">
                    <a href="admin_products.php">🛍️ Products</a>
                    <div class="permission-level level-<?php echo getLevel('manage_products'); ?>"><?php echo ucfirst(getLevel('manage_products')); ?></div>
                </div>
            <?php endif; ?>
            
            <?php if (hasAccess('manage_users')): ?>
                <div class="link-card <?php echo getLevel('manage_users'); ?>">
                    <a href="admin_manage_users.php">👥 Users</a>
                    <div class="permission-level level-<?php echo getLevel('manage_users'); ?>"><?php echo ucfirst(getLevel('manage_users')); ?></div>
                </div>
            <?php endif; ?>
            
            <?php if (hasAccess('manage_subadmins')): ?>
                <div class="link-card <?php echo getLevel('manage_subadmins'); ?>">
                    <a href="admin_subadmins.php">🔑 Sub-admins</a>
                    <div class="permission-level level-<?php echo getLevel('manage_subadmins'); ?>"><?php echo ucfirst(getLevel('manage_subadmins')); ?></div>
                </div>
            <?php endif; ?>
            
            <?php if (hasAccess('manage_orders')): ?>
                <div class="link-card <?php echo getLevel('manage_orders'); ?>">
                    <a href="admin_orders.php">📦 Orders</a>
                    <div class="permission-level level-<?php echo getLevel('manage_orders'); ?>"><?php echo ucfirst(getLevel('manage_orders')); ?></div>
                </div>
            <?php endif; ?>
            
            <?php if (hasAccess('manage_news')): ?>
                <div class="link-card <?php echo getLevel('manage_news'); ?>">
                    <a href="admin_add_news.php">📢 News / Banners</a>
                    <div class="permission-level level-<?php echo getLevel('manage_news'); ?>"><?php echo ucfirst(getLevel('manage_news')); ?></div>
                </div>
            <?php endif; ?>
            
            <?php if (hasAccess('manage_marquee')): ?>
                <div class="link-card <?php echo getLevel('manage_marquee'); ?>">
                    <a href="admin_manage_marquee.php">⭐ Marquee Text</a>
                    <div class="permission-level level-<?php echo getLevel('manage_marquee'); ?>"><?php echo ucfirst(getLevel('manage_marquee')); ?></div>
                </div>
            <?php endif; ?>
            
            <?php if (hasAccess('manage_footer_marquee')): ?>
                <div class="link-card <?php echo getLevel('manage_footer_marquee'); ?>">
                    <a href="admin_manage_marquee.php">⭐ Footer Marquee</a>
                    <div class="permission-level level-<?php echo getLevel('manage_footer_marquee'); ?>"><?php echo ucfirst(getLevel('manage_footer_marquee')); ?></div>
                </div>
            <?php endif; ?>
            
            <?php if (hasAccess('manage_featured_categories')): ?>
                <div class="link-card <?php echo getLevel('manage_featured_categories'); ?>">
                    <a href="admin_manage_homepage.php">⭐ Featured Categories</a>
                    <div class="permission-level level-<?php echo getLevel('manage_featured_categories'); ?>"><?php echo ucfirst(getLevel('manage_featured_categories')); ?></div>
                </div>
            <?php endif; ?>
            
            <?php if (hasAccess('manage_carousel')): ?>
                <div class="link-card <?php echo getLevel('manage_carousel'); ?>">
                    <a href="admin_manage_carousel.php">🎠 Carousel</a>
                    <div class="permission-level level-<?php echo getLevel('manage_carousel'); ?>"><?php echo ucfirst(getLevel('manage_carousel')); ?></div>
                </div>
            <?php endif; ?>
            
            <?php if (hasAccess('manage_slider_settings')): ?>
                <div class="link-card <?php echo getLevel('manage_slider_settings'); ?>">
                    <a href="admin_manage_slider.php">⚙️ Slider Settings</a>
                    <div class="permission-level level-<?php echo getLevel('manage_slider_settings'); ?>"><?php echo ucfirst(getLevel('manage_slider_settings')); ?></div>
                </div>
            <?php endif; ?>
            
            <?php if (hasAccess('manage_awards')): ?>
                <div class="link-card <?php echo getLevel('manage_awards'); ?>">
                    <a href="admin_manage_awards.php">🏆 Awards</a>
                    <div class="permission-level level-<?php echo getLevel('manage_awards'); ?>"><?php echo ucfirst(getLevel('manage_awards')); ?></div>
                </div>
            <?php endif; ?>
            
            <?php if (hasAccess('manage_latest_products')): ?>
                <div class="link-card <?php echo getLevel('manage_latest_products'); ?>">
                    <a href="admin_manage_latest_products.php">🆕 Latest Products</a>
                    <div class="permission-level level-<?php echo getLevel('manage_latest_products'); ?>"><?php echo ucfirst(getLevel('manage_latest_products')); ?></div>
                </div>
            <?php endif; ?>
            
            <?php if (hasAccess('manage_video')): ?>
                <div class="link-card <?php echo getLevel('manage_video'); ?>">
                    <a href="admin_manage_video.php">🎬 Video Section</a>
                    <div class="permission-level level-<?php echo getLevel('manage_video'); ?>"><?php echo ucfirst(getLevel('manage_video')); ?></div>
                </div>
            <?php endif; ?>
            
            <?php if (hasAccess('manage_footer_settings')): ?>
                <div class="link-card <?php echo getLevel('manage_footer_settings'); ?>">
                    <a href="admin_manage_footer.php">🔧 Footer Settings</a>
                    <div class="permission-level level-<?php echo getLevel('manage_footer_settings'); ?>"><?php echo ucfirst(getLevel('manage_footer_settings')); ?></div>
                </div>
            <?php endif; ?>
            
            <?php if (hasAccess('manage_seo')): ?>
                <div class="link-card <?php echo getLevel('manage_seo'); ?>">
                    <a href="admin_seo_manager.php">🔍 SEO Manager</a>
                    <div class="permission-level level-<?php echo getLevel('manage_seo'); ?>"><?php echo ucfirst(getLevel('manage_seo')); ?></div>
                </div>
            <?php endif; ?>
            
            <?php if (hasAccess('manage_homepage')): ?>
                <div class="link-card <?php echo getLevel('manage_homepage'); ?>">
                    <a href="admin_manage_homepage.php">🏠 Homepage</a>
                    <div class="permission-level level-<?php echo getLevel('manage_homepage'); ?>"><?php echo ucfirst(getLevel('manage_homepage')); ?></div>
                </div>
            <?php endif; ?>
            
            <?php if (hasAccess('manage_settings')): ?>
                <div class="link-card <?php echo getLevel('manage_settings'); ?>">
                    <a href="admin_dashboard.php">⚛️ General Settings</a>
                    <div class="permission-level level-<?php echo getLevel('manage_settings'); ?>"><?php echo ucfirst(getLevel('manage_settings')); ?></div>
                </div>
            <?php endif; ?>
        </div>
    </div>

    <div style="margin-top:20px;" class="card">
        <h3>How Permissions Work</h3>
        <ul style="line-height:1.8;">
            <li><strong>Viewer:</strong> Can view content only, no modifications allowed</li>
            <li><strong>Editor:</strong> Can view, add, and edit content - but cannot delete</li>
            <li><strong>Administrator:</strong> Full access - can view, add, edit, and delete</li>
        </ul>
    </div>
</div>
</body>
</html>
