<?php
declare(strict_types=1);

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

$conn = iscms_init();
$pageTitle = 'Product categories';
$activeNav = 'products';

$sql = "SELECT c.id, c.name, c.code, c.hs_code, c.is_regulated, c.is_active,
               p.name AS parent_name
        FROM product_categories c
        LEFT JOIN product_categories p ON c.parent_id = p.id
        ORDER BY c.name ASC
        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>Product categories</h1>
        <p>Regulated product classification and HS alignment.</p>
    </div>

    <div class="section-card">
        <div class="section-header">
            <h2><i class="ri-price-tag-3-line"></i> Categories</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>Name</th>
                        <th>Code</th>
                        <th>HS code</th>
                        <th>Parent</th>
                        <th>Regulated</th>
                        <th>Active</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                    <tr class="empty-row"><td colspan="6"><i class="ri-inbox-line me-2"></i>No categories</td></tr>
                    <?php else: ?>
                        <?php foreach ($rows as $r): ?>
                        <tr>
                            <td style="font-weight:600; color:var(--ssbs-blue)"><?= iscms_h($r['name']) ?></td>
                            <td class="iscms-mono"><?= iscms_val($r['code'] ?? null) ?></td>
                            <td class="iscms-mono"><?= iscms_val($r['hs_code'] ?? null) ?></td>
                            <td><small><?= iscms_val($r['parent_name'] ?? null) ?></small></td>
                            <td><?= !empty($r['is_regulated']) ? '<span class="badge bg-warning-subtle text-dark" style="font-size:.68rem">Yes</span>' : '<span class="badge bg-secondary-subtle text-secondary" style="font-size:.68rem">No</span>' ?></td>
                            <td><?= !empty($r['is_active']) ? '<span class="badge bg-success-subtle text-success" style="font-size:.68rem">Yes</span>' : '<span class="badge bg-secondary-subtle text-secondary" style="font-size:.68rem">No</span>' ?></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
<?php
iscms_render_footer();
