<?php
declare(strict_types=1);

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

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

$sql = "SELECT ps.product_id, ps.standard_id, ps.is_mandatory, ps.added_at,
               p.product_name, p.product_code,
               s.standard_number, s.title AS standard_title
        FROM product_standards ps
        INNER JOIN products p ON ps.product_id = p.id
        INNER JOIN standards s ON ps.standard_id = s.id
        INNER JOIN organizations o ON p.org_id = o.id
        ORDER BY p.product_name ASC, s.standard_number 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 standards mapping</h1>
        <p>Links between registered products and applicable standards (mandatory or optional).</p>
    </div>

    <div class="section-card">
        <div class="section-header">
            <h2><i class="ri-links-line"></i> Mappings</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>Product code</th>
                        <th>Standard</th>
                        <th>Standard title</th>
                        <th>Mandatory</th>
                        <th>Added</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                    <tr class="empty-row"><td colspan="6"><i class="ri-inbox-line me-2"></i>No mappings</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 class="iscms-mono"><?= iscms_h($r['standard_number']) ?></td>
                            <td><small><?= iscms_h($r['standard_title']) ?></small></td>
                            <td><?= !empty($r['is_mandatory']) ? '<span class="badge bg-danger-subtle text-danger" style="font-size:.68rem">Yes</span>' : '<span class="badge bg-secondary-subtle text-secondary" style="font-size:.68rem">No</span>' ?></td>
                            <td><small class="text-muted"><?= !empty($r['added_at']) ? iscms_h(substr((string) $r['added_at'], 0, 10)) : '<span class="text-muted">-</span>' ?></small></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
<?php
iscms_render_footer();
