<?php
$pageTitle = "Admin Dashboard";
require_once 'sidebar.php';

$totalVendors = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as c FROM users WHERE role='vendor'"))['c'];
$pendingVendors = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as c FROM users WHERE role='vendor' AND status='pending'"))['c'];
$totalUsers = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as c FROM users WHERE role='user'"))['c'];
$totalHalls = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as c FROM halls"))['c'];
$totalBookings = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as c FROM bookings"))['c'];
$pendingBookings = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as c FROM bookings WHERE status='pending'"))['c'];
$unreadMsgs = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as c FROM messages WHERE receiver_id={$_SESSION['user_id']} AND is_read=0"))['c'];
$revenue = mysqli_fetch_assoc(mysqli_query($conn, "SELECT SUM(total_amount) as s FROM bookings WHERE status='confirmed'"))['s'] ?? 0;

$recentBookings = mysqli_query($conn, "SELECT b.*, u.name as user_name, h.hall_name 
                                        FROM bookings b 
                                        JOIN users u ON b.user_id = u.id 
                                        JOIN halls h ON b.hall_id = h.id 
                                        ORDER BY b.created_at DESC LIMIT 5");
?>

<div class="welcome-hero">
    <h2>Welcome, <em><?php echo htmlspecialchars($_SESSION['name']); ?></em></h2>
    <p>Manage the entire ShadiHall platform from this elegant dashboard.</p>
</div>

<?php showFlash(); ?>

<div class="stats-grid">
    <div class="stat-card">
        <h4>Total Vendors</h4>
        <div class="num"><?php echo $totalVendors; ?></div>
        <?php if ($pendingVendors > 0): ?>
            <small style="color:#c0392b; font-weight:600;"><?php echo $pendingVendors; ?> pending approval</small>
        <?php endif; ?>
    </div>
    <div class="stat-card brown">
        <h4>Registered Users</h4>
        <div class="num"><?php echo $totalUsers; ?></div>
    </div>
    <div class="stat-card">
        <h4>Total Halls</h4>
        <div class="num"><?php echo $totalHalls; ?></div>
    </div>
    <div class="stat-card brown">
        <h4>Total Bookings</h4>
        <div class="num"><?php echo $totalBookings; ?></div>
    </div>
    <div class="stat-card">
        <h4>Pending Bookings</h4>
        <div class="num"><?php echo $pendingBookings; ?></div>
    </div>
    <div class="stat-card brown">
        <h4>New Messages</h4>
        <div class="num"><?php echo $unreadMsgs; ?></div>
    </div>
    <div class="stat-card">
        <h4>Total Revenue</h4>
        <div class="num" style="font-size:26px;">Rs. <?php echo number_format($revenue); ?></div>
    </div>
</div>

<!-- Quick Actions -->
<div class="stats-grid" style="margin-top:30px;">
    <a href="vendors.php" class="action-card maroon">
        <div class="action-card-inner">
            <div style="font-size:40px; margin-bottom:10px;">🏪</div>
            <h3>Manage Vendors</h3>
            <p>Approve, block or review vendors</p>
        </div>
    </a>
    <a href="bookings.php" class="action-card bronze">
        <div class="action-card-inner">
            <div style="font-size:40px; margin-bottom:10px;">📋</div>
            <h3>Review Bookings</h3>
            <p>Confirm or cancel booking requests</p>
        </div>
    </a>
    <a href="messages.php" class="action-card maroon">
        <div class="action-card-inner">
            <div style="font-size:40px; margin-bottom:10px;">💬</div>
            <h3>View Messages</h3>
            <p>Reply to user inquiries</p>
        </div>
    </a>
</div>

<div class="dashboard-card" style="margin-top:30px;">
    <div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:20px;">
        <h2 style="margin:0; padding:0; border:none;">Recent Bookings</h2>
        <a href="bookings.php" class="btn btn-sm">View All</a>
    </div>
    
    <div class="table-wrapper">
        <table>
            <thead>
                <tr><th>#</th><th>User</th><th>Hall</th><th>Date</th><th>Amount</th><th>Status</th></tr>
            </thead>
            <tbody>
                <?php if (mysqli_num_rows($recentBookings) > 0): ?>
                    <?php while ($b = mysqli_fetch_assoc($recentBookings)): ?>
                        <tr>
                            <td>#<?php echo $b['id']; ?></td>
                            <td><?php echo htmlspecialchars($b['user_name']); ?></td>
                            <td><?php echo htmlspecialchars($b['hall_name']); ?></td>
                            <td><?php echo date('d M Y', strtotime($b['booking_date'])); ?></td>
                            <td style="color:var(--bronze); font-weight:600;">Rs. <?php echo number_format($b['total_amount']); ?></td>
                            <td><span class="badge badge-<?php echo $b['status']; ?>"><?php echo ucfirst($b['status']); ?></span></td>
                        </tr>
                    <?php endwhile; ?>
                <?php else: ?>
                    <tr><td colspan="6" style="text-align:center; padding:30px; color:#888;">No bookings yet.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

</main></div></body></html>
