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

// Get requisition ID from query string
$req_id = intval($_GET['id'] ?? 0);

if(!$req_id){
    die("Invalid requisition ID.");
}

// Fetch requisition header
$requisition = $conn->query("
    SELECT br.*, u.username AS requested_by, ua.username AS approved_by_name
    FROM bar_requisitions br
    LEFT JOIN users u ON br.requested_by=u.id
    LEFT JOIN users ua ON br.approved_by=ua.id
    WHERE br.id=$req_id
")->fetch_assoc();

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

// Fetch items
$items = $conn->query("
    SELECT bri.*, p.name
    FROM bar_requisition_items bri
    LEFT JOIN products p ON bri.product_id=p.id
    WHERE bri.requisition_id=$req_id
");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Print Requisition #<?= $requisition['id'] ?></title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.2/css/bootstrap.min.css">
    <style>
        @media print {
            .no-print { display: none; }
        }
        body { font-family: Arial, sans-serif; margin: 20px; }
        .header { text-align: center; margin-bottom: 20px; }
        .header img { max-height: 80px; }
        table { width: 100%; }
        .signature { margin-top: 50px; }
        .signature div { display: inline-block; width: 45%; text-align: center; }
    </style>
</head>
<body>

<div class="header">
    <img src="assets/logo.png" alt="Company Logo">
    <h2>Bar Requisition Form</h2>
    <hr>
</div>

<div class="mb-3">
    <strong>Requisition ID:</strong> <?= $requisition['id'] ?><br>
    <strong>Requested By:</strong> <?= $requisition['requested_by'] ?><br>
    <strong>Date:</strong> <?= $requisition['created_at'] ?><br>
    <strong>Status:</strong> <?= $requisition['status'] ?> 
    <?php if($requisition['status']=='Approved'): ?>
        | <strong>Approved By:</strong> <?= $requisition['approved_by_name'] ?> 
        at <?= $requisition['approved_at'] ?>
    <?php endif; ?>
</div>

<table class="table table-bordered">
    <thead class="bg-secondary text-white">
        <tr>
            <th>#</th>
            <th>Product</th>
            <th>Quantity</th>
        </tr>
    </thead>
    <tbody>
        <?php 
        $count=1; 
        while($item = $items->fetch_assoc()): 
        ?>
        <tr>
            <td><?= $count++ ?></td>
            <td><?= htmlspecialchars($item['name']) ?></td>
            <td><?= intval($item['quantity']) ?></td>
        </tr>
        <?php endwhile; ?>
    </tbody>
</table>

<div class="signature">
    <div>
        ________________________<br>
        Requester Signature
    </div>
    <div>
        ________________________<br>
        Approver Signature
    </div>
</div>

<div class="text-center mt-4 no-print">
    <button onclick="window.print()" class="btn btn-primary"><i class="fa fa-print"></i> Print</button>
    <a href="bar_requisitions.php" class="btn btn-secondary">Back</a>
</div>

</body>
</html>
