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

$uid = $_SESSION['user_id'];
$totalBookings = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as c FROM bookings WHERE user_id=$uid"))['c'];
$pending = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as c FROM bookings WHERE user_id=$uid AND status='pending'"))['c'];
$confirmed = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as c FROM bookings WHERE user_id=$uid AND status='confirmed'"))['c'];
$unreadMsgs = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as c FROM messages WHERE receiver_id=$uid AND is_read=0"))['c'];

$recent = mysqli_query($conn, "SELECT b.*, h.hall_name, h.main_image FROM bookings b 
                               JOIN halls h ON b.hall_id=h.id 
                               WHERE b.user_id=$uid ORDER BY b.created_at DESC LIMIT 5");
?>

<div class="welcome-hero">
    <h2>Welcome, <em><?php echo htmlspecialchars($_SESSION['name']); ?></em></h2>
    <p>Browse elegant shadi halls and book your dream wedding venue with ease.</p>
</div>

<?php showFlash(); ?>

<div class="stats-grid">
    <div class="stat-card">
        <h4>Total Bookings</h4>
        <div class="num"><?php echo $totalBookings; ?></div>
    </div>
    <div class="stat-card brown">
        <h4>Pending</h4>
        <div class="num"><?php echo $pending; ?></div>
    </div>
    <div class="stat-card">
        <h4>Confirmed</h4>
        <div class="num"><?php echo $confirmed; ?></div>
    </div>
    <div class="stat-card brown">
        <h4>New Messages</h4>
        <div class="num"><?php echo $unreadMsgs; ?></div>
    </div>
</div>

<div class="stats-grid" style="margin-top:30px;">
    <a href="<?php echo BASE_URL; ?>/halls.php" class="action-card maroon">
        <div class="action-card-inner">
            <div style="font-size:40px; margin-bottom:10px;">🏛️</div>
            <h3>Browse Halls</h3>
            <p>Explore all available venues</p>
        </div>
    </a>
    <a href="my-bookings.php" class="action-card bronze">
        <div class="action-card-inner">
            <div style="font-size:40px; margin-bottom:10px;">📋</div>
            <h3>My Bookings</h3>
            <p>View your booking history</p>
        </div>
    </a>
    <a href="contact-admin.php" class="action-card maroon">
        <div class="action-card-inner">
            <div style="font-size:40px; margin-bottom:10px;">💬</div>
            <h3>Contact Admin</h3>
            <p>Send inquiry to admin</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="my-bookings.php" class="btn btn-sm">View All</a>
    </div>
    
    <div class="table-wrapper">
        <table>
            <thead>
                <tr><th>#</th><th>Hall</th><th>Date</th><th>Amount</th><th>Status</th></tr>
            </thead>
            <tbody>
                <?php if (mysqli_num_rows($recent) > 0): ?>
                    <?php while ($b = mysqli_fetch_assoc($recent)): ?>
                        <tr>
                            <td>#<?php echo $b['id']; ?></td>
                            <td>
                                <img src="<?php echo $b['main_image'] ? BASE_URL.'/assets/uploads/halls/'.$b['main_image'] : 'https://via.placeholder.com/50'; ?>" 
                                     style="width:40px; height:35px; object-fit:cover; border-radius:4px; vertical-align:middle; margin-right:10px;">
                                <strong><?php echo htmlspecialchars($b['hall_name']); ?></strong>
                            </td>
                            <td style="font-family:var(--font-display); color:var(--maroon);"><?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="5" style="text-align:center; padding:30px; color:#888;">
                        <div style="font-size:40px; color:var(--bronze); margin-bottom:10px;">❦</div>
                        No bookings yet. <a href="<?php echo BASE_URL; ?>/halls.php" style="color:var(--maroon); font-weight:600;">Browse halls</a> to get started.
                    </td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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