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

$booking_id = intval($_GET['booking_id'] ?? 0);

$booking = $conn->query("
    SELECT * 
    FROM conference_bookings 
    WHERE id=$booking_id
")->fetch_assoc();

if(!$booking){
    die("<div class='alert alert-danger'>Booking not found</div>");
}

$attendees = intval($booking['attendees']);
$num_days  = intval($booking['num_days']);

/* =========================================
   ACCOMMODATION
========================================= */
$total_room_discount = 0;
$total_accommodation = 0;

$rooms_stmt = $conn->prepare("
    SELECT 
        r.price,
        cba.discount
    FROM conference_booking_accommodation cba
    JOIN rooms r ON cba.room_id=r.id
    WHERE cba.booking_id=?
");

$rooms_stmt->bind_param("i",$booking_id);
$rooms_stmt->execute();

$rooms_result = $rooms_stmt->get_result();

while($r = $rooms_result->fetch_assoc()){

    $room_price_total = $r['price'] * $num_days;

    $discount = floatval($r['discount']);

    $total_room_discount += $discount;

    $total_accommodation += max(
        $room_price_total - $discount,
        0
    );
}

/* =========================================
   MEALS
========================================= */
$total_meals = 0;

$res_stmt = $conn->prepare("
    SELECT cm.price AS meal_price
    FROM conference_booking_resources cbr
    JOIN conference_meals cm 
        ON cbr.resource_id=cm.id
    WHERE cbr.booking_id=?
    AND cbr.resource_type='FOOD'
");

$res_stmt->bind_param("i",$booking_id);
$res_stmt->execute();

$res_result = $res_stmt->get_result();

while($row = $res_result->fetch_assoc()){

    $total_meals += (
        $row['meal_price']
        * $attendees
        * $num_days
    );
}

/* =========================================
   REFRESHMENTS
========================================= */
$total_refreshments = 0;

$res_stmt = $conn->prepare("
    SELECT crf.price AS refresh_price
    FROM conference_booking_resources cbr
    JOIN conference_refreshments crf
        ON cbr.resource_id=crf.id
    WHERE cbr.booking_id=?
    AND cbr.resource_type='REFRESHMENT'
");

$res_stmt->bind_param("i",$booking_id);
$res_stmt->execute();

$res_result = $res_stmt->get_result();

while($row = $res_result->fetch_assoc()){

    $total_refreshments += (
        $row['refresh_price']
        * $attendees
        * $num_days
    );
}

/* =========================================
   CONFERENCE ROOM
========================================= */
$conference_total = $booking['room_price'] * $num_days;

/* =========================================
   TOTALS
========================================= */
$total_booking_discount = floatval($booking['total_discount']);

$subtotal =
    $total_meals +
    $total_refreshments +
    $total_accommodation +
    $conference_total;

$subtotal -= $total_booking_discount;

$vat  = $subtotal * 0.175;
$levy = $subtotal * 0.01;

$grand_total = $subtotal + $vat + $levy;

/* =========================================
   QR CODE
========================================= */
$qr_data = urlencode(
    "Conference Quote #".$booking_id.
    " Total MWK ".$grand_total
);

$qr_url = "https://api.qrserver.com/v1/create-qr-code/?size=120x120&data=".$qr_data;

/* =========================================
   LOGO
========================================= */
$logo = "assets/logo.png";
?>

<style>

.invoice-box{
    background:#fff;
    margin:0 auto;
    padding:-80mm 10mm 10mm 10mm; /* top reduced */
    box-sizing:border-box;
    position:relative;
    font-size:13px;
}

/* WATERMARK */
.watermark{
    position:absolute;
    top:50%;
    left:50%;
    transform:translate(-50%, -50%) rotate(-30deg);
    font-size:90px;
    font-weight:700;
    color:rgba(10,127,46,0.06);
    letter-spacing:5px;
    z-index:0;
    pointer-events:none;
    white-space:nowrap;
}

.invoice-box *{
    position:relative;
    z-index:1;
}

/* LOGO */
.logo{
    height:160px;
    width:auto;
    margin-bottom:5px;
}

/* HEADER */
.cert-header{
    text-align:center;
}

.cert-title{
    font-size:28px;
    font-weight:bold;
    color:#0a7f2e;
    margin-top:10px;
}

.header-info{
    display:flex;
    justify-content:space-between;
    flex-wrap:wrap;
    margin-top:10px;
}

.header-left,
.header-right{
    font-size:13px;
    text-align:left;
}

/* TABLES */
.table{
    width:100%;
    border-collapse:collapse;
    font-size:13px;
}

.table th,
.table td{
    border:1px solid #dcdcdc !important;
    padding:6px !important;
    vertical-align:middle;
}

.table thead th{
    background:#0a7f2e !important;
    color:#fff !important;
    text-transform:uppercase;
    font-size:13px;
}

.table tbody tr:nth-child(even){
    background:#f9f9f9;
}

.cert-table td{
    padding:8px;
    border-bottom:1px dotted #ccc;
}

.cert-label{
    width:220px;
    font-weight:bold;
}

.row{
    margin:0 !important;
}

.col-md-6{
    padding:0 !important;
}

.print-page{
    width:210mm;
    height:297mm;
    margin:0 auto;
}

/* PRINT */
@media print{

    html, body{
        margin:0;
        padding:0;
      
    }

    .no-print{
        display:none !important;
    }

    .invoice-box{
      
      
        page-break-inside:avoid;
    }

    table, tr, td{
        page-break-inside:avoid !important;
    }
}

@page{
    size:A4;
    margin:10mm;
}

</style>

<div class="print-page">

<div class="invoice-box">

<div class="watermark">
    CONFERENCE QUOTE
</div>

<!-- HEADER -->
<div class="cert-header">

    <img src="<?= $logo ?>" class="logo">

    <div class="header-info">

        <div class="header-left">

            <strong>
                RAMS EXECUTIVE LODGE<br>
                Mponela Trading Centre<br>
                100M from Mount Meru Filling Station<br>
                Along M1 Road, Next St Peters Secondary<br>
                TPIN: 30798071
            </strong>

        </div>

        <div class="header-right">

            <strong>
                0993 899 382<br>
                0883 899 382<br>
                https://ramslodgemw.com<br>
                ramslodge@gmail.com
            </strong>

        </div>

    </div>

    <div class="cert-title">
        CONFERENCE QUOTATION
    </div>

</div>

<hr>

<!-- CLIENT DETAILS -->
<table class="cert-table" width="100%">

<tr>
    <td class="cert-label">Client Name</td>
    <td><?= htmlspecialchars($booking['client_name']) ?></td>
</tr>

<tr>
    <td class="cert-label">Booking Number</td>
    <td>#<?= $booking_id ?></td>
</tr>

<tr>
    <td class="cert-label">Conference Days</td>
    <td><?= $num_days ?></td>
</tr>

<tr>
    <td class="cert-label">Attendees</td>
    <td><?= number_format($attendees) ?></td>
</tr>

<tr>
    <td class="cert-label">Date</td>
    <td><?= date('d/m/Y') ?></td>
</tr>

</table>

<br>

<!-- BREAKDOWN -->
<h5>
    <strong>
        Conference Package Breakdown
    </strong>
</h5>

<table class="table table-bordered">

<thead>
<tr>
    <th>#</th>
    <th>Description</th>
    <th>Amount (MWK)</th>
</tr>
</thead>

<tbody>

<?php if($total_meals > 0): ?>
<tr>
    <td>1</td>
    <td>Conference Package</td>
    <td><?= number_format($total_meals,2) ?></td>
</tr>
<?php endif; ?>

<?php if($total_refreshments > 0): ?>
<tr>
    <td>2</td>
    <td>Conference Package</td>
    <td><?= number_format($total_refreshments,2) ?></td>
</tr>
<?php endif; ?>

<?php if($total_accommodation > 0): ?>
<tr>
    <td>3</td>
    <td>
        Accommodation
        <?php if($total_room_discount > 0): ?>
            <br>
            <small>
                Discount Applied:
                MWK <?= number_format($total_room_discount,2) ?>
            </small>
        <?php endif; ?>
    </td>
    <td><?= number_format($total_accommodation,2) ?></td>
</tr>
<?php endif; ?>

<?php if($conference_total > 0): ?>
<tr>
    <td>4</td>
    <td>Conference Room</td>
    <td><?= number_format($conference_total,2) ?></td>
</tr>
<?php endif; ?>

<?php if($total_booking_discount > 0): ?>
<tr>
    <td>5</td>
    <td>Total Booking Discount</td>
    <td>-<?= number_format($total_booking_discount,2) ?></td>
</tr>
<?php endif; ?>

</tbody>

</table>

<!-- TOTALS -->
<table class="table table-bordered">

<tr>
    <th>Subtotal</th>
    <td class="text-end">
        <?= number_format($subtotal,2) ?>
    </td>
</tr>

<tr>
    <th>VAT</th>
    <td class="text-end">
        <?= number_format($vat,2) ?>
    </td>
</tr>

<tr>
    <th>Tourism Levy</th>
    <td class="text-end">
        <?= number_format($levy,2) ?>
    </td>
</tr>

<tr style="background:#0a7f2e;color:#fff;">

    <th>
        Grand Total (MWK)
    </th>

    <td class="text-end">

        <strong>
            <?= number_format($grand_total,2) ?>
        </strong>

    </td>

</tr>

</table>

<!-- FOOTER -->
<div class="row mt-4">

    <div class="col-md-6">

      
    </div>

    <div class="col-md-6 text-end">

        <img src="<?= $qr_url ?>">

        <br>

        <small>
            Scan to verify quotation
        </small>

    </div>

</div>

<hr>

<div class="text-muted">

    <em>
        Prices are indicative and subject to confirmation.
    </em>

</div>

<!-- BUTTONS -->
<div class="no-print text-center mt-3">

    <button class="btn btn-dark" onclick="window.print()">
        <i class="fa fa-print"></i> Print
    </button>

    <button class="btn btn-secondary" onclick="history.back()">
        Back
    </button>

</div>

</div>
</div>

