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

$id = intval($_GET['id'] ?? 0);
if(!$id){
    die("Invalid requisition ID");
}

// Fetch requisition info
$req = $conn->query("SELECT * FROM perishable_production WHERE id=$id")->fetch_assoc();
if(!$req){
    die("Requisition not found");
}

// Fetch items for this requisition
$itemsRes = $conn->query("
    SELECT pp.*, p.name AS item_name, p.unit
    FROM perishable_production_items pp
    JOIN perishables p ON p.id = pp.perishable_id
    WHERE pp.production_id = $id
");
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Perishable Requisition #<?= $id ?></title>
<style>
    body { font-family: Arial, sans-serif; margin: 20px; }
    .header { display: flex; align-items: center; justify-content: space-between; }
    .header img { height: 60px; }
    .header h2 { text-align: center; flex-grow: 1; margin: 0; font-size: 24px; }
    table { border-collapse: collapse; width: 100%; margin-top: 20px; }
    th, td { border: 1px solid #333; padding: 8px; text-align: left; }
    th { background: #333; color: white; }
    tfoot th { background: #f0f0f0; color: #000; }
    .info { margin-top: 15px; }
    .info p { margin: 4px 0; }
    .signature { margin-top: 50px; display: flex; justify-content: space-between; }
    .signature div { width: 30%; text-align: center; }
    .signature-line { margin-top: 60px; border-top: 1px solid #333; }
    @media print {
        .no-print { display: none; }
    }
</style>
</head>
<body>

<div class="header">
    <img src="assets/logo.png" alt="Company Logo">
    <h2>Perishable Requisition</h2>
    <div style="width:60px"></div>
</div>

<div class="info">
    <p><strong>Requisition ID:</strong> <?= $req['id'] ?></p>
    <p><strong>Prepared By:</strong> <?= htmlspecialchars($req['created_by']) ?></p>
    <p><strong>Number of People:</strong> <?= $req['number_of_people'] ?></p>
    <p><strong>Date:</strong> <?= $req['created_at'] ?></p>
</div>

<table>
    <thead>
        <tr>
            <th>#</th>
            <th>Item</th>
            <th>Unit</th>
            <th>Total Needed</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $i = 1;
        $grandTotal = 0;
        while($row = $itemsRes->fetch_assoc()){
            $total = $row['total_needed']; // Already calculated total
            $grandTotal += $total;
            echo "<tr>
                <td>{$i}</td>
                <td>{$row['item_name']}</td>
                <td>{$row['unit']}</td>
                <td>{$total}</td>
            </tr>";
            $i++;
        }
        ?>
    </tbody>
    <tfoot>
        <tr>
            <th colspan="3" class="text-end">Grand Total:</th>
            <th><?= $grandTotal ?></th>
        </tr>
    </tfoot>
</table>

<div class="signature">
    <div>
        <div class="signature-line"></div>
        <p>Prepared By</p>
    </div>
    <div>
        <div class="signature-line"></div>
        <p>Checked By</p>
    </div>
    <div>
        <div class="signature-line"></div>
        <p>Received By</p>
    </div>
</div>

<div class="no-print" style="margin-top: 20px;">
    <button onclick="window.print()" style="padding:10px 20px; font-size:16px;">Print</button>
</div>

<script>
    window.onload = function(){
        window.print();
    }
</script>

</body>
</html>