<?php
$pageTitle = 'Products';
require_once __DIR__ . '/../../app/includes/header.php'; require_role_access('product:view');
require_once __DIR__ . '/../../app/includes/product_helpers.php';
$products = fetch_filtered_products($_GET);
$brands = db()->query("SELECT DISTINCT brand FROM products WHERE brand IS NOT NULL AND brand <> '' ORDER BY brand")->fetchAll(PDO::FETCH_COLUMN);
?>
<div class="page-head">
    <div><h1>Products</h1><p>Manage solar products, components, accessories, and service items.</p></div>
    <div class="d-flex flex-wrap gap-2">
        <?php if (can_manage_products()): ?>
            <a class="btn btn-primary" href="<?= APP_BASE ?>/products/create.php"><i class="bi bi-plus-lg me-1"></i>Add Product</a>
            <a class="btn btn-info text-white" href="<?= APP_BASE ?>/products/import.php"><i class="bi bi-upload me-1"></i>Import CSV</a>
        <?php endif; ?>
        <a class="btn btn-outline-secondary" href="<?= APP_BASE ?>/products/sample-csv.php"><i class="bi bi-file-earmark-text me-1"></i>Download Sample CSV</a>
        <?php if (can_export_products()): ?><a class="btn btn-dark" href="<?= APP_BASE ?>/products/export.php"><i class="bi bi-download me-1"></i>Export Products CSV</a><?php endif; ?>
    </div>
</div>
<?php if (!empty($_SESSION['product_import_errors'])): ?>
    <div class="alert alert-warning">Some CSV rows failed during the last import. <a href="<?= APP_BASE ?>/products/import-errors.php">Download error report CSV</a></div>
<?php endif; ?>

<form class="card app-card p-3 mb-4" method="get">
    <div class="row g-3 align-items-end">
        <div class="col-md-4"><label class="form-label">Search</label><input class="form-control" name="search" value="<?= e($_GET['search'] ?? '') ?>" placeholder="Product name, brand, model"></div>
        <div class="col-md-3"><label class="form-label">Category</label><select class="form-select" name="category"><option value="">All categories</option><?php foreach (PRODUCT_CATEGORIES as $cat): ?><option value="<?= e($cat) ?>" <?= ($_GET['category'] ?? '')===$cat?'selected':'' ?>><?= e($cat) ?></option><?php endforeach; ?></select></div>
        <div class="col-md-2"><label class="form-label">Brand</label><select class="form-select" name="brand"><option value="">All brands</option><?php foreach ($brands as $brand): ?><option value="<?= e($brand) ?>" <?= ($_GET['brand'] ?? '')===$brand?'selected':'' ?>><?= e($brand) ?></option><?php endforeach; ?></select></div>
        <div class="col-md-2"><label class="form-label">Status</label><select class="form-select" name="status"><option value="">All status</option><?php foreach (PRODUCT_STATUSES as $status): ?><option value="<?= e($status) ?>" <?= ($_GET['status'] ?? '')===$status?'selected':'' ?>><?= e(ucfirst($status)) ?></option><?php endforeach; ?></select></div>
        <div class="col-md-1 d-flex gap-2"><button class="btn btn-primary" type="submit">Apply</button><a class="btn btn-outline-secondary" href="<?= APP_BASE ?>/products/index.php">Reset</a></div>
    </div>
</form>

<div class="card app-card"><div class="table-responsive"><table class="table align-middle mb-0">
<thead><tr><th>SL</th><th>Product Category</th><th>Product Name</th><th>Description</th><th>Brand</th><th>Model</th><th>Unit</th><th>Unit Price</th><th>Status</th><th>Action</th></tr></thead><tbody>
<?php foreach ($products as $i => $p): ?><tr>
    <td><?= $i + 1 ?></td><td><?= e($p['product_category']) ?></td><td><?= e($p['product_name']) ?></td><td><?= e($p['description']) ?></td><td><?= e($p['brand']) ?></td><td><?= e($p['model']) ?></td><td><?= e($p['unit']) ?></td><td><?= money((float)$p['unit_price']) ?></td>
    <td><span class="badge text-bg-<?= $p['status']==='active'?'success':'secondary' ?>"><?= e(ucfirst($p['status'])) ?></span></td>
    <td class="text-nowrap"><a class="btn btn-sm btn-outline-secondary" href="<?= APP_BASE ?>/products/edit.php?id=<?= (int)$p['id'] ?>&view=1">View</a><?php if (can_manage_products()): ?> <a class="btn btn-sm btn-outline-primary" href="<?= APP_BASE ?>/products/edit.php?id=<?= (int)$p['id'] ?>">Edit</a> <form class="d-inline" method="post" action="<?= APP_BASE ?>/products/update.php?id=<?= (int)$p['id'] ?>"><?= csrf_field() ?><input type="hidden" name="action" value="toggle_status"><button class="btn btn-sm btn-outline-warning" type="submit"><?= $p['status']==='active'?'Inactivate':'Activate' ?></button></form><?php endif; ?></td>
</tr><?php endforeach; ?>
<?php if (!$products): ?><tr><td colspan="10" class="text-center text-muted py-4">No products found.</td></tr><?php endif; ?>
</tbody></table></div></div>
<?php require_once __DIR__ . '/../../app/includes/footer.php'; ?>
