<?php
if(!isset($_GET['id'])){
    header("Location: ./");
    exit;
}
$id = htmlspecialchars($_GET['id']);
if($_SERVER['REQUEST_METHOD']==="POST"){
    require "../db.php";
    $name = htmlspecialchars($_POST["name"]);
    $description = htmlspecialchars($_POST["description"]);
    $state = htmlspecialchars($_POST["state"]);
    $shelf = htmlspecialchars($_POST["shelf"]);

    $stmtCurrent=$pdo->prepare("SELECT storageShelf FROM item WHERE id=?");
    $stmtCurrent->execute([$id]);
    $current=$stmtCurrent->fetch();

    if($current && $current['storageShelf'] && $current['storageShelf'] != $shelf){
        $stmtMove=$pdo->prepare("INSERT INTO moves (itemId, previousLocation, newLocation) VALUES (?,?,?)");
        $stmtMove->execute([$id, $current['storageShelf'], $shelf]);
    }

    $stmt=$pdo->prepare("UPDATE item SET name=?, description=?, state=?, storageShelf=? WHERE id=?");
    $stmt->execute([$name,$description,$state,$shelf,$id]);
    header("Location: view.php?id=".$id);
    exit;
}
require_once "../base.php";
session_start();
if(!isset($_SESSION["userId"]))header("Location: /storage/login.php");
head("Modify item");
nav();
require "../db.php";
$stmt=$pdo->prepare("SELECT i.id AS id, i.name AS name, i.description AS description, state, storageShelf AS shelf, wh.displayName AS warehouse, sh.warehouseId AS warehouseId, CONCAT(ad.street,' ',ad.block,'/',ad.flat,' ',ad.postCode,' ',ad.country) AS address 
                       FROM item AS i 
                       LEFT JOIN shelf AS sh ON i.storageShelf=sh.id 
                       LEFT JOIN warehouse AS wh ON sh.warehouseId=wh.id 
                       LEFT JOIN address AS ad ON wh.address=ad.id 
                       WHERE i.id=?;");
$stmt->execute([$id]);
?>
<div>
<h1>Modify item</h1>
<?php
if($stmt->rowCount()>0):
?>
<form method="POST">
    <table class="table table-striped table-sm">
        <thead>
            <tr>
                <td>id</td>
                <td>name</td>
                <td>state</td>
                <td>shelf</td>
                <td>warehouse</td>
                <td>location</td>
            </tr>
        </thead>
        <tbody>
            <?php $row=$stmt->fetch(); ?>
            <tr>
                <td><?= $row['id'] ?></td>
                <td><input type="text" name="name" value="<?= $row['name'] ?>"></td>
                <td>
                    <select name="state">
                        <?php 
                        $stmt2=$pdo->prepare("SELECT * FROM states;");
                        $stmt2->execute();
                        while($row2=$stmt2->fetch()): ?>
                            <option value="<?= $row2['id'] ?>" <?= $row2['id']==$row['state']?"selected":"" ?>><?= $row2['name'] ?></option>
                        <?php endwhile; ?>
                    </select>
                </td>
                <td>
                    <select name="shelf">
                        <?php 
                        $stmt2=$pdo->prepare("SELECT * FROM shelf WHERE warehouseId=?;");
                        $stmt2->execute([$row['warehouseId']]);
                        while($row2=$stmt2->fetch()): ?>
                            <option value="<?= $row2['id'] ?>" <?= $row2['id']==$row['shelf']?"selected":"" ?>><?= $row2['name'] ?></option>
                        <?php endwhile; ?>
                    </select>
                </td>
                <td><?= $row['warehouse'] ?></td>
                <td><?= $row['address'] ?></td>
            </tr>
        </tbody>
    </table>
    <div>
        <label for="description"><strong>Description</strong></label><br>
        <textarea id="description" name="description" rows="4" style="width: 100%"><?= htmlspecialchars($row['description'] ?? '') ?></textarea>
    </div>
    <br>
    <a class="btn btn-danger" href="view.php?id=<?= $id ?>">Cancel</a> <input type="submit" class="btn btn-success" value="Apply">
</form>
<?php
endif;
footer();
?>
