<?php
declare(strict_types=1);

require_once __DIR__ . '/includes/iscms_layout.php';

$conn = iscms_init();
$pageTitle = 'Products register';
$activeNav = 'products';

$sql = "SELECT p.id, p.product_name, p.product_code, p.brand, p.status, p.hs_code,
               o.org_name, pc.name AS category_name
        FROM products p
        INNER JOIN organizations o ON p.org_id = o.id
        LEFT JOIN product_categories pc ON p.category_id = pc.id
        ORDER BY p.updated_at DESC
        LIMIT 500";
$result = $conn->query($sql);
$rows = $result ? $result->fetch_all(MYSQLI_ASSOC) : [];
$dbError = $result ? null : $conn->error;

iscms_render_head($pageTitle);
iscms_render_sidebar($activeNav);
iscms_render_topbar($pageTitle);
?>
    <div class="page-header">
        <h1>Products register</h1>
        <p>Products submitted by organisations for certification, permits, and surveillance.</p>
    </div>

    <div class="section-card">
        <div class="section-header">
            <h2><i class="ri-box-3-line"></i> Products</h2>
            <span class="text-muted" style="font-size:.75rem"><?= count($rows) ?> shown</span>
        </div>
        <div class="table-responsive">
            <?php if ($dbError): ?>
                <div class="alert alert-danger m-3" style="font-size:.85rem">Unable to load data: <?= iscms_h($dbError) ?></div>
            <?php else: ?>
            <table class="ssbs-table">
                <thead>
                    <tr>
                        <th>Product</th>
                        <th>Code</th>
                        <th>Brand</th>
                        <th>Organisation</th>
                        <th>Category</th>
                        <th>HS</th>
                        <th>Status</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                    <tr class="empty-row"><td colspan="7"><i class="ri-inbox-line me-2"></i>No products</td></tr>
                    <?php else: ?>
                        <?php foreach ($rows as $r): ?>
                        <tr>
                            <td style="font-weight:600; color:var(--ssbs-blue)"><?= iscms_h($r['product_name']) ?></td>
                            <td class="iscms-mono"><?= iscms_val($r['product_code'] ?? null) ?></td>
                            <td><small><?= iscms_val($r['brand'] ?? null) ?></small></td>
                            <td><small><?= iscms_h($r['org_name']) ?></small></td>
                            <td><small><?= iscms_val($r['category_name'] ?? null) ?></small></td>
                            <td class="iscms-mono"><?= iscms_val($r['hs_code'] ?? null) ?></td>
                            <td><?= iscms_status_badge((string) ($r['status'] ?? '')) ?></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
<?php
iscms_render_footer();
