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

$username = $_SESSION['username'] ?? 'User';

// ================= APPROVE REQUEST =================
if(isset($_GET['approve'])){
    $id = intval($_GET['approve']);

    $conn->query("
        UPDATE purchase_requestsk
        SET status='Approved', approved_at=NOW() 
        WHERE id=$id
    ");

    echo "<script>alert('Purchase request approved'); window.location='approve_kitchen_purchases.php';</script>";
    exit;
}

// ================= REJECT REQUEST =================
if(isset($_GET['reject'])){
    $id = intval($_GET['reject']);

    $conn->query("
        UPDATE purchase_requestsk
        SET status='Rejected', approved_at=NOW() 
        WHERE id=$id
    ");

    echo "<script>alert('Purchase request rejected'); window.location='approve_kitchen_purchases.php';</script>";
    exit;
}

// ================= FETCH REQUESTS =================
$requests = $conn->query("
    SELECT pr.*, p.name, p.sku, p.buying_price, u.username
    FROM purchase_requestsk pr
    JOIN products p ON pr.product_id=p.id
    JOIN users u ON pr.requested_by=u.id
    ORDER BY pr.created_at DESC
");
?>

<div class="container mt-4">

<button class="btn btn-secondary mb-3" onclick="history.back()">
<i class="fa fa-arrow-left"></i> Back
</button>

<h4 class="mb-3">
<i class="fa fa-cart-plus"></i>Kitchen Purchase Request Approvals
</h4>

<table class="table table-bordered table-striped table-hover">
<thead class="bg-dark text-white">
<tr>
<th>#</th>
<th>SKU</th>
<th>Product</th>
<th>Buying Price</th>
<th>Quantity</th>
<th>Amount</th>
<th>Reason</th>
<th>Requested By</th>
<th>Dated</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>

<tbody>

<?php 
$i=1;
while($r = $requests->fetch_assoc()): 

$amount = $r['buying_price'] * $r['quantity'];

$status_color = "secondary";
if($r['status']=="Pending") $status_color="warning";
if($r['status']=="Approved") $status_color="success";
if($r['status']=="Rejected") $status_color="danger";

?>

<tr>

<td><?= $i ?></td>

<td><?= htmlspecialchars($r['sku']) ?></td>

<td><?= htmlspecialchars($r['name']) ?></td>

<td><?= number_format($r['buying_price'],2) ?></td>

<td><?= intval($r['quantity']) ?></td>

<td><?= number_format($amount,2) ?></td>

<td><?= htmlspecialchars($r['reason']) ?></td>

<td><?= htmlspecialchars($r['username']) ?></td>
<td><?= htmlspecialchars($r['created_at']) ?></td>

<td>
<span class="badge bg-<?= $status_color ?>">
<?= $r['status'] ?>
</span>
</td>

<td>

<?php if($r['status']=="Pending"): ?>

<a href="?approve=<?= $r['id'] ?>" 
class="btn btn-success btn-sm"
onclick="return confirm('Approve this request?')">
<i class="fa fa-check"></i>
</a>

<a href="?reject=<?= $r['id'] ?>" 
class="btn btn-danger btn-sm"
onclick="return confirm('Reject this request?')">
<i class="fa fa-times"></i>
</a>

<?php else: ?>

<span class="text-muted">Processed</span>

<?php endif; ?>

</td>

</tr>

<?php $i++; endwhile; ?>

</tbody>
</table>

</div>

<?php include 'includes/footer.php'; ?>