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

if(!isset($_GET['id']) || !is_numeric($_GET['id'])){
    die("Invalid requisition ID.");
}

$requisition_id = intval($_GET['id']);

// Fetch requisition details
$stmt = $conn->prepare("
SELECT
    r.*,
    u.username
FROM low_stock_requisitions r
LEFT JOIN users u ON r.requested_by = u.id
WHERE r.id = ?
");

$stmt->bind_param("i", $requisition_id);
$stmt->execute();
$req = $stmt->get_result()->fetch_assoc();

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

// Fetch requisition items
$items = $conn->query("
SELECT
    i.*,
    p.sku,
    p.name,
    p.buying_price
FROM low_stock_requisition_items i
INNER JOIN products p
    ON p.id = i.product_id
WHERE i.requisition_id = $requisition_id
ORDER BY p.name ASC
");

$grand_total = 0;
?>

<div class="container-fluid mt-4">

<div class="d-flex justify-content-between align-items-center mb-3">
    <h3>
        <i class="fa fa-file-alt"></i>
        Requisition #<?= $requisition_id ?>
    </h3>

    <div>
        <button onclick="window.print()" class="btn btn-success">
            <i class="fa fa-print"></i> Print
        </button>

        <a href="inventory_rep.php"
           class="btn btn-secondary">
            <i class="fa fa-arrow-left"></i> Back
        </a>
    </div>
</div>

<div class="card shadow">

<div class="card-header bg-dark text-white">
    Requisition Details
</div>

<div class="card-body">

<div class="row">

<div class="col-md-6">
    <p>
        <strong>Requisition ID:</strong>
        #<?= $requisition_id ?>
    </p>

    <p>
        <strong>Requested By:</strong>
        <?= htmlspecialchars($req['username'] ?? 'Unknown') ?>
    </p>
</div>

<div class="col-md-6 text-end">
    <p>
        <strong>Date Created:</strong>
        <?= date('d M Y H:i', strtotime($req['created_at'])) ?>
    </p>
</div>

</div>

<hr>

<h5 class="mb-3">
    Inventory Replenishment Items
</h5>

<table class="table table-bordered table-striped">

<thead class="table-dark">
<tr>
    <th>#</th>
    <th>SKU</th>
    <th>Product Name</th>
    <th>Buying Price</th>
    <th>Quantity</th>
    <th>Total Value</th>
</tr>
</thead>

<tbody>

<?php
$i = 1;

while($row = $items->fetch_assoc()):

$total = $row['buying_price'] * $row['quantity'];
$grand_total += $total;
?>

<tr>

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

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

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

<td class="text-end">
    MWK <?= number_format($row['buying_price'],2) ?>
</td>

<td class="text-center">
    <?= number_format($row['quantity']) ?>
</td>

<td class="text-end">
    MWK <?= number_format($total,2) ?>
</td>

</tr>

<?php endwhile; ?>

<tr class="table-success">

<td colspan="5" class="text-end">
    <strong>GRAND TOTAL</strong>
</td>

<td class="text-end">
    <strong>
        MWK <?= number_format($grand_total,2) ?>
    </strong>
</td>

</tr>

</tbody>

</table>

<div class="row mt-5">

<div class="col-md-4 text-center">
    <br><br>
    __________________________
    <br>
    Prepared By
</div>

<div class="col-md-4 text-center">
    <br><br>
    __________________________
    <br>
    Approved By
</div>

<div class="col-md-4 text-center">
    <br><br>
    __________________________
    <br>
    Store Issued By
</div>

</div>

</div>
</div>

</div>

<style>
@media print {

    .btn,
    .navbar,
    .sidebar,
    .content-header,
    footer {
        display:none !important;
    }

    body{
        background:#fff;
    }

    .card{
        border:none;
        box-shadow:none !important;
    }

    .card-header{
        background:#fff !important;
        color:#000 !important;
        border-bottom:2px solid #000;
    }
}
</style>

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