<?php
include 'includes/auth.php';
include 'db.php';

// ================= DASHBOARD CONFIG =================
$dashboard_title = 'Restaurant Dashboard';
$dashboard_icon  = 'fa-cutlery';

// ================= NOTIFICATIONS =================

// 🔔 Unpaid restaurant orders
$unpaid_orders = $conn->query("
    SELECT COUNT(*) AS t 
    FROM restaurant_orders 
    WHERE status != 'PAID'
")->fetch_assoc()['t'] ?? 0;

// 🥞 Pending breakfast orders (NEW like your breakfast page)
$pending_breakfast = $conn->query("
    SELECT COUNT(*) AS t
    FROM stays s
    JOIN guests g ON s.guest_id = g.id
    JOIN rooms r ON s.room_id = r.id
    WHERE 
        s.checkout_date IS NULL
        AND s.breakfast_prepared = 0
        AND (g.name IS NULL OR g.name <> 'Walk-in')
")->fetch_assoc()['t'] ?? 0;


// ================= MODULE CARDS =================
$module_cards = [

      ['link'=>'restaurant_shift.php', 'icon'=>'fa-chart-line', 'title'=>'Shifts', 'color'=>'bg-danger'],
    ['link'=>'restaurant_pos.php', 'icon'=>'fa-cash-register', 'title'=>'POS', 'color'=>'bg-success'],

    ['link'=>'restaurant_menu.php', 'icon'=>'fa-utensils', 'title'=>'Restaurant Menu', 'color'=>'bg-success'],

    ['link'=>'restaurant_sales_list.php', 'icon'=>'fa-receipt', 'title'=>'Restaurant Sales', 'color'=>'bg-success'],

    ['link'=>'meal_dashboard.php', 'icon'=>'fa-concierge-bell', 'title'=>'Assign Ingredients', 'color'=>'bg-success'],

    ['link'=>'produce_meal.php', 'icon'=>'fa-concierge-bell', 'title'=>'Produce Meals', 'color'=>'bg-success'],

    // 🥞 FIXED BREAKFAST WITH BADGE
    [
        'link'=>'breakfast_orders_list.php',
        'icon'=>'fa-bread-slice',
        'title'=>'Breakfast Orders',
        'color'=>'bg-success',
        'notify' => $pending_breakfast
    ],

    // 🔴 RESTAURANT ORDERS
    [
        'link'=>'restaurant_orders_list.php',
        'icon'=>'fa-shopping-basket',
        'title'=>'Restaurant Orders',
        'color'=>'bg-primary',
        'notify' => $unpaid_orders
    ],

    ['link'=>'guest_account_management.php', 'icon'=>'fa-users', 'title'=>'Guest Accounts', 'color'=>'bg-primary'],

    ['link'=>'restaurant_inventory.php', 'icon'=>'fa-warehouse', 'title'=>'Restaurant Requisitions', 'color'=>'bg-dark'],

    ['link'=>'rest_rep.php', 'icon'=>'fa-user-shield', 'title'=>'Requisition Reports', 'color'=>'bg-danger'],

    ['link'=>'restaurant_reports.php', 'icon'=>'fa-chart-line', 'title'=>'Reports', 'color'=>'bg-dark'],
];

include 'includes/header.php';
?>

<style>
.card-module {
    width: 160px;
    text-align: center;
    display: inline-block;
    border-radius: 12px;
    transition: 0.2s;
    position: relative;
    background: #fff;
}

.card-module:hover {
    transform: translateY(-3px);
    box-shadow: 0 6px 15px rgba(0,0,0,0.12);
}

.card-module a {
    text-decoration: none;
    color: inherit;
}

.icon-box {
    font-size: 28px;
    width: 60px;
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    margin: 0 auto 10px auto;
    color: #fff;
}

/* layout */
.flex-center {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    gap: 20px;
}

/* 🔴 NOTIFICATION BADGE (fixed like orders system) */
.notify-badge {
    position: absolute;
    top: 8px;
    right: 10px;
    background: #dc3545;
    color: white;
    font-size: 12px;
    padding: 3px 8px;
    border-radius: 20px;
    font-weight: bold;
}
</style>

<div class="container-fluid mt-4">

<button class="btn btn-secondary" onclick="history.back()">
    <i class="fa fa-arrow-left"></i> Back
</button>

<div class="welcome-msg mt-2">
    <i class="fa fa-user-circle"></i> Welcome,
    <strong><?= htmlspecialchars($_SESSION['username'] ?? 'Guest') ?></strong>!
</div>

<h3 class="mb-3">
    <i class="fa <?= $dashboard_icon ?> text-success"></i>
    <?= $dashboard_title ?>
</h3>

<hr>

<div class="flex-center">

<?php foreach($module_cards as $card): ?>

    <div class="card card-module shadow-sm">

        <?php if(!empty($card['notify']) && $card['notify'] > 0): ?>
            <div class="notify-badge">
                <?= $card['notify'] ?>
            </div>
        <?php endif; ?>

        <a href="<?= $card['link'] ?>">
            <div class="card-body p-2">

                <div class="icon-box <?= $card['color'] ?>">
                    <i class="fa <?= $card['icon'] ?>"></i>
                </div>

                <h6><?= $card['title'] ?></h6>

            </div>
        </a>

    </div>

<?php endforeach; ?>

</div>

</div>

<?php include 'includes/footer.php'; ?>