<?php
// config.php ko shaamil karein, jo session aur database connection ko sambhalega.
if (file_exists('config.php')) {
    require_once 'config.php';
} else {
    die("Configuration file not found. Please ensure config.php is in the correct directory.");
}

// Yeh function jaanch karega ki user ke paas zaroori role hai ya nahi.
if (!function_exists('userHasRole')) {
    function userHasRole($required_roles) {
        // Aapke code me user_roles session use hua hai
        if (!isset($_SESSION['user_roles'])) return false;
        if (!is_array($required_roles)) {
            $required_roles = [$required_roles];
        }
        return count(array_intersect($required_roles, $_SESSION['user_roles'])) > 0;
    }
}


// Suraksha jaanch: User log in hona chahiye.
if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit();
}

// Alag-alag roles ke liye variables set karein
$is_admin = userHasRole(['Admin']);
$is_manager = userHasRole(['Manager']);
$is_smm = userHasRole(['SMM']);
$is_sales = userHasRole(['Sales']);

// Maujooda page ka naam prapt karein
$current_page = basename($_SERVER['PHP_SELF']);

// Active menu ke liye check karein
$is_task_tool_page = in_array($current_page, ['my_tasks.php', 'master_task_flow.php', 'smm_workspace.php', 'review_tasks.php', 'add_additional_task.php']);
$is_management_page = in_array($current_page, ['reports.php', 'manage_ads.php', 'manual_add_completed_task.php', 'manager_review.php', 'skipped_tasks.php']);
$is_admin_page = in_array($current_page, ['dashboard.php', 'employees.php', 'add-employee.php', 'edit-employee.php', 'schedules.php', 'manage-schedule.php', 'edit-schedule.php', 'leave-management.php']);
$is_sales_tool_page = in_array($current_page, ['clients.php', 'add-client.php', 'client-profile.php', 'import-clients.php', 'pending_reports.php', 'invoices.php', 'create-invoice.php', 'view-invoice.php', 'invoice-settings.php']);

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js" defer></script>
    <style>
        :root {
            --sidebar-bg: #1f2937; /* Default Gray */
        }
    </style>
    <script>
        document.addEventListener('alpine:init', () => {
            Alpine.data('themeSwitcher', () => ({
                themes: {
                    default: '#1f2937',
                    blue: '#1e40af',
                    green: '#15803d',
                    purple: '#6b21a8',
                },
                currentTheme: localStorage.getItem('sidebarTheme') || 'default',
                init() {
                    this.applyTheme(this.currentTheme);
                },
                applyTheme(themeName) {
                    const color = this.themes[themeName];
                    document.documentElement.style.setProperty('--sidebar-bg', color);
                    localStorage.setItem('sidebarTheme', themeName);
                    this.currentTheme = themeName;
                }
            }));
        });
    </script>
