<?php
require_once "base.php";
session_start();
if(!isset($_SESSION["userId"]))header("Location: login.php");
head("Your items");
nav();
?>
<div>
<h1>Items you lent</h1>
<?php
require "db.php";
$stmt=$pdo->prepare("SELECT l.id AS id, i.name AS name, st.name AS state, i.id AS itemId, CONCAT(p.name,' ',p.surname) AS person, l.timeLoaned
		       FROM loans AS l
                       LEFT JOIN item AS i ON i.id = l.itemId
		       LEFT JOIN states AS st ON i.state=st.id 
		       LEFT JOIN people AS p ON p.id=loanerId
                       WHERE i.owner=? AND l.timeReturned IS NULL");
$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>loaner</td>
	    <td>time loaned</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['person'] ?></td>
	    <td><?= $row['timeLoaned'] ?></td>
	    <td>
		<button onclick="location.href='/storage/item/view.php?id=<?= $row['itemId'] ?>';" class="btn btn-primary">View</button>
		<button onclick="location.href='/storage/item/return.php?id=<?= $row['id'] ?>';" class="btn btn-warning">Return</button>
	    </td>
        </tr>
        <?php endwhile; ?>
    </tbody>
</table>
<?php else: ?>
    <p class="text-secondary">You haven't leant any items</p>
<?php endif; ?>
</div>
<?php
footer();
?>
