<?php require "includes/header.php"; ?>
<?php require "../config/config.php"; ?>

<?php
// Admin session check
if(!isset($_SESSION['admin_username'])){
    header("location: login.php");
    exit;
}

try {
    $total_hotels   = (int) $conn->query("SELECT COUNT(*) FROM hotels")->fetchColumn();
    $total_rooms    = (int) $conn->query("SELECT COUNT(*) FROM rooms")->fetchColumn();
    $total_bookings = (int) $conn->query("SELECT COUNT(*) FROM bookings")->fetchColumn();
    $total_revenue  = (float) $conn->query("SELECT COALESCE(SUM(price),0) FROM bookings WHERE payment_status = 'paid'")->fetchColumn();
    $pending        = (int) $conn->query("SELECT COUNT(*) FROM bookings WHERE payment_status = 'pending'")->fetchColumn();
    $total_users    = (int) $conn->query("SELECT COUNT(*) FROM users")->fetchColumn();
    $recent         = $conn->query("SELECT * FROM bookings ORDER BY id DESC LIMIT 8")->fetchAll(PDO::FETCH_OBJ);
} catch (Exception $e) {
    $total_hotels   = isset($total_hotels) ? $total_hotels : 0;
    $total_rooms    = isset($total_rooms) ? $total_rooms : 0;
    $total_bookings = isset($total_bookings) ? $total_bookings : 0;
    $total_revenue  = 0;
    $pending        = 0;
    $total_users    = isset($total_users) ? $total_users : 0;
    $recent         = [];
}
?>

<!-- Topbar -->
<div class="topbar">
    <h4 class="page-title"><i class="fa fa-tachometer"></i> Dashboard</h4>
    <div class="admin-info">
        <div class="admin-avatar"><?php echo strtoupper(substr($_SESSION['admin_username'],0,1)); ?></div>
        <div>
          <div class="admin-name"><?php echo htmlspecialchars($_SESSION['admin_username']); ?></div>
          <div class="admin-role">Administrator</div>
        </div>
        <a href="logout.php" class="logout-btn"><i class="fa fa-sign-out mr-1"></i>Sign Out</a>
    </div>
</div>

<!-- Main Content -->
<div class="main-content">

    <!-- Stats Row -->
    <div class="row mb-4">
        <div class="col-md-3 mb-3">
            <div class="stat-card card-navy">
                <div class="icon"><i class="fa fa-building"></i></div>
                <div class="stat-label">Properties</div>
                <h3><?php echo $total_hotels; ?></h3>
                <p>Active Hotel Properties</p>
            </div>
        </div>
        <div class="col-md-3 mb-3">
            <div class="stat-card card-green">
                <div class="icon"><i class="fa fa-bed"></i></div>
                <div class="stat-label">Inventory</div>
                <h3><?php echo $total_rooms; ?></h3>
                <p>Guest Rooms Available</p>
            </div>
        </div>
        <div class="col-md-3 mb-3">
            <div class="stat-card card-slate">
                <div class="icon"><i class="fa fa-calendar-check-o"></i></div>
                <div class="stat-label">Reservations</div>
                <h3><?php echo $total_bookings; ?></h3>
                <p>Total Reservations</p>
            </div>
        </div>
        <div class="col-md-3 mb-3">
            <div class="stat-card card-gold">
                <div class="icon"><i class="fa fa-money"></i></div>
                <div class="stat-label">Revenue</div>
                <h3>GH&#8373;<?php echo number_format($total_revenue, 0); ?></h3>
                <p>Total Confirmed Revenue</p>
            </div>
        </div>
    </div>

    <!-- Quick stats row -->
    <div class="row mb-4">
        <div class="col-md-6 mb-3">
            <div style="background:#E8E2D8; border-radius:4px; padding:24px; box-shadow:0 2px 12px rgba(35,35,35,0.07); border:1px solid rgba(35,35,35,0.06); border-left:4px solid #A39382;">
                <div style="font-size:11px; text-transform:uppercase; letter-spacing:1.5px; color:#685D54; font-weight:700; margin-bottom:8px;">Pending Payments</div>
                <div style="font-family:'Cormorant Garamond',serif; font-size:48px; font-weight:700; color:#A39382; line-height:1;"><?php echo $pending; ?></div>
                <a href="bookings/index.php" class="btn-admin-primary d-inline-block mt-3" style="font-size:11px; padding:8px 20px;">View Reservations</a>
            </div>
        </div>
        <div class="col-md-6 mb-3">
            <div style="background:#E8E2D8; border-radius:4px; padding:24px; box-shadow:0 2px 12px rgba(35,35,35,0.07); border:1px solid rgba(35,35,35,0.06); border-left:4px solid #685D54;">
                <div style="font-size:11px; text-transform:uppercase; letter-spacing:1.5px; color:#685D54; font-weight:700; margin-bottom:8px;">Registered Guests</div>
                <div style="font-family:'Cormorant Garamond',serif; font-size:48px; font-weight:700; color:#685D54; line-height:1;"><?php echo $total_users; ?></div>
                <a href="admins/index.php" class="btn-admin-primary d-inline-block mt-3" style="font-size:11px; padding:8px 20px;">Manage Staff</a>
            </div>
        </div>
    </div>

    <!-- Recent Reservations -->
    <div class="admin-table">
        <div class="table-section-header">
            <h5><i class="fa fa-calendar-check-o"></i> Recent Reservations</h5>
            <a href="bookings/index.php" class="btn-admin-primary" style="padding:8px 20px; font-size:11px;">View All</a>
        </div>
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Guest Name</th>
                        <th>Hotel</th>
                        <th>Room</th>
                        <th>Check-In</th>
                        <th>Check-Out</th>
                        <th>Amount</th>
                        <th>Payment Status</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach($recent as $b): ?>
                    <tr>
                        <td style="font-weight:700; color:#0B1D3A;">#<?php echo $b->id; ?></td>
                        <td><strong><?php echo htmlspecialchars($b->full_name); ?></strong></td>
                        <td><?php echo htmlspecialchars($b->hotel_name); ?></td>
                        <td><?php echo htmlspecialchars($b->room_name); ?></td>
                        <td><?php echo htmlspecialchars($b->check_in); ?></td>
                        <td><?php echo htmlspecialchars($b->check_out); ?></td>
                        <td style="font-weight:700; color:#0B1D3A;">GH&#8373;<?php echo number_format($b->price, 2); ?></td>
                        <td>
                            <?php if($b->payment_status === 'paid'): ?>
                                <span class="badge-paid"><i class="fa fa-check mr-1"></i>Confirmed</span>
                            <?php else: ?>
                                <span class="badge-pending"><i class="fa fa-clock-o mr-1"></i>Pending</span>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>

</div><!-- /.main-content -->

<?php require "includes/footer.php"; ?>
