<?php
if ($_SERVER['REQUEST_METHOD'] === "POST") {
    require "../db.php";
    $itemId      = (int)$_POST['itemId'];
    $newLocation = (int)$_POST['newLocation'];

    // Fetch current shelf to log as previousLocation
    $stmt = $pdo->prepare("SELECT storageShelf FROM item WHERE id = ?;");
    $stmt->execute([$itemId]);
    $item = $stmt->fetch();

    if (!$item) {
        die("Item not found.");
    }

    $previousLocation = $item['storageShelf'];

    // Update item location
    $stmt = $pdo->prepare("UPDATE item SET storageShelf = ? WHERE id = ?;");
    $stmt->execute([$newLocation, $itemId]);

    // Log the move
    $stmt = $pdo->prepare("INSERT INTO moves (itemId, previousLocation, newLocation) VALUES (?, ?, ?);");
    $stmt->execute([$itemId, $previousLocation, $newLocation]);

    header("Location: view.php?id=".$itemId);
    exit;
}

require_once "../base.php";
session_start();
if (!isset($_SESSION["userId"])) header("Location: /storage/login.php");
head("Move item");
nav();
require "../db.php";

$itemId = (int)($_GET['id'] ?? 0);
if (!$itemId) die("No item specified.");

$stmt = $pdo->prepare("SELECT item.*, shelf.name AS shelfName, warehouse.displayName AS warehouseName
                        FROM item
                        LEFT JOIN shelf ON item.storageShelf = shelf.id
                        LEFT JOIN warehouse ON shelf.warehouseId = warehouse.id
                        WHERE item.id = ?;");
$stmt->execute([$itemId]);
$item = $stmt->fetch();

if (!$item) die("Item not found.");
?>
<div>
    <h1>Move item</h1>
    <p>
        Moving: <strong><?= htmlspecialchars($item['name']) ?></strong><br>
        Current location: <strong><?= htmlspecialchars($item['warehouseName'] . ' / ' . $item['shelfName']) ?></strong>
    </p>
    <form method="POST">
        <input type="hidden" name="itemId" value="<?= $itemId ?>">
        <table class="table table-striped table-sm">
            <thead>
                <tr>
                    <td>New shelf</td>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                        <select name="newLocation" required>
                            <?php
                            $stmt = $pdo->prepare("SELECT * FROM warehouse;");
                            $stmt->execute();
                            while ($row = $stmt->fetch()):
                                ?><optgroup label="<?= htmlspecialchars($row['displayName']) ?>"><?php
                                $stmt2 = $pdo->prepare("SELECT * FROM shelf WHERE warehouseId = ? AND id != ?;");
                                $stmt2->execute([$row['id'],$item['storageShelf']]);
                                while ($row2 = $stmt2->fetch()): ?>
                                    <option value="<?= $row2['id'] ?>"
                                        <?= $row2['id'] == $item['storageShelf'] ? 'selected' : '' ?>>
                                        <?= htmlspecialchars($row2['name']) ?>
                                    </option>
                                <?php endwhile; ?>
                                </optgroup>
                            <?php endwhile; ?>
                        </select>
                    </td>
                </tr>
            </tbody>
        </table>
        <a class="btn btn-danger" href="view.php?id=<?= $itemId ?>">Cancel</a>
        <input type="submit" class="btn btn-success" value="Move">
    </form>
</div>
<?php
footer();
?>
