<?php
require_once "../base.php";
session_start();
if(!isset($_SESSION["bootstrapAdmin"])){
	if(!isset($_SESSION["userId"]))header("Location: /storage/login.php");
	if(!isAdmin($_SESSION["userId"])){
	    http_response_code(403);
	    print("Forbidden, you don't have access to this resource.");
	    exit;
	}
}
head("Add shelf");
nav();
?>
<div>
<h1>Shelves</h1>
<?php if(isset($_SESSION["bootstrapAdmin"])): ?>
<p>Creating a shelf. Shelfs are places to store items inside warehouses.</p>
<?php endif;
require "../db.php";
if($_SERVER["REQUEST_METHOD"]==="POST"){
    $name = htmlspecialchars($_POST['name']);
    $warehouseId = htmlspecialchars($_POST['warehouseId']);
    $stmt=$pdo->prepare("INSERT INTO shelf (name,warehouseId) VALUES (?,?)");
    $stmt->execute([$name,$warehouseId]);

    if(!isset($_SESSION["bootstrapAdmin"])){
	    header("Location: list.php");
    } else {
	    header("Location: /storage/logout.php");
    }
}
?>
<form method="POST">
<table class="table table-striped table-sm">
    <thead>
        <tr>
            <td>name</td>
            <td>warehouse</td>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td><input type="text" name="name"></td>
            <td>
                <select name="warehouseId">
                    <?php
                        $stmt2=$pdo->prepare("SELECT id,displayName FROM warehouse");
                        $stmt2->execute([]);
                        while($row2=$stmt2->fetch()): ?>
                            <option value="<?= $row2['id'] ?>"><?= $row2['displayName'] ?></option>
                        <?php endwhile; ?>
                </select>
            </td>
        </tr>
    </tbody>
</table>
<a class="btn btn-danger" href="list.php">Cancel</a> <input type="submit" value="Add" class="btn btn-success">
</form>
</div>
<?php
footer();
?>
