<?php
include 'includes/auth.php';
include 'db.php';
require_once __DIR__ . '/dompdf/vendor/autoload.php'; // for Composer install
use Dompdf\Dompdf;
use Dompdf\Options;

// ==================== FILTERS ====================
$status_filter = $_GET['status'] ?? '';
$room_filter   = $_GET['room'] ?? '';
$from_date     = $_GET['from_date'] ?? '';
$to_date       = $_GET['to_date'] ?? '';
$guest_filter  = $_GET['guest'] ?? '';

// ==================== BUILD QUERY ====================
$query = "
    SELECT cb.*, g.name AS guest_name, cr.name AS room_name, cr.price AS room_price, 
           i.invoice_number
    FROM conference_bookings cb
    LEFT JOIN guests g ON cb.guest_id = g.id
    LEFT JOIN conference_rooms cr ON cb.conference_room_id = cr.id
    LEFT JOIN invoices i ON cb.id = i.stay_id
    WHERE i.status='Paid'
";

if ($status_filter) $query .= " AND cb.status='". $conn->real_escape_string($status_filter) ."'";
if ($room_filter)   $query .= " AND cb.conference_room_id=". intval($room_filter);
if ($from_date)     $query .= " AND cb.booking_date >= '". $conn->real_escape_string($from_date) ."'";
if ($to_date)       $query .= " AND cb.booking_date <= '". $conn->real_escape_string($to_date) ."'";
if ($guest_filter)  $query .= " AND g.name LIKE '%". $conn->real_escape_string($guest_filter) ."%'";
$query .= " ORDER BY cb.id DESC";

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

// ==================== GENERATE HTML ====================
$html = '<h2 style="text-align:center;">Paid Conference Bookings Report</h2>';
$html .= '<table border="1" cellpadding="6" cellspacing="0" width="100%">';
$html .= '
    <thead>
        <tr style="background:#28a745;color:#fff;">
            <th>#Booking ID</th>
            <th>Client</th>
            <th>Room</th>
            <th>Booking Date</th>
            <th>Total Amount (MK)</th>
            <th>Invoice #</th>
            <th>Status</th>
        </tr>
    </thead><tbody>';

if ($bookings->num_rows > 0) {
    while ($b = $bookings->fetch_assoc()) {
        $html .= '<tr>
            <td>'. $b['id'] .'</td>
            <td>'. htmlspecialchars($b['guest_name']) .'</td>
            <td>'. htmlspecialchars($b['room_name']).' ('.number_format($b['room_price'],2).')</td>
            <td>'. date('d-m-Y H:i', strtotime($b['booking_date'])) .'</td>
            <td>'. number_format($b['total_amount'],2) .'</td>
            <td>'. htmlspecialchars($b['invoice_number']) .'</td>
            <td>Paid</td>
        </tr>';
    }
} else {
    $html .= '<tr><td colspan="7" style="text-align:center;">No paid bookings found.</td></tr>';
}

$html .= '</tbody></table>';

// ==================== GENERATE PDF ====================
$options = new Options();
$options->set('isRemoteEnabled', true);
$dompdf = new Dompdf($options);

// Load HTML
$dompdf->loadHtml($html);

// Set paper size (A4 portrait)
$dompdf->setPaper('A4', 'portrait');

// Render HTML
$dompdf->render();

// Output PDF
$pdfFilename = 'Paid_Conference_Bookings_'.date('YmdHis').'.pdf';
$dompdf->stream($pdfFilename, ["Attachment" => true]);
exit;