<?php
if($_SERVER['REQUEST_METHOD']==="POST"){
    require "../db.php";
    $name = htmlspecialchars($_POST["name"]);
    $state = htmlspecialchars($_POST["state"]);
    $shelf = htmlspecialchars($_POST["shelf"]);
    $owner = htmlspecialchars($_POST['owner']);
    $description = htmlspecialchars($_POST['description']);


    $stmt=$pdo->prepare("INSERT INTO item (name,state,storageShelf,owner,description) VALUES (?,?,?,?,?);");
    $stmt->execute([$name,$state,$shelf,$owner,$description]);
    header("Location: ../");
    exit;
}
require_once "../base.php";
session_start();
if(!isset($_SESSION["userId"]))header("Location: /storage/login.php");
head("Add item");
nav();
require "../db.php";
?>
<div>
<h1>Add item</h1>
<form method="POST">
    <table class="table table-striped table-sm">
        <thead>
            <tr>
                <td>name</td>
                <td>state</td>
                <td>shelf</td>
                <td>owner</td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><input type="text" name="name" value="<?= $row['name'] ?>" style="width: 100%"></td>
                <td>
                    <select name="state">
                        <?php 
                        $stmt2=$pdo->prepare("SELECT * FROM states;");
                        $stmt2->execute();
                        while($row2=$stmt2->fetch()): ?>
                            <option value="<?= $row2['id'] ?>"><?= $row2['name'] ?></option>
                        <?php endwhile; ?>
                    </select>
                </td>
                <td>
                    <select name="shelf">
                        <?php 
                        $stmt=$pdo->prepare("SELECT * FROM warehouse");
                        $stmt->execute();
                            while($row=$stmt->fetch()):
                                ?><optgroup label="<?= $row['displayName'] ?>"><?php
                                $stmt2=$pdo->prepare("SELECT * FROM shelf WHERE warehouseId=?;");
                                $stmt2->execute([$row['id']]);
                                while($row2=$stmt2->fetch()): ?>
                                    <option value="<?= $row2['id'] ?>"><?= $row2['name'] ?></option>
                                <?php endwhile; ?>
                                </optgroup>
                            <?php endwhile;?>
                    </select>
                </td>
                <td>
                    <select name="owner">
                        <?php 
                        $stmt2=$pdo->prepare("SELECT * FROM people");
                        $stmt2->execute();
                        while($row2=$stmt2->fetch()): ?>
                            <option value="<?= $row2['id'] ?>"><?= $row2['name'].' '.$row2['surname'] ?></option>
                        <?php endwhile; ?>
                    </select>
                </td>
            </tr>
        </tbody>
    </table>
    <div>
        <label for="description"><strong>Description</strong></label><br>
        <textarea id="description" name="description" rows="4" style="width: 100%"></textarea>
    </div>
    <a class="btn btn-danger" href="../">Cancel</a> <input type="submit" class="btn btn-success" value="Apply">
</form>
<?php
footer();
?>
