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

$user_id = $_SESSION['user_id'] ?? 0;

// Fetch pending purchase requests
$requests = $conn->query("
    SELECT pr.*, p.name, p.sku, u.username
    FROM purchase_requests pr
    JOIN products p ON pr.product_id = p.id
    JOIN users u ON pr.requested_by = u.id
    WHERE pr.requested_by = $user_id AND pr.status='Pending'
    ORDER BY pr.created_at DESC
");

// Logo path
$logo = 'assets/logo.png'; // replace with your logo path
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Purchase Request Template</title>
<style>
    body { font-family: Arial, sans-serif; margin: 30px; }
    .header { display: flex; align-items: center; margin-bottom: 20px; }
    .header img { height: 80px; margin-right: 20px; }
    .header h2 { margin: 0; color: #333; }
    table { width: 100%; border-collapse: collapse; margin-top: 20px; }
    table th, table td { border: 1px solid #444; padding: 8px; text-align: left; }
    table th { background-color: #007BFF; color: white; }
    .footer { margin-top: 40px; font-size: 0.9rem; }
    @media print {
        button { display: none; }
        body { margin: 10mm; }
    }
</style>
</head>
<body>

       <button class="btn btn-secondary btn-custom" onclick="history.back()">
        <i class="fa fa-arrow-left"></i> Back
    </button>
<div class="header">
    <img src="<?= $logo ?>" alt="Company Logo">
    <h2>Housekeeping Purchase Request</h2>
</div>

<p><strong>Requested By:</strong> <?= htmlspecialchars($_SESSION['username'] ?? 'Unknown') ?></p>
<p><strong>Date:</strong> <?= date('d-m-Y H:i') ?></p>

<table>
    <thead>
        <tr>
            <th>#</th>
            <th>SKU</th>
            <th>Product</th>
            <th>Quantity</th>
            <th>Reason</th>
        </tr>
    </thead>
    <tbody>
        <?php $i=1; while($r = $requests->fetch_assoc()): ?>
        <tr>
            <td><?= $i ?></td>
            <td><?= htmlspecialchars($r['sku']) ?></td>
            <td><?= htmlspecialchars($r['name']) ?></td>
            <td><?= intval($r['quantity']) ?></td>
            <td><?= htmlspecialchars($r['reason']) ?></td>
        </tr>
        <?php $i++; endwhile; ?>
    </tbody>
</table>

<div class="footer">
    <p>Signature: ___________________________</p>
    <p>Approved By: ________________________</p>
</div>

<button onclick="window.print()">Print Request</button>

</body>
</html>