<?php
require_once "base.php";
session_start();
if(!isset($_SESSION["userId"]))header("Location: login.php");
head("Your items");
nav();
?>
<div>
<h1>Items you borrowed</h1>
<?php
require "db.php";
$stmt=$pdo->prepare("SELECT i.id AS id, l.id AS loanId, i.name AS name, st.name AS state, l.timeLoaned, CONCAT(p.name,' ',p.surname) AS owner 
                     FROM loans AS l 
                     LEFT JOIN item AS i ON l.itemId=i.id 
                     LEFT JOIN states AS st ON i.state=st.id 
                     LEFT JOIN people AS p ON i.owner=p.id
                     WHERE timeReturned IS NULL AND loanerId=?;");
$stmt->execute([htmlspecialchars($_SESSION["personId"])]);
if($stmt->rowCount()>0):
?>
<table class="table table-striped table-sm">
    <thead>
        <tr>
            <td>id</td>
            <td>name</td>
            <td>state</td>
            <td>time loaned</td>
            <td>owner</td>
            <td>actions</td>
        </tr>
    </thead>
    <tbody>
        <?php while($row=$stmt->fetch()): ?>
        <tr>
            <td><?= $row['id'] ?></td>
            <td><?= $row['name'] ?></td>
            <td><?= $row['state'] ?></td>
            <td><?= $row['timeLoaned'] ?></td>
            <td><?= $row['owner'] ?></td>
	    <td><a class="btn btn-warning" href="/storage/item/return.php?id=<?= $row['loanId'] ?>">Return<a></td>
        </tr>
        <?php endwhile; ?>
    </tbody>
</table>
<?php else: ?>
    <p class="text-secondary">You have not borrowed any items</p>
<?php endif; ?>
<h1>Your items</h1>
<a class="btn btn-success" href="item/add.php">Add item</a>
<?php
require "db.php";
$stmt=$pdo->prepare("SELECT i.id AS id, i.name AS name, st.name AS state, sh.name AS shelf, wh.displayName AS warehouse, ad.street,ad.block,ad.flat,ad.postCode,ad.country
                       FROM item AS i 
                       LEFT JOIN states AS st ON i.state=st.id 
                       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.owner=?;");
$stmt->execute([htmlspecialchars($_SESSION["personId"])]);
if($stmt->rowCount()>0):
?>
<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>
            <td>actions</td>
        </tr>
    </thead>
    <tbody>
        <?php while($row=$stmt->fetch()): ?>
        <tr>
            <td><?= $row['id'] ?></td>
            <td><?= $row['name'] ?></td>
            <td><?= $row['state'] ?></td>
            <td><?= $row['shelf'] ?></td>
            <td><?= $row['warehouse'] ?></td>
            <td><?= $row['street'].' '.$row["block"].(isset($row["flat"])?'/'.$row["flat"]:"").' '.$row["postCode"].' '.$row["country"]?></td>
            <td><button onclick="location.href='/storage/item/view.php?id=<?= $row['id'] ?>';" class="btn btn-primary">View</button></td>
        </tr>
        <?php endwhile; ?>
    </tbody>
</table>
<?php else: ?>
    <p class="text-secondary">You don't have any items</p>
<?php endif; ?>
</div>
<?php
footer();
?>
