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

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

$id = intval($_GET['id'] ?? 0);

if($id <= 0){
echo "<div class='alert alert-danger'>Invalid Product</div>";
exit;
}

/* ================= FETCH PRODUCT ================= */

$product = $conn->query("
SELECT pp.*, d.name AS department
FROM perishable_products pp
LEFT JOIN departments d ON pp.department_id=d.id
WHERE pp.id=$id
")->fetch_assoc();

if(!$product){
echo "<div class='alert alert-danger'>Product not found</div>";
exit;
}

/* ================= HANDLE DISPATCH ================= */

if(isset($_POST['dispatch'])){

$department_id = intval($_POST['department_id']);
$quantity = intval($_POST['quantity']);
$collected_by = trim($_POST['collected_by']);

if($quantity <= 0){
echo "<div class='alert alert-danger'>Invalid quantity</div>";
}

elseif($quantity > $product['quantity']){
echo "<div class='alert alert-danger'>Quantity exceeds available stock</div>";
}

else{

/* RECORD DISPATCH */

$stmt = $conn->prepare("
INSERT INTO perishable_dispatch
(perishable_id,department_id,quantity,collected_by,dispatched_by)
VALUES (?,?,?,?,?)
");

$stmt->bind_param(
"iiiss",
$id,
$department_id,
$quantity,
$collected_by,
$username
);

$stmt->execute();

/* UPDATE STOCK */

$stmt2 = $conn->prepare("
UPDATE perishable_products
SET quantity = quantity - ?
WHERE id = ?
");

$stmt2->bind_param("ii",$quantity,$id);
$stmt2->execute();

echo "<script>
alert('Stock dispatched successfully');
window.location='perishable_products.php';
</script>";

exit;

}

}

/* ================= FETCH DEPARTMENTS ================= */

$departments = $conn->query("
SELECT id,name
FROM departments
ORDER BY name
");

?>

<div class="container mt-4">

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

<h3>📦 Dispatch Perishable Product</h3>

<hr>

<div class="card shadow">

<div class="card-body">

<div class="row mb-3">

<div class="col-md-4">
<b>Product</b><br>
<?= htmlspecialchars($product['product_name']) ?>
</div>

<div class="col-md-2">
<b>Batch</b><br>
<?= $product['batch_number'] ?>
</div>

<div class="col-md-2">
<b>Expiry</b><br>
<?= $product['expiry_date'] ?>
</div>

<div class="col-md-2">
<b>Available Qty</b><br>
<span class="badge bg-success">
<?= $product['quantity'] ?>
</span>
</div>

<div class="col-md-2">
<b>Recorded By</b><br>
<?= $product['recorded_by'] ?>
</div>

</div>

<hr>

<form method="POST">

<div class="row">

<div class="col-md-4 mb-3">

<label>Department Receiving</label>

<select name="department_id" class="form-control" required>

<option value="">Select Department</option>

<?php while($d=$departments->fetch_assoc()): ?>

<option value="<?= $d['id'] ?>">
<?= $d['name'] ?>
</option>

<?php endwhile; ?>

</select>

</div>

<div class="col-md-3 mb-3">

<label>Quantity to Dispatch</label>

<input 
type="number"
name="quantity"
class="form-control"
max="<?= $product['quantity'] ?>"
required
>

</div>

<div class="col-md-5 mb-3">

<label>Person Collecting</label>

<input
type="text"
name="collected_by"
class="form-control"
placeholder="Enter name"
required
>

</div>

</div>

<button class="btn btn-primary" name="dispatch">

<i class="fa fa-share"></i> Dispatch Stock

</button>

<a href="perishable_products.php" class="btn btn-secondary">

Cancel

</a>

</form>

</div>

</div>

</div>

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