<?php
session_start();
require_once __DIR__ . '/../../includes/auth.php';
require_once __DIR__ . '/../../classes/Database.php';
require_once __DIR__ . '/../../classes/Inventory.php';


$database = new Database();
$conn = $database->getConnection();


$auth = new Auth($conn);


if (!isset($_SESSION['user_id']) || !$auth->isSuperAdmin($_SESSION['user_id'])) {
    header('Location: ../../login.php');
    exit();
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $inventory = new Inventory($conn);
    $itemId = $_POST['item_id'];
    $branchId = $_POST['branch_id'];
    $quantity = $_POST['quantity'];

    if ($inventory->assignItemToBranch($itemId, $branchId, $quantity)) {
        echo "Item assigned to branch successfully.";
    } else {
        echo "Failed to assign item to branch.";
    }
}
?>

<?php include __DIR__ . '/../../includes/header.php'; ?>
<div class="container mt-5">
        <h1 class="mb-4">Assign Item to Branch</h1>
        <form action="assign_item.php" method="POST">
            <div class="form-group">
                <label for="item_id">Item ID</label>
                <input type="number" name="item_id" id="item_id" class="form-control" required>
            </div>
            <div class="form-group">
                <label for="branch_id">Branch ID</label>
                <input type="number" name="branch_id" id="branch_id" class="form-control" required>
            </div>
            <div class="form-group">
                <label for="quantity">Quantity</label>
                <input type="number" name="quantity" id="quantity" class="form-control" required>
            </div>
            <button type="submit" class="btn btn-primary">Assign Item</button>
        </form>
    </div>
<?php include __DIR__ . '/../../includes/footer.php'; ?>