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

$vid = $_SESSION['user_id'];

if ($myHall) {
    $hid = $myHall['id'];
    $totalDishes = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as c FROM dishes WHERE hall_id=$hid"))['c'];
    $totalDates = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as c FROM hall_dates WHERE hall_id=$hid"))['c'];
    $bookedDates = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as c FROM hall_dates WHERE hall_id=$hid AND status='booked'"))['c'];
    $totalBookings = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as c FROM bookings WHERE hall_id=$hid"))['c'];
    $earnings = mysqli_fetch_assoc(mysqli_query($conn, "SELECT SUM(total_amount) as s FROM bookings WHERE hall_id=$hid AND status='confirmed'"))['s'] ?? 0;
    $galleryCount = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as c FROM hall_gallery WHERE hall_id=$hid"))['c'];
    
    $recentBookings = mysqli_query($conn, "SELECT b.*, u.name as user_name 
                                            FROM bookings b JOIN users u ON b.user_id=u.id 
                                            WHERE b.hall_id=$hid ORDER BY b.created_at DESC LIMIT 5");
}
?>

<div class="welcome-hero">
    <h2>Welcome, <em><?php echo htmlspecialchars($_SESSION['name']); ?></em></h2>
    <p>Manage your shadi hall, menu, and availability from your elegant dashboard.</p>
</div>

<?php showFlash(); ?>

<?php if (!$myHall): ?>
    <div class="dashboard-card text-center" style="padding:60px 40px;">
        <div style="font-size:60px; color:var(--bronze); margin-bottom:15px;">❦</div>
        <h2 class="full">Start Your Journey</h2>
        <p style="color:#666; margin-bottom:25px; font-size:16px;">You haven't added your shadi hall yet. Begin by setting up your hall details.</p>
        <a href="my-hall.php" class="btn">Add Your Hall</a>
    </div>
<?php else: ?>
    <div class="stats-grid">
        <div class="stat-card">
            <h4>Total Dishes</h4>
            <div class="num"><?php echo $totalDishes; ?></div>
        </div>
        <div class="stat-card brown">
            <h4>Gallery Photos</h4>
            <div class="num"><?php echo $galleryCount; ?></div>
        </div>
        <div class="stat-card">
            <h4>Available Dates</h4>
            <div class="num"><?php echo $totalDates - $bookedDates; ?></div>
        </div>
        <div class="stat-card brown">
            <h4>Booked Dates</h4>
            <div class="num"><?php echo $bookedDates; ?></div>
        </div>
        <div class="stat-card">
            <h4>Total Bookings</h4>
            <div class="num"><?php echo $totalBookings; ?></div>
        </div>
        <div class="stat-card brown">
            <h4>Total Earnings</h4>
            <div class="num" style="font-size:24px;">Rs. <?php echo number_format($earnings); ?></div>
        </div>
    </div>
    
    <div class="stats-grid" style="margin-top:30px;">
        <a href="my-hall.php" class="action-card maroon">
            <div class="action-card-inner">
                <div style="font-size:40px; margin-bottom:10px;">🏛️</div>
                <h3>Edit Hall</h3>
                <p>Update your venue details</p>
            </div>
        </a>
        <a href="dishes.php" class="action-card bronze">
            <div class="action-card-inner">
                <div style="font-size:40px; margin-bottom:10px;">🍽️</div>
                <h3>Manage Menu</h3>
                <p>Add dishes and pricing</p>
            </div>
        </a>
        <a href="dates.php" class="action-card maroon">
            <div class="action-card-inner">
                <div style="font-size:40px; margin-bottom:10px;">📅</div>
                <h3>Set Dates</h3>
                <p>Update availability calendar</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>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><strong><?php echo htmlspecialchars($b['user_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;">No bookings yet.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
<?php endif; ?>

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