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

$booking_id = intval($_GET['booking_id'] ?? 0);
if (!$booking_id) {
    echo "<div class='alert alert-danger'>Invalid Booking</div>";
    exit;
}

// ================= FETCH BOOKING =================
$booking = $conn->query("SELECT * FROM conference_bookings WHERE id=$booking_id")->fetch_assoc();
if (!$booking) {
    echo "<div class='alert alert-danger'>Booking not found</div>";
    exit;
}

// ================= FETCH ASSIGNED ITEMS =================
$food_items = $conn->query("
    SELECT cbr.resource_type, rm.name, rm.price
    FROM conference_booking_resources cbr
    JOIN restaurant_menu rm ON cbr.resource_id = rm.id
    WHERE cbr.booking_id=$booking_id AND cbr.resource_type='FOOD'
");

$other_resources = $conn->query("
    SELECT cbr.resource_type, cr.resource_name, cr.unit_cost
    FROM conference_booking_resources cbr
    JOIN conference_resources cr ON cbr.resource_id = cr.id
    WHERE cbr.booking_id=$booking_id AND cbr.resource_type='OTHER'
");

$rooms = $conn->query("
    SELECT r.room_type, r.price, r.capacity
    FROM conference_booking_accommodation cba
    JOIN rooms r ON cba.room_id = r.id
    WHERE cba.booking_id=$booking_id
");

// ================= TOTAL CALCULATION =================
$total_food = 0;
$food_list = [];
while ($f = $food_items->fetch_assoc()) {
    $food_list[] = $f;
    $total_food += $f['price'];
}

$total_resources = 0;
$resources_list = [];
while ($r = $other_resources->fetch_assoc()) {
    $resources_list[] = $r;
    $total_resources += $r['unit_cost'];
}

$total_rooms = 0;
$rooms_list = [];
while ($rm = $rooms->fetch_assoc()) {
    $rooms_list[] = $rm;
    $total_rooms += $rm['price'];
}

$total_amount = ($booking['num_days'] * $booking['room_price']) + $total_food + $total_resources + $total_rooms;

// ================= LOGO & QR =================
$logo = "assets/logo.png";
$qr_data = urlencode("Booking #$booking_id | Total: $total_amount");
$qr_url = "https://api.qrserver.com/v1/create-qr-code/?size=120x120&data=$qr_data";
?>

<style>
.invoice-box {
    background: #fff;
    padding: 25px;
    border-radius: 10px;
    font-size: 14px;
    position: relative;
}
.header-flex {
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.table td, .table th { vertical-align: middle; }
.no-print { margin-top: 15px; }
@media print { .no-print { display: none; } }
.type-header { background: #f0f0f0; font-weight: bold; }
</style>

<div class="container mt-3 invoice-box">
    <!-- HEADER -->
    <div class="header-flex mb-3">
        <div><img src="<?= $logo ?>" style="height:80px;"></div>
        <div class="text-center">
            <h3 style="margin:0;">QUOTATION</h3>
            <strong>Conference Services</strong><br>
            Attendees: <?= $booking['attendees'] ?><br>
            Number of Days: <?= $booking['num_days'] ?><br>
        </div>
        <div class="text-end"><img src="<?= $qr_url ?>"></div>
    </div>
    <hr>

    <!-- BOOKING INFO -->
    <div class="row mb-3">
        <div class="col-md-6">
            <strong>Client:</strong> <?= htmlspecialchars($booking['client_name']) ?><br>
            <strong>Date & Time:</strong> <?= date('d/m/Y H:i', strtotime($booking['booking_date'])) ?><br>
        </div>
        <div class="col-md-6 text-end">
            <strong>Room Price/Day:</strong> <?= number_format($booking['room_price'],2) ?><br>
            <strong>Total Amount:</strong> <?= number_format($total_amount,2) ?>
        </div>
    </div>

    <!-- FOOD ITEMS -->
    <?php if(count($food_list) > 0): ?>
    <h5>🍳 Food Items</h5>
    <table class="table table-bordered">
        <thead class="table-light">
            <tr><th>#</th><th>Name</th><th>Price (MWK)</th></tr>
        </thead>
        <tbody>
            <?php $count=1; foreach($food_list as $f): ?>
            <tr>
                <td><?= $count++ ?></td>
                <td><?= htmlspecialchars($f['name']) ?></td>
                <td><?= number_format($f['price'],2) ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <?php endif; ?>

    <!-- OTHER RESOURCES -->
    <?php if(count($resources_list) > 0): ?>
    <h5>📦 Other Resources</h5>
    <table class="table table-bordered">
        <thead class="table-light">
            <tr><th>#</th><th>Name</th><th>Cost (MWK)</th></tr>
        </thead>
        <tbody>
            <?php $count=1; foreach($resources_list as $r): ?>
            <tr>
                <td><?= $count++ ?></td>
                <td><?= htmlspecialchars($r['resource_name']) ?></td>
                <td><?= number_format($r['unit_cost'],2) ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <?php endif; ?>

    <!-- ACCOMMODATION -->
    <?php if(count($rooms_list) > 0): ?>
    <h5>🛏️ Accommodation Rooms</h5>
    <table class="table table-bordered">
        <thead class="table-light">
            <tr><th>#</th><th>Room Type</th><th>Capacity</th><th>Price (MWK)</th></tr>
        </thead>
        <tbody>
            <?php $count=1; foreach($rooms_list as $rm): ?>
            <tr>
                <td><?= $count++ ?></td>
                <td><?= htmlspecialchars($rm['room_type']) ?></td>
                <td><?= $rm['capacity'] ?></td>
                <td><?= number_format($rm['price'],2) ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <?php endif; ?>

    <hr>
    <div>
        <strong>OUR BANKING DETAILS</strong><br>
        ACCOUNT NAME: CONFERENCE CENTER<br>
        ACCOUNT NUMBER: 1003010771<br>
        BANK: NATIONAL BANK OF MALAWI<br>
        Capital City Branch
    </div>

    <!-- ACTION BUTTONS -->
    <div class="no-print mt-3">
        <button class="btn btn-dark" onclick="window.print()"><i class="fa fa-print"></i> Print</button>
        <a href="conference_bookings.php" class="btn btn-secondary"><i class="fa fa-arrow-left"></i> Back</a>
    </div>
</div>

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