<?php
session_start();

require_once '../../config/database.php';
require_once '../../includes/auth.php';

$db = (new Database())->connect();

/*
    Initial items
*/
$stmt = $db->prepare("
    SELECT *
    FROM items
    ORDER BY name ASC
    LIMIT 50
");
$stmt->execute();

$items = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html>
<head>
    <title>Select Item</title>

    <link
        href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
        rel="stylesheet"
    >

    <style>
        body {
            background: #f8f9fa;
        }

        .item-row {
            cursor: pointer;
        }

        .item-row:hover,
        .item-row.active {
            background: #0d6efd;
            color: white;
        }

        .search-box {
            font-size: 22px;
            height: 60px;
        }

        .table-container {
            max-height: 550px;
            overflow-y: auto;
        }
    </style>
</head>
<body>

<div class="container-fluid p-3">

    <div class="card shadow-sm">

        <div class="card-header bg-dark text-white">
            Item Search (F1)
        </div>

        <div class="card-body">

            <!-- SEARCH -->
            <input
                type="text"
                id="search"
                class="form-control search-box mb-3"
                placeholder="Search item / barcode"
                autofocus
                onkeyup="handleSearchKeyUp(event)"
            >

            <!-- RESULTS -->
            <div class="table-container">

                <table class="table table-bordered table-hover">

                    <thead class="table-dark sticky-top">
                        <tr>
                            <th>Item</th>
                            <th width="120">Barcode</th>
                            <th width="120">Price</th>
                        </tr>
                    </thead>

                    <tbody id="item-results">

                    <?php foreach ($items as $item): ?>

                        <tr
                            class="item-row"
                            data-id="<?= $item['id'] ?>"
                            ondblclick="selectItem(<?= $item['id'] ?>)"
                        >

                            <td>
                                <?= htmlspecialchars($item['name']) ?>
                            </td>

                            <td>
                                <?= htmlspecialchars($item['barcode'] ?? '') ?>
                            </td>

                            <td>
                                ₹ <?= number_format($item['price'], 2) ?>
                            </td>

                        </tr>

                    <?php endforeach; ?>

                    </tbody>

                </table>

            </div>

        </div>

    </div>

</div>



</body>
</html>