<?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(['cashier', '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();
?>
<!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>Cashier 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>
        .payment-method-btn { padding: 12px 20px; border: 2px solid var(--gray-300); border-radius: 10px; background: white; cursor: pointer; text-align: center; transition: all 0.2s; }
        .payment-method-btn:hover { border-color: var(--primary); }
        .payment-method-btn.active { border-color: var(--primary); background: var(--primary-light); color: var(--primary); }
        .payment-method-btn i { font-size: 1.5rem; display: block; margin-bottom: 4px; }
        .bill-summary .bill-row { display: flex; justify-content: space-between; padding: 8px 0; font-size: 0.95rem; }
        .bill-summary .bill-total { border-top: 2px solid var(--gray-800); padding-top: 10px; margin-top: 6px; font-weight: 700; font-size: 1.15rem; }
        .order-search-box { position: relative; }
        .order-search-box input { padding-left: 40px; font-size: 1.1rem; height: 50px; border-radius: 12px; }
        .order-search-box i { position: absolute; left: 14px; top: 50%; transform: translateY(-50%); color: var(--gray-400); font-size: 1.1rem; }
        .pending-order-card { border: 1px solid var(--gray-200); border-radius: 10px; padding: 16px; margin-bottom: 12px; cursor: pointer; transition: all 0.2s; background: white; }
        .pending-order-card:hover { border-color: var(--primary); box-shadow: var(--shadow); transform: translateY(-1px); }
        .pending-order-card.selected { border-color: var(--primary); background: var(--primary-light); }
    </style>
</head>
<body>
    <div class="app-wrapper">
        <nav id="sidebar" class="sidebar">
            <div class="sidebar-brand">
                <a href="<?php echo $baseUrl; ?>/cashier/" 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">Cashier</li>
                <li>
                    <a href="<?php echo $baseUrl; ?>/cashier/" class="active">
                        <i class="fas fa-cash-register"></i><span>Dashboard</span>
                    </a>
                </li>
                <li>
                    <a href="<?php echo $baseUrl; ?>/cashier/#pending-section">
                        <i class="fas fa-hourglass-half"></i><span>Pending Payments</span>
                    </a>
                </li>
                <li>
                    <a href="<?php echo $baseUrl; ?>/cashier/#completed-section">
                        <i class="fas fa-check-double"></i><span>Completed Today</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">Cashier 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">
