<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

require_once __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'database.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'config.php';

$auth = new Auth();
$auth->requireRole(['waiter', 'restaurant_admin', 'restaurant_manager']);

$csrf = new CSRF();
$csrfToken = $csrf->getToken();

$user = $auth->user();
$restaurantId = $auth->restaurantId();
$restaurantName = APP_NAME;
$restaurantCurrency = 'USD';

$db = Database::getInstance();
if ($restaurantId) {
    $stmt = $db->query("SELECT name, logo, currency FROM restaurants WHERE id = ?", [$restaurantId]);
    $restaurant = $stmt->fetch();
    if ($restaurant) {
        $restaurantName = $restaurant['name'];
        $restaurantCurrency = $restaurant['currency'] ?? 'USD';
    }
}

$baseUrl = getBaseUrl();
$currentPage = 'dashboard';
if (isset($_GET['view'])) {
    $view = $_GET['view'];
    if (in_array($view, ['dashboard', 'tables', 'new-order', 'my-orders'])) {
        $currentPage = $view;
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="csrf-token" content="<?php echo sanitize($csrfToken); ?>">
    <title>Waiter Dashboard - <?php echo sanitize($restaurantName); ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="<?php echo $baseUrl; ?>/assets/css/style.css" rel="stylesheet">
    <style>
        .sidebar { background: var(--secondary); }
        .waiter-table-card { cursor: pointer; transition: all 0.2s; border-radius: 12px; padding: 20px; text-align: center; border: 2px solid transparent; }
        .waiter-table-card:hover { transform: translateY(-3px); box-shadow: var(--shadow); }
        .waiter-table-card.status-available { background: #eafaf1; border-color: #27ae60; }
        .waiter-table-card.status-occupied { background: #fef9e7; border-color: #f39c12; }
        .waiter-table-card.status-ordered { background: #ebf5fb; border-color: #3498db; }
        .waiter-table-card.status-preparing { background: #fdedec; border-color: #e74c3c; }
        .waiter-table-card.status-ready { background: #eafaf1; border-color: #27ae60; }
        .waiter-table-card.status-bill_requested { background: #f4ecf7; border-color: #8e44ad; }
        .table-number { font-size: 1.8rem; font-weight: 700; }
        .table-status-badge { font-size: 0.75rem; padding: 3px 10px; border-radius: 12px; }
        .cart-item { display: flex; align-items: center; justify-content: space-between; padding: 10px 0; border-bottom: 1px solid var(--gray-100); }
        .cart-item:last-child { border-bottom: none; }
        .menu-item-card { cursor: pointer; border: 2px solid var(--gray-200); border-radius: 10px; padding: 12px; transition: all 0.2s; }
        .menu-item-card:hover { border-color: var(--primary); background: var(--primary-light); }
        .menu-item-card.selected { border-color: var(--primary); background: var(--primary-light); }
        .category-filter { display: flex; gap: 8px; overflow-x: auto; padding: 8px 0; }
        .category-filter-btn { padding: 6px 16px; border: 1px solid var(--gray-300); border-radius: 20px; background: white; cursor: pointer; white-space: nowrap; font-size: 0.85rem; }
        .category-filter-btn.active { background: var(--primary); color: white; border-color: var(--primary); }
    </style>
</head>
<body>
    <div class="app-wrapper">
        <nav id="sidebar" class="sidebar">
            <div class="sidebar-brand">
                <a href="<?php echo $baseUrl; ?>/waiter/" class="sidebar-brand">
                    <i class="fas fa-utensils"></i>
                    <span><?php echo sanitize(APP_NAME); ?></span>
                </a>
            </div>
            <ul class="sidebar-menu">
                <li class="menu-label">Main</li>
                <li>
                    <a href="<?php echo $baseUrl; ?>/waiter/?view=dashboard" class="<?php echo $currentPage === 'dashboard' ? 'active' : ''; ?>">
                        <i class="fas fa-tachometer-alt"></i><span>Dashboard</span>
                    </a>
                </li>
                <li>
                    <a href="<?php echo $baseUrl; ?>/waiter/?view=tables" class="<?php echo $currentPage === 'tables' ? 'active' : ''; ?>">
                        <i class="fas fa-border-all"></i><span>Table View</span>
                    </a>
                </li>
                <li>
                    <a href="<?php echo $baseUrl; ?>/waiter/?view=new-order" class="<?php echo $currentPage === 'new-order' ? 'active' : ''; ?>">
                        <i class="fas fa-plus-circle"></i><span>New Order</span>
                    </a>
                </li>
                <li>
                    <a href="<?php echo $baseUrl; ?>/waiter/?view=my-orders" class="<?php echo $currentPage === 'my-orders' ? 'active' : ''; ?>">
                        <i class="fas fa-clipboard-list"></i><span>My Orders</span>
                    </a>
                </li>
                <li class="menu-label">Account</li>
                <li>
                    <a href="<?php echo $baseUrl; ?>/logout.php">
                        <i class="fas fa-sign-out-alt"></i><span>Logout</span>
                    </a>
                </li>
            </ul>
        </nav>

        <div id="content" class="content">
            <nav class="navbar navbar-expand-lg navbar-light bg-white shadow-sm mb-3">
                <div class="d-flex align-items-center">
                    <button type="button" id="sidebarToggle" class="btn btn-sm btn-outline-secondary me-2">
                        <i class="fas fa-bars"></i>
                    </button>
                    <h5 class="mb-0 d-none d-md-block">
                        <?php
                        $titles = [
                            'dashboard' => 'Dashboard',
                            'tables' => 'Table View',
                            'new-order' => 'Create New Order',
                            'my-orders' => 'My Orders',
                        ];
                        echo sanitize($titles[$currentPage] ?? 'Dashboard');
                        ?>
                    </h5>
                </div>
                <div class="ms-auto d-flex align-items-center">
                    <span class="me-3 text-muted d-none d-md-block">
                        <i class="fas fa-user me-1"></i><?php echo sanitize($user['name']); ?>
                    </span>
                    <a href="<?php echo $baseUrl; ?>/logout.php" class="btn btn-sm btn-outline-danger">
                        <i class="fas fa-sign-out-alt"></i>
                    </a>
                </div>
            </nav>
            <div class="container-fluid">