</head>
<body class="bg-gray-100">

    <?php if (isset($_SESSION['admin_id'])): ?>
        <div class="bg-yellow-400 text-center p-2 font-semibold text-gray-800">
            You are currently logged in as <?php echo htmlspecialchars($_SESSION['user_name']); ?>. 
            <a href="switch_back_to_admin.php" class="underline hover:text-blue-700">Switch back to your Admin account</a>.
        </div>
    <?php endif; ?>

    <div class="flex flex-col md:flex-row h-screen" x-data="{ sidebarOpen: false }">
        
        <div :class="{'translate-x-0': sidebarOpen, '-translate-x-full': !sidebarOpen}" class="w-64 text-white p-4 fixed inset-y-0 left-0 transform md:relative md:translate-x-0 transition-transform duration-200 ease-in-out z-30 flex flex-col h-full" style="background-color: var(--sidebar-bg);">
            <h1 class="text-2xl font-bold mb-4 flex-shrink-0">Digify OS</h1>
            
            <div x-data="themeSwitcher" class="mb-6 text-center">
                <span class="text-xs text-gray-400">Theme:</span>
                <div class="flex justify-center space-x-2 mt-1">
                    <button @click="applyTheme('default')" class="w-5 h-5 rounded-full bg-gray-700 ring-2 ring-offset-2" :class="currentTheme === 'default' ? 'ring-white' : 'ring-transparent'" style="--tw-ring-offset-color: var(--sidebar-bg);"></button>
                    <button @click="applyTheme('blue')" class="w-5 h-5 rounded-full bg-blue-700 ring-2 ring-offset-2" :class="currentTheme === 'blue' ? 'ring-white' : 'ring-transparent'" style="--tw-ring-offset-color: var(--sidebar-bg);"></button>
                    <button @click="applyTheme('green')" class="w-5 h-5 rounded-full bg-green-700 ring-2 ring-offset-2" :class="currentTheme === 'green' ? 'ring-white' : 'ring-transparent'" style="--tw-ring-offset-color: var(--sidebar-bg);"></button>
                    <button @click="applyTheme('purple')" class="w-5 h-5 rounded-full bg-purple-700 ring-2 ring-offset-2" :class="currentTheme === 'purple' ? 'ring-white' : 'ring-transparent'" style="--tw-ring-offset-color: var(--sidebar-bg);"></button>
                </div>
            </div>
            
            <nav class="flex-grow overflow-y-auto" x-data="{ taskMenuOpen: <?php echo $is_task_tool_page ? 'true' : 'false'; ?>, managementMenuOpen: <?php echo $is_management_page ? 'true' : 'false'; ?>, adminMenuOpen: <?php echo $is_admin_page ? 'true' : 'false'; ?>, salesMenuOpen: <?php echo $is_sales_tool_page ? 'true' : 'false'; ?> }">
                
                <a href="employee-profile.php?id=<?php echo $_SESSION['user_id']; ?>" class="flex items-center gap-3 py-2.5 px-4 rounded <?php if($current_page == 'employee-profile.php') echo 'bg-gray-700'; else echo 'hover:bg-gray-700'; ?>">My Profile</a>
                
                <div class="mt-4 pt-4 border-t border-gray-700">
                    <button @click="taskMenuOpen = !taskMenuOpen" class="w-full flex justify-between items-center px-4 py-2 text-left text-sm font-medium text-gray-300 hover:bg-gray-700 rounded-md">
                        <span>Task Tools</span>
                        <svg class="w-4 h-4 transition-transform" :class="{ 'rotate-180': taskMenuOpen }" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path></svg>
                    </button>
                    <div x-show="taskMenuOpen" x-transition class="mt-2 space-y-1 pl-4">
                        <a href="my_tasks.php" class="block py-2 px-4 rounded <?php if($current_page == 'my_tasks.php') echo 'bg-gray-700'; else echo 'hover:bg-gray-700'; ?>">My Tasks</a>
                        <a href="add_additional_task.php" class="block py-2 px-4 rounded <?php if($current_page == 'add_additional_task.php') echo 'bg-gray-700'; else echo 'hover:bg-gray-700'; ?>">Add Additional Task</a>
                        <a href="master_task_flow.php" class="block py-2 px-4 rounded <?php if($current_page == 'master_task_flow.php') echo 'bg-gray-700'; else echo 'hover:bg-gray-700'; ?>">Task Flow</a>
                        <?php if ($is_smm || $is_admin || $is_manager): ?>
                            <a href="review_tasks.php" class="block py-2 px-4 rounded <?php if($current_page == 'review_tasks.php') echo 'bg-gray-700'; else echo 'hover:bg-gray-700'; ?>">Review Tasks</a>
                            <a href="smm_workspace.php" class="block py-2 px-4 rounded <?php if($current_page == 'smm_workspace.php') echo 'bg-gray-700'; else echo 'hover:bg-gray-700'; ?>">SMM Workspace</a>
                        <?php endif; ?>
                    </div>
                </div>

                <?php if ($is_admin || $is_sales): ?>
                    <div class="mt-4 pt-4 border-t border-gray-700">
                        <button @click="salesMenuOpen = !salesMenuOpen" class="w-full flex justify-between items-center px-4 py-2 text-left text-sm font-medium text-gray-300 hover:bg-gray-700 rounded-md">
                            <span>Sales Tools</span>
                            <svg class="w-4 h-4 transition-transform" :class="{ 'rotate-180': salesMenuOpen }" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path></svg>
                        </button>
                        <div x-show="salesMenuOpen" x-transition class="mt-2 space-y-1 pl-4">
                            <a href="clients.php" class="block py-2 px-4 rounded <?php if(in_array($current_page, ['clients.php', 'add-client.php', 'client-profile.php', 'import-clients.php'])) echo 'bg-gray-700'; else echo 'hover:bg-gray-700'; ?>">Clients</a>
                            <a href="invoices.php" class="block py-2 px-4 rounded <?php if(in_array($current_page, ['invoices.php', 'create-invoice.php', 'view-invoice.php'])) echo 'bg-gray-700'; else echo 'hover:bg-gray-700'; ?>">Invoicing</a>
                            <a href="invoice-settings.php" class="block py-2 px-4 rounded <?php if($current_page == 'invoice-settings.php') echo 'bg-gray-700'; else echo 'hover:bg-gray-700'; ?>">Invoice Settings</a>
                        </div>
                    </div>
                <?php endif; ?>

                <?php if ($is_admin || $is_manager): ?>
                    <div class="mt-4 pt-4 border-t border-gray-700">
                        <button @click="managementMenuOpen = !managementMenuOpen" class="w-full flex justify-between items-center px-4 py-2 text-left text-sm font-medium text-gray-300 hover:bg-gray-700 rounded-md">
                            <span>Management Tools</span>
                            <svg class="w-4 h-4 transition-transform" :class="{ 'rotate-180': managementMenuOpen }" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path></svg>
                        </button>
                        <div x-show="managementMenuOpen" x-transition class="mt-2 space-y-1 pl-4">
						<a href="clients.php" class="block py-2 px-4 rounded <?php if(in_array($current_page, ['clients.php', 'client-profile.php'])) echo 'bg-gray-700'; else echo 'hover:bg-gray-700'; ?>">Clients</a>
						<a href="pending_reports.php" class="block py-2 px-4 rounded <?php if($current_page == 'pending_reports.php') echo 'bg-gray-700'; else echo 'hover:bg-gray-700'; ?>">Pending Reports</a>
                            <a href="reports.php" class="block py-2 px-4 rounded <?php if($current_page == 'reports.php') echo 'bg-gray-700'; else echo 'hover:bg-gray-700'; ?>">Reports</a>
                            <a href="manage_ads.php" class="block py-2 px-4 rounded <?php if($current_page == 'manage_ads.php') echo 'bg-gray-700'; else echo 'hover:bg-gray-700'; ?>">Ads Management</a>
                            <a href="manual_task_generator.php" class="block py-2 px-4 rounded <?php if($current_page == 'manual_task_generator.php') echo 'bg-gray-700'; else echo 'hover:bg-gray-700'; ?>">Daily Task Generator</a>
							<a href="assign_manual_tasks.php" class="block py-2 px-4 rounded <?php if($current_page == 'assign_manual_tasks.php') echo 'bg-gray-700'; else echo 'hover:bg-gray-700'; ?>">Advance Tasks Pull</a>
                            <a href="skipped_tasks.php" class="block py-2 px-4 rounded <?php if($current_page == 'skipped_tasks.php') echo 'bg-gray-700'; else echo 'hover:bg-gray-700'; ?>">Skipped Tasks</a>
                            <a href="schedules.php" class="block py-2 px-4 rounded <?php if(in_array($current_page, ['schedules.php', 'manage-schedule.php', 'edit-schedule.php'])) echo 'bg-gray-700'; else echo 'hover:bg-gray-700'; ?>">Manage Schedules</a>
                        </div>
                    </div>
                <?php endif; ?>

                <?php if ($is_admin): ?>
                    <div class="mt-4 pt-4 border-t border-gray-700">
                        <button @click="adminMenuOpen = !adminMenuOpen" class="w-full flex justify-between items-center px-4 py-2 text-left text-sm font-medium text-gray-300 hover:bg-gray-700 rounded-md">
                            <span>Admin Tools</span>
                            <svg class="w-4 h-4 transition-transform" :class="{ 'rotate-180': adminMenuOpen }" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path></svg>
                        </button>
                        <div x-show="adminMenuOpen" x-transition class="mt-2 space-y-1 pl-4">
                            <a href="dashboard.php" class="block py-2 px-4 rounded <?php if($current_page == 'dashboard.php') echo 'bg-gray-700'; else echo 'hover:bg-gray-700'; ?>">Admin Dashboard</a>
                            <a href="employees.php" class="block py-2 px-4 rounded <?php if(in_array($current_page, ['employees.php', 'add-employee.php', 'edit-employee.php'])) echo 'bg-gray-700'; else echo 'hover:bg-gray-700'; ?>">Employees</a>
                            <a href="schedules.php" class="block py-2 px-4 rounded <?php if(in_array($current_page, ['schedules.php', 'manage-schedule.php', 'edit-schedule.php'])) echo 'bg-gray-700'; else echo 'hover:bg-gray-700'; ?>">Manage Schedules</a>
                            <a href="leave-management.php" class="block py-2 px-4 rounded <?php if($current_page == 'leave-management.php') echo 'bg-gray-700'; else echo 'hover:bg-gray-700'; ?>">Leave Management</a>
                            <a href="manual_backup.php" class="block py-2 px-4 rounded <?php if($current_page == 'manual_backup.php') echo 'bg-gray-700'; else echo 'hover:bg-gray-700'; ?>">Backup Generator</a>
                        </div>
                    </div>
                <?php endif; ?>

                 <div class="mt-4 pt-4 border-t border-gray-700">
                    <a href="leave-request.php" class="flex items-center gap-3 py-2.5 px-4 rounded <?php if($current_page == 'leave-request.php') echo 'bg-gray-700'; else echo 'hover:bg-gray-700'; ?>">Leave Request</a>
                    <a href="change-password.php" class="flex items-center gap-3 mt-2 py-2.5 px-4 rounded <?php if($current_page == 'change-password.php') echo 'bg-gray-700'; else echo 'hover:bg-gray-700'; ?>">Change Password</a>
                    <a href="logout.php" class="flex items-center gap-3 mt-2 py-2.5 px-4 rounded hover:bg-gray-700">Logout</a>
                </div>
            </nav>
        </div>
        
        <div class="flex-1 flex flex-col overflow-hidden">
             <header class="bg-white shadow-sm p-4 flex justify-between items-center flex-shrink-0">
                <button class="md:hidden" @click.stop="sidebarOpen = !sidebarOpen">
                    <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16" /></svg>
                </button>
                <div class="text-sm"><?php echo htmlspecialchars($_SESSION['user_name']); ?></div>
            </header>
            
            <div class="flex-1 overflow-x-hidden overflow-y-auto bg-gray-100">
<!-- END OF HEADER, CONTENT STARTS IN INDIVIDUAL FILES -->

