<?php

ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(E_ALL);

require_once __DIR__ . '/../../includes/auth.php';
requireLogin();
requirePermission('view_inventory');

require_once __DIR__ . '/../../config/database.php';

$company_id = $_SESSION['company_id'] ?? 0;

$search   = $_GET['search'] ?? '';
$category = $_GET['category'] ?? '';

/* ===============================
LOAD CATEGORIES
================================ */

$stmt = $pdo->prepare("
SELECT *
FROM inventory_categories
WHERE company_id = ?
ORDER BY category_name
");

$stmt->execute([$company_id]);
$categories = $stmt->fetchAll(PDO::FETCH_ASSOC);

/* ===============================
FETCH ITEMS (STILL USING inventory_products)
================================ */

$sql = "
SELECT p.*, c.category_name
FROM inventory_products p
LEFT JOIN inventory_categories c
ON p.category_id = c.id
WHERE p.company_id = ?
";

$params = [$company_id];

if($search){
    $sql .= " AND (
        p.product_name LIKE ?
        OR p.sku LIKE ?
        OR p.brand LIKE ?
        OR p.model LIKE ?
    )";

    $params[] = "%$search%";
    $params[] = "%$search%";
    $params[] = "%$search%";
    $params[] = "%$search%";
}

if($category){
    $sql .= " AND p.category_id = ?";
    $params[] = $category;
}

$sql .= " ORDER BY p.product_name";

$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);

/* ===============================
LOAD LAYOUT
================================ */

$breadcrumb = "Items";
require_once __DIR__ . '/../../includes/layout.php';
?>

<div class="container">

<div class="page-header">
<h2>Items</h2>
<p class="page-subtitle">
Products and services used in quotations and invoices
</p>
</div>

<div class="topbar">

<a class="btn btn-primary" href="add_product.php">
+ Add Item
</a>

<form method="GET" style="display:inline-flex;gap:10px;margin-left:20px">

<input
type="text"
name="search"
placeholder="Search item..."
value="<?= htmlspecialchars($search) ?>"
style="padding:6px;width:220px"
>

<select name="category" style="padding:6px">
<option value="">All Categories</option>

<?php foreach($categories as $cat): ?>
<option
value="<?= $cat['id'] ?>"
<?= $category == $cat['id'] ? 'selected' : '' ?>
>
<?= htmlspecialchars($cat['category_name']) ?>
</option>
<?php endforeach; ?>

</select>

<button class="btn btn-dark">Search</button>

</form>

</div>

<div class="table-wrap">

<table class="data-table">

<thead>
<tr>
<th>Item</th>
<th>Type</th>
<th>Category</th>
<th>Brand</th>
<th>Model</th>
<th>SKU</th>
<th>Cost</th>
<th>Selling</th>
<th>Stock</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>

<tbody>

<?php if(!$products): ?>

<tr>
<td colspan="11">No items found.</td>
</tr>

<?php else: ?>

<?php foreach($products as $p): ?>

<tr>

<td><?= htmlspecialchars($p['product_name']) ?></td>

<td><?= htmlspecialchars($p['item_type'] ?? 'PRODUCT') ?></td>

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

<td><?= htmlspecialchars($p['brand'] ?? '') ?></td>

<td><?= htmlspecialchars($p['model'] ?? '') ?></td>

<td><?= htmlspecialchars($p['sku'] ?? '') ?></td>

<td>R <?= number_format((float)$p['cost_price'],2) ?></td>

<td>R <?= number_format((float)$p['selling_price'],2) ?></td>

<td>
<?php if(!empty($p['track_inventory'])): ?>
    <?= (int)($p['stock_qty'] ?? 0) ?>
<?php else: ?>
    —
<?php endif; ?>
</td>

<td><?= htmlspecialchars($p['status'] ?? 'active') ?></td>

<td>
<a href="edit_product.php?id=<?= (int)$p['id'] ?>" class="btn-small edit">
Edit
</a>
</td>

</tr>

<?php endforeach; ?>

<?php endif; ?>

</tbody>

</table>

</div>

</div>

<?php require_once __DIR__ . '/../../includes/layout_footer.php'; ?>