<?php
session_start();

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

$basePath = '../../';
include '../../includes/header.php';

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

$search = trim($_GET['search'] ?? '');

$sql = "
    SELECT 
        i.*,
        c.name AS category_name,
        u.name AS unit_name,
        t.name AS type_name
    FROM items i
    LEFT JOIN categories c ON i.category_id = c.id
    LEFT JOIN units u ON i.unit_id = u.id
    LEFT JOIN item_types t ON i.type_id = t.id
";

$params = [];

if (!empty($search)) {
    $sql .= " WHERE i.name LIKE ? OR i.barcode LIKE ?";
    $params[] = '%' . $search . '%';
    $params[] = '%' . $search . '%';
}

$sql .= " ORDER BY i.name ASC";

$stmt = $db->prepare($sql);
$stmt->execute($params);

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

<div class="container mt-4">

    <div class="d-flex justify-content-between align-items-center mb-3">
        <h3>Item Management</h3>

        <a href="create.php" class="btn btn-primary">
            Add New Item
        </a>
    </div>

    <form method="GET" class="mb-3">
        <div class="row">
            <div class="col-md-10">
                <input
                    type="text"
                    name="search"
                    class="form-control"
                    placeholder="Search by item name or barcode..."
                    value="<?= htmlspecialchars($search) ?>"
                >
            </div>

            <div class="col-md-2">
                <button class="btn btn-success w-100">
                    Search
                </button>
            </div>
        </div>
    </form>

    <div class="card shadow-sm">
        <div class="card-body table-responsive">

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

                <thead class="table-dark">
                    <tr>
                        <th>ID</th>
                        <th>Image</th>
                        <th>Name</th>
                        <th>Category</th>
                        <th>Price</th>
                        <th>GST %</th>
                        <th>Stock</th>
                        <th>Barcode</th>
                        <th>Unit</th>
                        <th>Type</th>
                        <th width="180">Actions</th>
                    </tr>
                </thead>

                <tbody>

                <?php if (empty($items)): ?>
                    <tr>
                        <td colspan="11" class="text-center">
                            No items found.
                        </td>
                    </tr>
                <?php endif; ?>

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

                    <?php
                    $imagePath = !empty($item['image_path'])
                    ? '../../assets/images/' . trim($item['image_path'])
                    : '../../assets/images/no_image.jpg';
                    ?>

                    <tr>
                        <td><?= $item['id'] ?></td>

                        <td>
                            <img
                                src="<?= $imagePath ?>"
                                width="60"
                                height="60"
                                style="object-fit: cover;"
                               
                            >
                        </td>

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

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

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

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

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

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

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

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

                        <td>
                            <a
                                href="edit.php?id=<?= $item['id'] ?>"
                                class="btn btn-warning btn-sm"
                            >
                                Edit
                            </a>

                            <a
                                href="delete.php?id=<?= $item['id'] ?>"
                                class="btn btn-danger btn-sm"
                                onclick="return confirm('Delete this item?')"
                            >
                                Delete
                            </a>
                        </td>
                    </tr>

                <?php endforeach; ?>

                </tbody>

            </table>

        </div>
    </div>

</div>

<?php include '../../includes/footer.php'; ?>