<?php
$pageTitle = "Available Dates";
require_once 'sidebar.php';

if (!$myHall) {
    redirect(BASE_URL.'/vendor/my-hall.php', 'Please add your hall first.', 'info');
}

$hid = $myHall['id'];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $start = $_POST['start_date'];
    $end = $_POST['end_date'] ?: $start;
    
    $s = strtotime($start);
    $e = strtotime($end);
    $added = 0;
    
    if ($s && $e && $e >= $s) {
        while ($s <= $e) {
            $d = date('Y-m-d', $s);
            $ok = mysqli_query($conn, "INSERT IGNORE INTO hall_dates (hall_id, available_date, status) VALUES ($hid, '$d', 'available')");
            if ($ok && mysqli_affected_rows($conn) > 0) $added++;
            $s = strtotime('+1 day', $s);
        }
        redirect(BASE_URL.'/vendor/dates.php', "$added date(s) added to your calendar.");
    } else {
        redirect(BASE_URL.'/vendor/dates.php', 'Invalid date range.', 'danger');
    }
}

if (isset($_GET['delete'])) {
    $id = (int)$_GET['delete'];
    mysqli_query($conn, "DELETE FROM hall_dates WHERE id=$id AND hall_id=$hid AND status='available'");
    redirect(BASE_URL.'/vendor/dates.php', 'Date removed.');
}

$dates = mysqli_query($conn, "SELECT * FROM hall_dates WHERE hall_id=$hid ORDER BY available_date ASC");
?>

<div class="page-header">
    <h1>Available Dates Management</h1>
</div>

<?php showFlash(); ?>

<div class="alert alert-info">
    <div>
        <strong>How it works:</strong> Add the dates when your hall is available for booking. 
        When admin confirms a booking, that date will automatically be marked as "Booked" and disappear from users' view.
    </div>
</div>

<div class="dashboard-card">
    <h2 class="full">📅 Add Available Dates</h2>
    <form method="POST">
        <div class="split-layout">
            <div class="form-group">
                <label>Start Date *</label>
                <input type="date" name="start_date" required min="<?php echo date('Y-m-d'); ?>">
            </div>
            <div class="form-group">
                <label>End Date (optional)</label>
                <input type="date" name="end_date" min="<?php echo date('Y-m-d'); ?>">
                <small style="color:#888; font-size:12px;">Leave empty to add just one date</small>
            </div>
        </div>
        <button type="submit" class="btn">Add Dates to Calendar</button>
    </form>
</div>

<div class="dashboard-card">
    <h2 class="full">Your Calendar (<?php echo mysqli_num_rows($dates); ?> dates)</h2>
    
    <div style="display:flex; gap:15px; margin-bottom:20px;">
        <span><span class="badge badge-available">Available</span></span>
        <span><span class="badge badge-booked">Booked</span></span>
    </div>
    
    <?php if (mysqli_num_rows($dates) > 0): ?>
        <div class="date-grid">
            <?php while ($d = mysqli_fetch_assoc($dates)): ?>
                <div class="date-cell <?php echo $d['status']; ?>" style="position:relative;">
                    <?php echo date('d M', strtotime($d['available_date'])); ?>
                    <small><?php echo date('Y', strtotime($d['available_date'])); ?></small>
                    <?php if ($d['status'] === 'available'): ?>
                        <a href="?delete=<?php echo $d['id']; ?>" 
                           onclick="return confirm('Remove this date?')"
                           style="position:absolute; top:-8px; right:-8px; background:#8b2635; color:white; width:22px; height:22px; border-radius:50%; text-decoration:none; font-size:14px; display:flex; align-items:center; justify-content:center; font-weight:bold;">×</a>
                    <?php endif; ?>
                </div>
            <?php endwhile; ?>
        </div>
    <?php else: ?>
        <div style="text-align:center; padding:40px; color:#888;">
            <div style="font-size:40px; color:var(--bronze); margin-bottom:10px;">📅</div>
            No dates added yet. Add your first available date above!
        </div>
    <?php endif; ?>
</div>

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