<?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
$today_total = $conn->query("
    SELECT IFNULL(SUM(amount),0) AS total
    FROM payments
    WHERE DATE(payment_date) = CURDATE()
")->fetch_assoc()['total'] ?? 0;

// Breakdown by payment type (room, restaurant, conference, bar, other)
$today_breakdown_res = $conn->query("
    SELECT type, IFNULL(SUM(amount),0) AS total
    FROM payments
    WHERE DATE(payment_date) = CURDATE()
    GROUP BY type
");
$today_breakdown = [];
while($row = $today_breakdown_res->fetch_assoc()){
    $today_breakdown[$row['type']] = $row['total'];
}
?>

<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;
}
</style>

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

    <!-- ================= 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): ?>
                            <?= ucfirst($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">
                <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('checkin.php','fa-sign-in-alt','Check-in','bg-success');
        moduleCard('guests.php','fa-users','Guests','bg-primary');
        moduleCard('reservations.php','fa-calendar-check','Bookings','bg-warning');
        moduleCard('room_availability.php','fa-bed','Rooms','bg-info');
        moduleCard('guest_accounts.php','fa-user-tag','Accounts','bg-secondary');
        moduleCard('payments.php','fa-money-bill-wave','Payments','bg-success');
        moduleCard('debtors_creditors.php','fa-balance-scale','Debtors','bg-danger');
        moduleCard('customer_statements.php','fa-file-invoice','Statements','bg-dark');
        moduleCard('cashier_summary.php','fa-cash-register','Cashier','bg-primary');
        moduleCard('guest_history.php','fa-history','History','bg-warning');
        moduleCard('conference_bookings.php','fa-handshake','Conference','bg-warning');
        moduleCard('invoices.php','fa-file-invoice-dollar','Invoices','bg-success');
        moduleCard('customer_bills.php','fa-receipt','Bills','bg-info');
        moduleCard('night_audit.php','fa-moon','Night Audit','bg-danger');
        moduleCard('restaurant_menu.php','fa-cutlery','Restaurant Menu','bg-danger');
        ?>

    </div>

</div>

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