<?php
error_reporting(E_ALL & ~E_DEPRECATED);
include 'includes/auth.php';
include 'db.php';
include 'phpqrcode/qrlib.php';

$requisition_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
if(!$requisition_id){
    die("Error: Requisition ID not specified.");
}

/* ================= FETCH REQUISITION ================= */
$stmt = $conn->prepare("
    SELECT r.*, u.username AS requested_by_name
    FROM hk_requisitions r
    LEFT JOIN users u ON r.requested_by = u.id
    WHERE r.id = ?
");
$stmt->bind_param("i", $requisition_id);
$stmt->execute();
$req = $stmt->get_result()->fetch_assoc();

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

/* ================= FETCH ITEMS ================= */
$items_stmt = $conn->prepare("
    SELECT ri.*, p.name, p.sku,
           COALESCE(SUM(ps.quantity),0) AS stock_qty
    FROM hk_requisition_items ri
    LEFT JOIN products p ON ri.product_id = p.id
    LEFT JOIN product_stock ps ON p.id = ps.product_id
    WHERE ri.requisition_id = ?
    GROUP BY ri.id
");
$items_stmt->bind_param("i", $requisition_id);
$items_stmt->execute();
$items = $items_stmt->get_result();

/* ================= STATUS COLOR ================= */
$status_color = match(strtolower($req['status'])){
    'approved' => '#198754',
    'rejected' => '#dc3545',
    default => '#ffc107'
};

/* ================= QR CODE ================= */
$qr_data = "HK Requisition #{$req['id']}\nRequested By: {$req['requested_by_name']}\nDate: {$req['created_at']}";
$qr_file = 'qrcodes/hk_req_'.$req['id'].'.png';

if(!file_exists($qr_file)){
    if(!is_dir('qrcodes')) mkdir('qrcodes', 0777, true);
    QRcode::png($qr_data, $qr_file, QR_ECLEVEL_L, 4);
}
?>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Housekeeping Requisition #<?= $req['id'] ?></title>

<style>
body{
    font-family: Arial, sans-serif;
    margin:30px;
    color:#000;
}

.header{
    display:flex;
    justify-content:space-between;
    align-items:center;
    border-bottom:3px solid #000;
    padding-bottom:10px;
    margin-bottom:20px;
}

.logo img{
    height:80px;
}

.title{
    text-align:right;
    font-size:14px;
}

.title h2{
    margin:0;
}

.info{
    margin-top:10px;
    line-height:1.6;
}

.status-badge{
    padding:4px 10px;
    color:#fff;
    font-weight:bold;
    border-radius:4px;
    background:<?= $status_color ?>;
}

.qr{
    text-align:right;
    margin-top:10px;
}

table{
    width:100%;
    border-collapse:collapse;
    margin-top:20px;
}

th, td{
    border:1px solid #000;
    padding:8px;
}

th{
    background:#f2f2f2;
}

.footer{
    margin-top:70px;
    display:flex;
    justify-content:space-between;
}

.signature{
    width:220px;
    border-top:1px solid #000;
    text-align:center;
    padding-top:6px;
    font-size:13px;
}

.page-footer{
    position:fixed;
    bottom:10px;
    left:0;
    right:0;
    text-align:center;
    font-size:12px;
    color:#666;
}

.print-btn{
    float:right;
    margin-bottom:10px;
}

@media print{
    .print-btn{ display:none; }
    body{ margin:15px; }
}
</style>
</head>

<body onload="window.print()">

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

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

    <div class="logo">
        <img src="assets/logo.png" onerror="this.style.display='none'">
    </div>

    <div class="title">
        <h2>RAMS EXECUTIVE LODGE</h2>
        Phone: 0993 899 382<br>
        Cell: 0889 899 382<br>
        https://ramslodgemw.com
    </div>

</div>

<!-- INFO -->
<div class="info">
    <strong>Requisition ID:</strong> #<?= $req['id'] ?><br>
    <strong>Department:</strong> Housekeeping<br>
    <strong>Requested By:</strong> <?= htmlspecialchars($req['requested_by_name']) ?><br>
    <strong>Date:</strong> <?= date('d M Y H:i', strtotime($req['created_at'])) ?><br>
    <strong>Status:</strong>
    <span class="status-badge"><?= htmlspecialchars($req['status']) ?></span>
</div>

<div class="qr">
    <img src="<?= $qr_file ?>" width="90">
</div>

<!-- ITEMS TABLE -->
<table>
<thead>
<tr>
    <th width="5%">#</th>
    <th width="20%">SKU</th>
    <th>Product</th>
    <th width="12%">Qty</th>
    <th width="15%">Available Stock</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><?= intval($item['quantity']) ?></td>
    <td><?= intval($item['stock_qty']) ?></td>
</tr>
<?php endwhile; ?>

<?php if($i==1): ?>
<tr>
    <td colspan="5" style="text-align:center;">No items found</td>
</tr>
<?php endif; ?>

</tbody>
</table>

<!-- SIGNATURE SECTION -->
<div class="footer">
    <div class="signature">Requested By</div>
    <div class="signature">Approved By</div>
    <div class="signature">Store Issued By</div>
</div>

<div class="page-footer">
    Generated on <?= date('d M Y H:i') ?>
</div>

</body>
</html>