<?php
include 'includes/auth.php';
include 'db.php';
include 'includes/header.php';

$username = $_SESSION['username'] ?? 'Guest';
$today = date('Y-m-d');

// ================= FILTERS =================
$from_date = $_GET['from'] ?? $today;
$to_date   = $_GET['to'] ?? $today;
$status    = $_GET['status'] ?? '';
$search    = $_GET['search'] ?? '';

// ================= BUILD WHERE =================
$where = [];
$where[] = "cb.booking_date >= '".$conn->real_escape_string($from_date)."'";
$where[] = "cb.booking_date <= '".$conn->real_escape_string($to_date)."'";

if($status){
    $where[] = "cb.status = '".$conn->real_escape_string($status)."'";
}
if($search){
    $s = $conn->real_escape_string($search);
    $where[] = "(g.client_name LIKE '%$s%' OR cr.name LIKE '%$s%')";
}

$where_sql = "WHERE ".implode(" AND ", $where);

// ================= FETCH BOOKINGS =================
$query = "
SELECT cb.*, g.name, cr.name AS conference_name
FROM conference_bookings cb
LEFT JOIN conference_rooms cr ON cr.id = cb.conference_room_id
LEFT JOIN guests g ON g.id = cb.guest_id
$where_sql
ORDER BY cb.booking_date DESC
";

$result = $conn->query($query);

