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

// Main Store department ID
$main_store_id = 1; 

// Get requisition ID from URL
$requisition_id = intval($_GET['id'] ?? 0);
if($requisition_id <= 0){
    echo "<div class='alert alert-danger'>Invalid requisition ID.</div>";
    exit;
}

// Get requisition info (requested_by from users)
$req = $conn->query("
    SELECT br.*, u.username AS requested_by
    FROM bar_requisitions br
    LEFT JOIN users u ON br.requested_by = u.id
    WHERE br.id = $requisition_id
")->fetch_assoc();

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

// Get requisition items with product info
$items = $conn->query("
    SELECT bri.*, p.name AS product_name, p.sku, p.selling_price, bri.quantity AS requested_qty, p.id AS product_id
    FROM bar_requisition_items bri
    LEFT JOIN products p ON bri.product_id = p.id
    WHERE bri.requisition_id = $requisition_id
");
?>

<div class="container mt-4">
    <h3><i class="fa fa-file-alt"></i> Requisition Details #<?= $req['id'] ?></h3>
    <p>
        <strong>Requested By:</strong> <?= htmlspecialchars($req['requested_by'] ?? 'N/A') ?><br>
        <strong>Status:</strong> <?= htmlspecialchars($req['status']) ?><br>
        <strong>Date:</strong> <?= date('d-M-Y H:i', strtotime($req['created_at'])) ?>
    </p>
    <hr>

    <h5>Products Requested</h5>
    <table class="table table-bordered table-striped">
        <thead class="bg-dark text-white">
            <tr>
                <th>#</th>
                <th>SKU</th>
                <th>Product</th>
                <th>Requested Qty</th>
                <th>Price (MK)</th>
                <th>Main Store Qty <i class="fa fa-warehouse ms-1"></i></th>
                <th>Total (MK)</th>
            </tr>
        </thead>
        <tbody>
            <?php 
            $total_amount = 0;
            if($items->num_rows > 0):
                $i = 1;
                while($item = $items->fetch_assoc()):
                    // Total line amount
                    $line_total = ($item['selling_price'] ?? 0) * $item['requested_qty'];
                    $total_amount += $line_total;

                    // Fetch Main Store quantity
                    $stock = $conn->query("
                        SELECT SUM(quantity) AS main_store_qty 
                        FROM product_stock 
                        WHERE product_id = ".$item['product_id']." 
                         
                    ")->fetch_assoc();
                    $main_store_qty = $stock['main_store_qty'] ?? 0;
            ?>
            <tr>
                <td><?= $i ?></td>
                <td><?= htmlspecialchars($item['sku'] ?? '-') ?></td>
                <td><?= htmlspecialchars($item['product_name'] ?? '-') ?></td>
                <td><?= $item['requested_qty'] ?></td>
                <td><?= number_format($item['selling_price'] ?? 0, 2) ?></td>
                <td>
                    <span class="badge bg-info text-dark">
                        <i class="fa fa-boxes me-1"></i> <?= $main_store_qty ?>
                    </span>
                </td>
                <td><?= number_format($line_total, 2) ?></td>
            </tr>
            <?php 
                    $i++;
                endwhile;
            else: ?>
            <tr>
                <td colspan="7" class="text-center">No products found for this requisition.</td>
            </tr>
            <?php endif; ?>
        </tbody>
        <tfoot>
            <tr>
                <th colspan="6" class="text-end">Total Amount:</th>
                <th>MK <?= number_format($total_amount,2) ?></th>
            </tr>
        </tfoot>
    </table>

    <a href="requisition_view.php" class="btn btn-secondary mt-3"><i class="fa fa-arrow-left"></i> Back to Requisitions</a>
</div>

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