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

$store_id = intval($_GET['store_id']);

// Fetch store details
$store = $conn->query("SELECT * FROM stores WHERE id=$store_id")->fetch_assoc();
if(!$store){
    echo "<script>alert('Store not found');window.location='products.php';</script>";
    exit;
}

// Fetch products with stock in this store
$products = $conn->query("
    SELECT p.*, c.name as category_name, 
           IFNULL(s.quantity, 0) as stock_quantity
    FROM products p
    LEFT JOIN categories c ON p.category_id=c.id
    LEFT JOIN store_stock s ON s.product_id = p.id AND s.store_id = $store_id
    ORDER BY p.id DESC
");

// Fetch all stores for transfer
$stores = $conn->query("SELECT * FROM stores WHERE id != $store_id ORDER BY store_name ASC");

?>

<div class="container mt-4">
     <div class="welcome-msg">
        <i class="fa fa-user-circle"></i> Welcome, <strong><?= htmlspecialchars($_SESSION['username'] ?? 'Guest') ?></strong>!
    </div>
    <h3>Inventory for Store: <?= htmlspecialchars($store['store_name']) ?></h3>
    <hr>

    <table class="table table-bordered table-striped">
        <thead class="bg-dark text-white">
            <tr>
                <th>#</th>
                <th>SKU</th>
                <th>Name</th>
                <th>Category</th>
                <th>Stock Qty</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
        <?php $i=1; while($row = $products->fetch_assoc()): ?>
            <tr>
                <td><?= $i ?></td>
                <td><?= $row['sku'] ?></td>
                <td><?= $row['name'] ?></td>
                <td><?= $row['category_name'] ?></td>
                <td><?= $row['stock_quantity'] ?></td>
                <td>
                    <!-- Restock / Transfer Form -->
                    <form method="POST" action="transfer_stock.php" class="d-flex align-items-center gap-2">
                        <input type="hidden" name="product_id" value="<?= $row['id'] ?>">
                        <input type="hidden" name="from_store_id" value="<?= $store_id ?>">
                        <input type="number" name="quantity" placeholder="Qty" class="form-control form-control-sm" style="width:70px;" required min="1">

                        <select name="to_store_id" class="form-control form-control-sm" style="width:150px;">
                            <option value="">-- Transfer To --</option>
                            <?php foreach($stores as $s): ?>
                                <option value="<?= $s['id'] ?>"><?= htmlspecialchars($s['store_name']) ?></option>
                            <?php endforeach; ?>
                        </select>
                        <button type="submit" name="transfer" class="btn btn-sm btn-success">Submit</button>
                    </form>
                </td>
            </tr>
        <?php $i++; endwhile; ?>
        </tbody>
    </table>

</div>

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