<?php
require_once __DIR__ . '/_logic.php';

/* ==============================
   FILTERS
============================== */

$scopeCompanyId = inventoryCompanyScope();

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

$params = [];
$where = [];

/* Company Scope */
if ($scopeCompanyId !== null) {
    $where[] = "m.current_holder_company_id = ?";
    $params[] = $scopeCompanyId;
}

/* Status Filter */
if ($filterStatus !== '') {
    $where[] = "m.status = ?";
    $params[] = strtoupper($filterStatus);
}

/* Search Filter */
if ($filterSearch !== '') {
    $where[] = "(m.meter_number LIKE ? OR m.serial_number LIKE ?)";
    $params[] = likeParam($filterSearch);
    $params[] = likeParam($filterSearch);
}

$whereSql = $where ? "WHERE " . implode(" AND ", $where) : "";

/* ==============================
   FETCH METERS
============================== */

$sql = "
    SELECT m.*, c.company_name
    FROM meters m
    LEFT JOIN companies c 
        ON m.current_holder_company_id = c.id
    $whereSql
    ORDER BY m.created_at DESC
";

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

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

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

<div class="container">

    <div class="page-header">
        <h2>Inventory Management</h2>
        <p class="page-subtitle">
            Physical device tracking & lifecycle management
        </p>
    </div>

    <!-- ACTION BAR -->
    <div class="topbar">
        <a class="btn" href="add.php">+ Add Meter</a>
        <a class="btn secondary" href="bulk_upload.php">Bulk Upload</a>
        <a class="btn secondary" href="template_download.php">Download Template</a>
    </div>

    <!-- FILTER BAR -->
    <form method="GET" style="margin-top:20px; margin-bottom:20px;">
        <div style="display:flex; gap:10px; flex-wrap:wrap;">

            <input type="text" 
                   name="search"
                   placeholder="Search meter or serial..."
                   value="<?= htmlspecialchars($filterSearch) ?>">

            <select name="status">
                <option value="">All Status</option>
                <?php foreach (['IN_STOCK','WITH_DISTRIBUTOR','INSTALLED','FAULTY'] as $status): ?>
                    <option value="<?= $status ?>"
                        <?= ($filterStatus === $status ? 'selected' : '') ?>>
                        <?= $status ?>
                    </option>
                <?php endforeach; ?>
            </select>

            <button class="btn">Filter</button>

        </div>
    </form>

    <!-- TABLE -->
    <div class="table-wrap">
        <table>
            <thead>
                <tr>
                    <th>Meter</th>
                    <th>Serial</th>
                    <th>Type</th>
                    <th>Holder</th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody>

            <?php if (!$meters): ?>
                <tr>
                    <td colspan="5">No meters found.</td>
                </tr>
            <?php else: ?>

            <?php foreach ($meters as $meter): ?>
                <tr>
                    <td><?= htmlspecialchars($meter['meter_number']) ?></td>
                    <td><?= htmlspecialchars($meter['serial_number'] ?? '-') ?></td>
                    <td><?= htmlspecialchars($meter['meter_type']) ?></td>
                    <td><?= htmlspecialchars($meter['company_name'] ?? 'Warehouse') ?></td>
                    <td>
                        <span class="status <?= invStatusLabel($meter['status']) ?>">
                            <?= invStatusLabel($meter['status']) ?>
                        </span>
                    </td>
                </tr>
            <?php endforeach; ?>

            <?php endif; ?>

            </tbody>
        </table>
    </div>

</div>

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