// ================= SUMMARY =================
$summary = $conn->query("
SELECT 
    COUNT(*) AS total_bookings,
    SUM(CASE WHEN status='PENDING' THEN 1 ELSE 0 END) AS pending_count,
    SUM(CASE WHEN status='CONFIRMED' THEN 1 ELSE 0 END) AS confirmed_count,
    SUM(CASE WHEN status='COMPLETED' THEN 1 ELSE 0 END) AS completed_count,
    SUM(CASE WHEN status='CANCELLED' THEN 1 ELSE 0 END) AS cancelled_count
FROM conference_bookings cb
$where_sql
")->fetch_assoc();

$total_bookings  = $summary['total_bookings'] ?? 0;
$pending_count   = $summary['pending_count'] ?? 0;
$confirmed_count = $summary['confirmed_count'] ?? 0;
$completed_count = $summary['completed_count'] ?? 0;
$cancelled_count = $summary['cancelled_count'] ?? 0;
?>

<style>
.report-card{border-radius:12px;}
.table thead th{white-space:nowrap;}
.welcome-msg{background:#f8f9fa;padding:8px 12px;border-radius:8px;margin-bottom:10px;}
@media print{.no-print{display:none!important;}}
</style>

<div class="container-fluid mt-4">
    <button class="btn btn-secondary no-print" onclick="history.back()"><i class="fa fa-arrow-left"></i> Back</button>

    <div class="welcome-msg no-print">
        <i class="fa fa-user-circle"></i>
        Welcome, <strong><?= htmlspecialchars($username) ?></strong>
    </div>

    <h3 class="mb-2"><i class="fa fa-users text-primary"></i> Conference Bookings Report</h3>
    <hr>

    <!-- ================= FILTERS ================= -->
    <form method="GET" class="row g-2 mb-3 no-print">
        <div class="col-md-2"><input type="date" name="from" class="form-control" value="<?= htmlspecialchars($from_date) ?>"></div>
        <div class="col-md-2"><input type="date" name="to" class="form-control" value="<?= htmlspecialchars($to_date) ?>"></div>
        <div class="col-md-2">
            <select name="status" class="form-control">
                <option value="">All Statuses</option>
                <option <?= $status=='PENDING'?'selected':'' ?>>PENDING</option>
                <option <?= $status=='CONFIRMED'?'selected':'' ?>>CONFIRMED</option>
                <option <?= $status=='COMPLETED'?'selected':'' ?>>COMPLETED</option>
                <option <?= $status=='CANCELLED'?'selected':'' ?>>CANCELLED</option>
                <option <?= $status=='ONGOING'?'selected':'' ?>>ONGOING</option>
            </select>
        </div>
        <div class="col-md-4"><input type="text" name="search" class="form-control" placeholder="Guest / Conference Room" value="<?= htmlspecialchars($search) ?>"></div>
        <div class="col-md-2 d-flex gap-1">
            <button class="btn btn-primary w-100"><i class="fa fa-filter"></i> Filter</button>
            <a href="conference_report.php" class="btn btn-secondary w-100">Reset</a>
        </div>
    </form>

    <!-- ================= ACTION BUTTONS ================= -->
    <div class="mb-3 no-print d-flex gap-2">
        <a href="export_conference_excel.php?from=<?= urlencode($from_date) ?>&to=<?= urlencode($to_date) ?>&status=<?= urlencode($status) ?>&search=<?= urlencode($search) ?>" class="btn btn-success">
            <i class="fa fa-file-excel"></i> Export Excel
        </a>
        <button onclick="window.print()" class="btn btn-info"><i class="fa fa-print"></i> Print / PDF</button>
    </div>

    <!-- ================= SUMMARY CARDS ================= -->
    <div class="row mb-3 text-center">
        <div class="col-md-2">
            <div class="card report-card shadow-sm">
                <div class="card-body">
                    <h6>Total Bookings</h6>
                    <h4 class="text-primary"><?= $total_bookings ?></h4>
                </div>
            </div>
        </div>
        <div class="col-md-2">
            <div class="card report-card shadow-sm">
                <div class="card-body">
                    <h6>Pending</h6>
                    <h4 class="text-warning"><?= $pending_count ?></h4>
                </div>
            </div>
        </div>
        <div class="col-md-2">
            <div class="card report-card shadow-sm">
                <div class="card-body">
                    <h6>Confirmed</h6>
                    <h4 class="text-success"><?= $confirmed_count ?></h4>
                </div>
            </div>
        </div>
        <div class="col-md-2">
            <div class="card report-card shadow-sm">
                <div class="card-body">
                    <h6>Completed</h6>
                    <h4 class="text-info"><?= $completed_count ?></h4>
                </div>
            </div>
        </div>
        <div class="col-md-2">
            <div class="card report-card shadow-sm">
                <div class="card-body">
                    <h6>Cancelled</h6>
                    <h4 class="text-danger"><?= $cancelled_count ?></h4>
                </div>
            </div>
        </div>
        <div class="col-md-2">
            <div class="card report-card shadow-sm">
                <div class="card-body">
                    <h6>Ongoing</h6>
                    <h4 class="text-secondary"><?= $summary['ongoing_count'] ?? 0 ?></h4>
                </div>
            </div>
        </div>
    </div>

    <!-- ================= TABLE ================= -->
    <div class="table-responsive">
        <table class="table table-bordered table-striped table-sm">
            <thead class="table-dark">
                <tr>
                    <th>ID</th>
                    <th>Guest</th>
                    <th>Conference Room</th>
                    <th>Booking Date</th>
                    <th>Status</th>
                    <th>Total Amount</th>
                    <th>Attendees</th>
                    <th>Room Price</th>
                    
                    <th>Started At</th>
                    <th>End Date</th>
                </tr>
            </thead>
            <tbody>
                <?php if($result && $result->num_rows): ?>
                    <?php while($row = $result->fetch_assoc()): ?>
                        <tr>
                            <td><?= $row['id'] ?></td>
                            <td><?= $row['client_name'] ?? $row['guest_id'] ?></td>
                            <td><?= $row['conference_name'] ?></td>
                            <td><?= $row['booking_date'] ?></td>
                            <td><?= $row['status'] ?></td>
                            <td><?= number_format($row['total_amount'],2) ?></td>
                            <td><?= $row['attendees'] ?></td>
                            <td><?= number_format($row['room_price'],2) ?></td>
                            
                            <td><?= $row['started_at'] ?></td>
                            <td><?= $row['end_date'] ?></td>
                        </tr>
                    <?php endwhile; ?>
                <?php else: ?>
                    <tr><td colspan="11" class="text-center text-danger">No conference bookings found</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<?php include 'includes/footer.php'; ?>