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

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

if(!$guest_id){
    echo "<div class='alert alert-danger'>Invalid Guest</div>";
    exit;
}

// Fetch guest
$guest = $conn->query("SELECT * FROM guests WHERE id=$guest_id")->fetch_assoc();
if(!$guest){
    echo "<div class='alert alert-danger'>Guest not found</div>";
    exit;
}

// Stay (optional)
$stay = null;
if($stay_id){
    $stay = $conn->query("
        SELECT s.*, r.room_number, r.room_type
        FROM stays s
        JOIN rooms r ON s.room_id=r.id
        WHERE s.id=$stay_id
    ")->fetch_assoc();
}

// Items
$items = $conn->query("
    SELECT * FROM quotations
    WHERE guest_id=$guest_id
    ".($stay_id ? "AND stay_id=$stay_id" : "")."
    ORDER BY created_at ASC
");

$subtotal = 0;
$item_rows = [];

while($i = $items->fetch_assoc()){
    $item_rows[] = $i;
    $subtotal += $i['amount'];
}

// Tax
$tax_rate = 0.175;
$levy_rate = 0.01;

$tax = $subtotal * $tax_rate;
$levy = $subtotal * $levy_rate;

$total_bill = $subtotal + $tax + $levy;

// QR
$qr_data = urlencode("Quotation for: ".$guest['name']." Total: MWK ".$total_bill);
$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;
    width:210mm;
    height:297mm;
    margin:0 auto;
    padding:10mm;
    box-sizing:border-box;
    position:relative;
    overflow:hidden;
    border:3px solid #0a7f2e;
    font-size:11px;
}

/* 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:14px;
    text-align:left;
}

/* TABLE */
.cert-table td{
    padding:10px;
    border-bottom:1px dotted #aaa;
}

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

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

/* PRINT */
@media print{
    body{zoom:0.9;}
    .no-print{display:none;}
   .invoice-box{
    border:none;
    box-shadow:none;
}
}

/* PAGE SETTINGS */
@page{
    size:A4;
    margin:10mm;
}

.print-page{
    width:210mm;
    height:297mm;
    margin:0 auto;
    display:block;
}
.row{
    margin:0 !important;
}

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

    body{
        margin:0;
        padding:0;
    }

    html, body{
        height:100%;
        overflow:hidden;
    }

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

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

    .table thead th{
    border-bottom:2px solid #0a7f2e !important;
    font-size:11px;
    text-transform:uppercase;
}

.table td{
    border-bottom:1px solid #eee !important;
}
/* ===== CLEAN INVOICE TABLE BORDERS ===== */

.table{
    width:100%;
    border-collapse:collapse;
    font-size:11px;
}

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

/* Header styling */
.table thead th{
    background:#0a7f2e;
    color:#fff;
    font-weight:bold;
    text-transform:uppercase;
    font-size:11px;
    border:1px solid #0a7f2e !important;
}

/* Zebra rows (optional but looks professional) */
.table tbody tr:nth-child(even){
    background:#f9f9f9;
}

/* Hover effect (screen only) */
.table tbody tr:hover{
    background:#f1f1f1;
}

/* TOTAL TABLE (keep strong border) */
.table-bordered th,
.table-bordered td{
    border:1px solid #ccc !important;
}

    .invoice-box{
        width:210mm;
        height:297mm;
        page-break-after: avoid;
        page-break-before: avoid;
        page-break-inside: avoid;
    }

    table, tr, td{
        page-break-inside: avoid !important;
    }
}
.invoice-box{
    position:relative;
    overflow:hidden;
}

.invoice-box *{
    position:relative;
    z-index:1;
}
</style>

<div class="print-page">
<div class="invoice-box">

<?php if($subtotal <= 0): ?>
<div class="watermark">QUOTATION</div>
<?php endif; ?>

<!-- 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">QUOTATION</div>
</div>

<hr>

<!-- GUEST INFO -->
<table class="cert-table" width="100%">
    <tr>
        <td class="cert-label">Guest Name</td>
        <td><?= htmlspecialchars($guest['name']) ?></td>
    </tr>

    <?php if($stay): ?>
    <tr>
        <td class="cert-label">Room</td>
        <td><?= $stay['room_number'] ?> (<?= $stay['room_type'] ?>)</td>
    </tr>
    <tr>
        <td class="cert-label">Stay Days</td>
        <td><?= $stay['days'] ?></td>
    </tr>
    <?php endif; ?>

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

<br>

<!-- ITEMS -->
<h5><strong>Quotation Breakdown</strong></h5>

<table class="table table-bordered">
    <thead class="table-dark">
        <tr>
            <th>#</th>
            <th>Type</th>
            <th>Description</th>
            <th>Unit Price</th>
            <th>Qty</th>
            <th>Amount</th>
        </tr>
    </thead>
    <tbody>
        <?php $i=1; foreach($item_rows as $item): ?>
        <tr>
            <td><?= $i++ ?></td>
            <td><?= htmlspecialchars($item['type']) ?></td>
            <td><?= htmlspecialchars($item['item_name']) ?></td>
            <td><?= number_format($item['unit_price'],2) ?></td>
            <td><?= $item['quantity'] ?></td>
            <td><?= number_format($item['amount'],2) ?></td>
        </tr>
        <?php endforeach; ?>
    </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 (17.5%)</th>
        <td class="text-end"><?= number_format($tax,2) ?></td>
    </tr>
    <tr>
        <th>Tourism Levy (1%)</th>
        <td class="text-end"><?= number_format($levy,2) ?></td>
    </tr>
    <tr class="table-dark text-white">
        <th>Total Amount (MWK)</th>
        <td class="text-end"><strong><?= number_format($total_bill,2) ?></strong></td>
    </tr>
</table>

<!-- SIGNATURE / QR -->
<div class="row mt-4">
    

    <div class="col-md-6 text-end">
        <img src="<?= $qr_url ?>"><br>
        <small>Scan to verify quotation</small>
    </div>
</div>

<hr>

<!-- 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>

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