<?php 
$company_id = $_SESSION['company_id'] ?? 0;
$company = fetch("SELECT name, logo FROM companies WHERE id = ?", [$company_id]) ?: ['name' => 'NexCore', 'logo' => ''];
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $company['name'] ?? 'NexCore'; ?> - Apparel Management</title>
    <link rel="stylesheet" href="assets/css/style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <script>
        window.APP_CONFIG = {
            BASE_URL: '<?php echo BASE_URL; ?>',
            BASE_PATH: '<?php echo dirname($_SERVER['SCRIPT_NAME']) === DIRECTORY_SEPARATOR ? '/' : dirname($_SERVER['SCRIPT_NAME']) . '/'; ?>'
        };
    </script>
</head>
<body>
    <div class="sidebar-overlay" id="sidebarOverlay" onclick="toggleSidebar()"></div>
    <div class="app-container">
        <!-- Sidebar -->
        <aside class="sidebar" id="sidebar">
            <a href="<?php echo url(); ?>" class="logo-section" style="text-decoration: none; color: inherit; display: flex; align-items: center; gap: 0.75rem; width: 100%;">
                <div class="logo-icon" style="<?php echo ($company['logo'] ?? '') ? 'background: none; box-shadow: none;' : ''; ?>">
                    <?php if ($company['logo'] ?? ''): ?>
                        <img src="<?php echo $company['logo'] ?? ''; ?>" style="width: 100%; height: 100%; object-fit: contain; border-radius: 8px;">
                    <?php else: ?>
                        <i class="fas fa-layer-group text-white"></i>
                    <?php endif; ?>
                </div>
                <span class="logo-text"><?php echo $company['name'] ?? 'NexCore'; ?></span>
                <button class="collapse-toggle" onclick="event.preventDefault(); toggleSidebarCollapse()" title="Toggle Navigation" style="margin-left: auto;">
                    <i class="fas fa-bars" id="collapseIcon"></i>
                </button>
            </a>
            
            <nav class="nav-links">
                <?php if (hasRole('superadmin')): ?>
                <li style="padding: 1.5rem 1rem 0.5rem; font-size: 0.7rem; font-weight: 700; color: var(--text-muted); text-transform: uppercase; letter-spacing: 1px;">
                    System Administration
                </li>
                <?php 
                $admin_tables = [
                    'users' => ['icon' => 'fa-users', 'label' => 'Users'],
                    'roles' => ['icon' => 'fa-user-tag', 'label' => 'Roles'],
                    'dashboards' => ['icon' => 'fa-tachometer-alt', 'label' => 'Dashboards'],
                    'workflow_stages' => ['icon' => 'fa-project-diagram', 'label' => 'Workflow Stages'],
                    'workflow_statuses' => ['icon' => 'fa-stream', 'label' => 'Workflow Statuses'],
                    'task_statuses' => ['icon' => 'fa-tasks', 'label' => 'Task Statuses'],
                    'skills' => ['icon' => 'fa-star', 'label' => 'Skills'],
                    'units' => ['icon' => 'fa-ruler-combined', 'label' => 'Units'],
                    'companies' => ['icon' => 'fa-building', 'label' => 'Companies']
                ];
                $current_tab = $_GET['table'] ?? 'users';
                foreach ($admin_tables as $tbl => $meta): 
                ?>
                <li class="nav-item">
                    <a href="<?php echo url('superadmin', ['table' => $tbl]); ?>" class="nav-link <?php echo ($page === 'superadmin' && $current_tab === $tbl) ? 'active' : ''; ?>">
                        <i class="fas <?php echo $meta['icon']; ?>"></i>
                        <span><?php echo $meta['label']; ?></span>
                    </a>
                </li>
                <?php endforeach; ?>
                <li class="nav-item">
                    <a href="<?php echo url('settings'); ?>" class="nav-link <?php echo ($page === 'settings') ? 'active' : ''; ?>">
                        <i class="fas fa-cog"></i>
                        <span>Settings</span>
                    </a>
                </li>
                <?php endif; ?>
                <?php if (hasRole(['owner', 'admin'])): ?>
                <li class="nav-item">
                    <a href="<?php echo url('dashboard'); ?>" class="nav-link <?php echo $page === 'dashboard' ? 'active' : ''; ?>">
                        <i class="fas fa-chart-pie"></i>
                        <span>Dashboard</span>
                    </a>
                </li>
                <?php endif; ?>
                
                <?php if (hasRole('admin')): ?>
                <li class="nav-item">
                    <a href="<?php echo url('orders'); ?>" class="nav-link <?php echo $page === 'orders' ? 'active' : ''; ?>">
                        <i class="fas fa-file-invoice"></i>
                        <span>Orders</span>
                    </a>
                </li>
                <?php endif; ?>

                <?php if (hasRole(['owner', 'admin'])): ?>
                <li class="nav-item">
                    <a href="<?php echo url('company-settings'); ?>" class="nav-link <?php echo $page === 'company-settings' ? 'active' : ''; ?>">
                        <i class="fas fa-building-circle-check"></i>
                        <span>Company Profile</span>
                    </a>
                </li>
                <li class="nav-item">
                    <a href="<?php echo url('branches'); ?>" class="nav-link <?php echo $page === 'branches' ? 'active' : ''; ?>">
                        <i class="fas fa-code-branch"></i>
                        <span>Branches</span>
                    </a>
                </li>
                <li class="nav-item">
                    <div class="nav-dropdown <?php echo in_array($page, ['employees']) ? 'active' : ''; ?>">
                        <div class="nav-link dropdown-toggle" onclick="this.parentElement.classList.toggle('active')">
                            <i class="fas fa-layer-group"></i>
                            <span>Management Modules</span>
                            <i class="fas fa-chevron-down ms-auto" style="font-size: 0.75rem; margin-left: auto;"></i>
                        </div>
                        <ul class="nav-submenu">
                            <li>
                                <a href="<?php echo url('employees'); ?>" class="nav-link <?php echo $page === 'employees' ? 'active' : ''; ?>">
                                    <i class="fas fa-users-viewfinder"></i>
                                    <span>Employees</span>
                                </a>
                            </li>
                        </ul>
                    </div>
                </li>
                <li class="nav-item">
                    <a href="<?php echo url('audit-logs'); ?>" class="nav-link <?php echo $page === 'audit-logs' ? 'active' : ''; ?>">
                        <i class="fas fa-shield-halved"></i>
                        <span>Audit Logs</span>
                    </a>
                </li>
                <?php endif; ?>

                <?php if (hasRole('manager')): ?>
                <li class="nav-item">
                    <a href="<?php echo url('employees'); ?>" class="nav-link <?php echo $page === 'employees' ? 'active' : ''; ?>">
                        <i class="fas fa-users"></i>
                        <span>Employees</span>
                    </a>
                </li>
                <li class="nav-item">
                    <a href="<?php echo url('suppliers'); ?>" class="nav-link <?php echo $page === 'suppliers' ? 'active' : ''; ?>">
                        <i class="fas fa-truck-ramp-box"></i>
                        <span>Supplies</span>
                    </a>
                </li>
                <li class="nav-item">
                    <a href="<?php echo url('buyers'); ?>" class="nav-link <?php echo $page === 'buyers' ? 'active' : ''; ?>">
                        <i class="fas fa-building"></i>
                        <span>Buyers</span>
                    </a>
                </li>
                <li class="nav-item">
                    <a href="<?php echo url('inventory'); ?>" class="nav-link <?php echo $page === 'inventory' ? 'active' : ''; ?>">
                        <i class="fas fa-boxes-stacked"></i>
                        <span>Inventory</span>
                    </a>
                </li>
                <li class="nav-item">
                    <a href="<?php echo url('products'); ?>" class="nav-link <?php echo $page === 'products' ? 'active' : ''; ?>">
                        <i class="fas fa-shirt"></i>
                        <span>Products</span>
                    </a>
                </li>
                <li class="nav-item">
                    <a href="<?php echo url('machinery'); ?>" class="nav-link <?php echo $page === 'machinery' ? 'active' : ''; ?>">
                        <i class="fas fa-gears"></i>
                        <span>Machinery</span>
                    </a>
                </li>
                
                <li style="padding: 1.5rem 1rem 0.5rem; font-size: 0.7rem; font-weight: 700; color: var(--text-muted); text-transform: uppercase; letter-spacing: 1px;">
                    Work Flows
                </li>
                <li class="nav-item">
                    <a href="<?php echo url('production-lines'); ?>" class="nav-link <?php echo $page === 'production-lines' ? 'active' : ''; ?>">
                        <i class="fas fa-industry"></i>
                        <span>Production Line</span>
                    </a>
                </li>
                <?php endif; ?>

                <?php if (hasRole(['supervisor', 'operator'])): ?>
                <li class="nav-item">
                    <a href="<?php echo url('worklist'); ?>" class="nav-link <?php echo $page === 'worklist' ? 'active' : ''; ?>">
                        <i class="fas fa-tasks"></i>
                        <span>My Work List</span>
                    </a>
                </li>
                <?php endif; ?>
            </nav>


        </aside>

        <!-- Main Content Area -->
        <main class="main-content" id="mainContent">
            <header class="top-header">
                <div style="display: flex; align-items: center; gap: 1rem;">
                    <button class="menu-toggle" onclick="toggleSidebar()">
                        <i class="fas fa-bars"></i>
                    </button>
                    <div class="page-title">
                        <h1><?php echo ucfirst(str_replace('-', ' ', $page)); ?></h1>
                        <p>Welcome back, <?php echo $_SESSION['user_name']; ?>. Here's what's happening.</p>
                    </div>
                </div>
                
                <div class="user-profile">
                    <div class="user-info">
                        <div style="font-weight: 600; font-size: 0.875rem;"><?php echo $_SESSION['user_name']; ?></div>
                        <div style="font-size: 0.75rem; color: var(--text-muted);"><?php echo strtoupper($_SESSION['user_role']); ?></div>
                    </div>
                    <div class="user-avatar">
                        <?php echo strtoupper(substr($_SESSION['user_name'], 0, 1)); ?>
                    </div>
                    <a href="<?php echo url('logout'); ?>" class="logout-btn" title="Logout">
                        <i class="fas fa-sign-out-alt"></i>
                    </a>
                </div>
            </header>
            <div class="animate-fade-in">
