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

if(!isset($_GET['id'])){
    die("Invalid Request");
}

$id = intval($_GET['id']);

// Fetch requisition
$req = $conn->query("
    SELECT r.*, u.username
    FROM restaurant_requisitions r
    LEFT JOIN users u ON r.requested_by = u.id
    WHERE r.id = $id
")->fetch_assoc();

if(!$req){
    die("Requisition not found");
}

// Fetch items
$items = $conn->query("
    SELECT ri.*, p.name, p.sku
    FROM restaurant_requisition_items ri
    LEFT JOIN products p ON ri.product_id = p.id
    WHERE ri.requisition_id = $id
");
?>

<!DOCTYPE html>
<html>
<head>
<title>Print Restaurant Requisition</title>
<style>
body{
    font-family: Arial, sans-serif;
    margin:40px;
}
.header{
    display:flex;
    justify-content:space-between;
    align-items:center;
    border-bottom:2px solid #000;
    padding-bottom:10px;
}
.logo img{
    height:80px;
}
.title{
    text-align:right;
}
h2{
    margin:0;
}
table{
    width:100%;
    border-collapse:collapse;
    margin-top:20px;
}
th, td{
    border:1px solid #000;
    padding:8px;
    text-align:left;
}
th{
    background:#f0f0f0;
}
.info{
    margin-top:20px;
}
.status{
    padding:5px 10px;
    font-weight:bold;
}
.footer{
    margin-top:60px;
    display:flex;
    justify-content:space-between;
}
.signature{
    width:200px;
    border-top:1px solid #000;
    text-align:center;
    padding-top:5px;
}
@media print{
    button{ display:none; }
}
</style>
</head>
<body>

<button onclick="window.print()" style="float:right;margin-bottom:10px;">
    🖨 Print
</button>

<div class="header">
    <div class="logo">
        <!-- 🔹 CHANGE LOGO PATH -->
        <img src="assets/logo.png" alt="Hotel Logo">
    </div>
    <div class="title">
        <h2>RAMS EXECUTIVE LODGE</h2>
       
         Phone: 0993 899 382<br>
        Cell: 0889 899 382<br/>
        https://ramslodgemw.com</small>
    </div>
</div>

<div class="info">
    <p><strong>Requisition ID:</strong> #<?= $req['id'] ?></p>
    <p><strong>Requested By:</strong> <?= htmlspecialchars($req['username']) ?></p>
    <p><strong>Date:</strong> <?= $req['created_at'] ?></p>
    <p><strong>Status:</strong> <?= $req['status'] ?></p>
</div>

<table>
<thead>
<tr>
    <th>#</th>
    <th>SKU</th>
    <th>Product</th>
    <th>Quantity</th>
</tr>
</thead>
<tbody>

<?php 
$i=1;
while($item = $items->fetch_assoc()):
?>
<tr>
    <td><?= $i++ ?></td>
    <td><?= htmlspecialchars($item['sku']) ?></td>
    <td><?= htmlspecialchars($item['name']) ?></td>
    <td><?= $item['quantity'] ?></td>
</tr>
<?php endwhile; ?>

</tbody>
</table>

<div class="footer">
    <div class="signature">
        Requested By
    </div>
    <div class="signature">
        Approved By
    </div>
    <div class="signature">
        Received By
    </div>
</div>

</body>
</html>