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

// ================= LOGGED-IN USER =================
$username = $_SESSION['username'] ?? 'Guest';

// ================= DASHBOARD COUNTS =================
$total_rooms   = $conn->query("SELECT COUNT(*) AS t FROM rooms")->fetch_assoc()['t'] ?? 0;
$occupied      = $conn->query("SELECT COUNT(*) AS t FROM rooms WHERE status='Occupied'")->fetch_assoc()['t'] ?? 0;
$available     = $conn->query("SELECT COUNT(*) AS t FROM rooms WHERE status='Available'")->fetch_assoc()['t'] ?? 0;

// ================= TODAY'S REVENUE =================
// Total revenue today from payments
$payments_total = $conn->query("
    SELECT IFNULL(SUM(amount),0) AS total
    FROM payments
    WHERE DATE(payment_date) = CURDATE()
")->fetch_assoc()['total'] ?? 0;

// Total revenue today from sales
$sales_total = $conn->query("
    SELECT IFNULL(SUM(total),0) AS total
    FROM sales
    WHERE DATE(sale_date) = CURDATE()
")->fetch_assoc()['total'] ?? 0;

// Combined total
$today_total = $payments_total + $sales_total;

// Breakdown by type
$today_breakdown = [];

// Payments by type
$payments_breakdown_res = $conn->query("
    SELECT type, IFNULL(SUM(amount),0) AS total
    FROM payments
    WHERE DATE(payment_date) = CURDATE()
    GROUP BY type
");
while($row = $payments_breakdown_res->fetch_assoc()){
    $today_breakdown['Payment - '.$row['type']] = $row['total'];
}

// Sales total (without category)
if($sales_total > 0){
    $today_breakdown['Sales'] = $sales_total;
}


// ================= PENDING APPROVALS FOR ADMIN =================
$pending_approvals = 0;
if($user_role === 'admin'){
    $pending_approvals = $conn->query("
        SELECT COUNT(*) AS t 
        FROM bar_requisitions  
        WHERE status='Pending'
    ")->fetch_assoc()['t'] ?? 0;
}
?>

<style>
.card-module {
    border-radius: 10px;
    transition: 0.2s;
    cursor: pointer;
    font-size: 0.9rem;
}
.card-module:hover {
    transform: translateY(-3px);
    box-shadow: 0 6px 15px rgba(0,0,0,0.12);
}
.icon-box {
    font-size: 20px;
    width: 50px;
    height: 50px;
    display:flex;
    align-items:center;
    justify-content:center;
    border-radius: 10px;
    color: #fff;
}
.card-body h6 {
    margin-bottom: 0.5rem;
    font-size: 0.85rem;
}
.card-body h3 {
    margin: 0;
    font-size: 1.3rem;
}
.welcome-msg {
    font-size: 1.1rem;
    margin-bottom: 15px;
    color: #1b4d32;
}
/* ===================== NOTIFICATION BELL ===================== */
#notification-bell {
    position: fixed;
    top: 100px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 34px;
    color: #ff0000;
    cursor: pointer;
    z-index: 9999;
    animation: bell-shake 1.2s infinite;
}

/* Red notification badge */
#notification-bell .badge {
    position: absolute;
    top: -8px;
    right: -10px;
    background: #ff0000;
    color: #fff;
    font-size: 12px;
    font-weight: bold;
    padding: 4px 7px;
    border-radius: 50%;
    min-width: 22px;
    text-align: center;
    box-shadow: 0 0 8px rgba(0,0,0,0.4);
}

/* Bell animation */
@keyframes bell-shake {
    0% { transform: translateX(-50%) rotate(0deg); }
    10% { transform: translateX(-50%) rotate(20deg); }
    20% { transform: translateX(-50%) rotate(-15deg); }
    30% { transform: translateX(-50%) rotate(20deg); }
    40% { transform: translateX(-50%) rotate(-10deg); }
    50% { transform: translateX(-50%) rotate(10deg); }
    60% { transform: translateX(-50%) rotate(-5deg); }
    70% { transform: translateX(-50%) rotate(5deg); }
    80% { transform: translateX(-50%) rotate(-3deg); }
    90% { transform: translateX(-50%) rotate(2deg); }
    100% { transform: translateX(-50%) rotate(0deg); }
}

/* Hover effect */
#notification-bell:hover {
    transform: translateX(-50%) scale(1.2);
}
</style>

<div class="container-fluid mt-4">
        <!-- ================= NOTIFICATION BELL (ADMIN ONLY) ================= -->
   <center> <?php if($user_role === 'admin' && $pending_approvals > 0): ?>
        <div id="notification-bell" title="Pending Approvals" onclick="window.location='requisitions.php'">
            <i class="fa fa-bell"></i>
            <span class="badge"><?= $pending_approvals ?></span>
        </div>
    <?php endif; ?></center>

    <!-- ================= WELCOME MESSAGE ================= -->
    <div class="welcome-msg">
        <i class="fa fa-user-circle"></i> Welcome, <strong><?= htmlspecialchars($username) ?></strong>!
    </div>

    <h3 class="mb-3">
        <i class="fa fa-hotel text-success"></i> Front Office Dashboard
    </h3>
    <hr>

    <!-- ================= SUMMARY ================= -->
    <div class="row mb-3 text-center g-2">

        <div class="col-md-2">
            <div class="card shadow-sm">
                <div class="card-body p-2">
                    <h6>Total Rooms</h6>
                    <h3 class="text-primary"><?= $total_rooms ?></h3>
                </div>
            </div>
        </div>

        <div class="col-md-2">
            <div class="card shadow-sm">
                <div class="card-body p-2">
                    <h6>Occupied Rooms</h6>
                    <h3 class="text-danger"><?= $occupied ?></h3>
                </div>
            </div>
        </div>

        <div class="col-md-2">
            <div class="card shadow-sm">
                <div class="card-body p-2">
                    <h6>Available Rooms</h6>
                    <h3 class="text-success"><?= $available ?></h3>
                </div>
            </div>
        </div>

        <div class="col-md-6">
            <div class="card shadow-sm">
                <div class="card-body p-2">
                    <h6>Today's Revenue (MK)</h6>
                    <h3 class="text-dark"><?= number_format($today_total,2) ?></h3>
                    <small>
                        <?php foreach($today_breakdown as $type => $amount): ?>
                            <?= htmlspecialchars($type) ?>: <?= number_format($amount,2) ?> &nbsp;&nbsp;
                        <?php endforeach; ?>
                    </small>
                </div>
            </div>
        </div>

    </div>

    <!-- ================= MODULE CARDS ================= -->
    <div class="row g-2">

        <?php
        function moduleCard($link, $icon, $title, $color='bg-success'){
            echo '
            <div class="col-md-2 mb-2">
                <a href="'.$link.'" class="text-decoration-none">
                    <div class="card card-module shadow-sm">
                        <div class="card-body text-center p-2">
                            <div class="icon-box '.$color.' mx-auto mb-1">
                                <i class="fa '.$icon.'"></i>
                            </div>
                            <h6>'.$title.'</h6>
                        </div>
                    </div>
                </a>
            </div>';
        }

        // Front office modules
           moduleCard('shifts.php','fa-boxes','Shifts','bg-danger');
         moduleCard('guests.php','fa-users','Guests','bg-primary');
        moduleCard('checkin.php','fa-sign-in-alt','Check-in','bg-success');
           moduleCard('guest_account_management.php','fa-sign-in-alt','Guest Account Management','bg-success');
        moduleCard('reservations.php','fa-calendar-check','Bookings','bg-warning');
        moduleCard('room_availability.php','fa-bed','Rooms','bg-info');
          moduleCard('checkouts.php','fa-calendar-check','CheckOuts Done','bg-warning');
        moduleCard('house_keeping.php','fa-user-tag','House Keeping','bg-secondary');
   
        moduleCard('conference_bookings.php','fa-handshake','Conference','bg-warning');
        moduleCard('invoices.php','fa-file-invoice-dollar','Invoices','bg-success');
     moduleCard('combine_invoices.php','fa-file-invoice-dollar','Combine Invoices','bg-info');
          moduleCard('combined_invoices.php','fa-handshake','Combined Invoices','bg-info');
         moduleCard('walk_in_meals.php','fa-file-invoice-dollar','Meal Productions','bg-success');
           moduleCard('quotations.php','fa-file-invoice-dollar','Quotations','bg-success');
             moduleCard('font_desk_requisitions.php','fa-handshake','Admin Requisitions','bg-secondary');
       
        moduleCard('night_audit.php','fa-moon','Night Audit','bg-danger');
     moduleCard('restaurant_menu.php', 'fa-utensils', 'Restaurant Menu', 'bg-primary');       // Menu: utensils icon
moduleCard('breakfast_orders_list.php', 'fa-bread-slice', 'Breakfast Orders', 'bg-success'); // Breakfast: bread slice icon



moduleCard('front_desk_inventory.php', 'fa-boxes', 'Admin Inventory', 'bg-danger');                      // Inventory: boxes icon
                      // Inventory: boxes icon







    moduleCard('manager_reports.php', 'fa-boxes', 'All Reports', 'bg-warning');                      // Inventory: boxes icon
                moduleCard('front_office_reports.php','fa-cutlery','Front Desk Reports','bg-primary');
        ?>

    </div>
<div class="tips-box" align="center">
  <h4>💡Tips</h4>
  <ul>
    <li>✅ <strong></strong> </li>

  </ul>
</div>
</div>

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