<?php
declare(strict_types=1);

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

$conn = iscms_init();
$pageTitle = 'Test result parameters';
$activeNav = 'laboratory';

$sql = "SELECT p.id, p.result_id, p.parameter_name, p.test_value, p.minimum_value, p.maximum_value,
               p.target_value, p.unit_of_measure, p.compliance_status, p.sort_order,
               tr.result_number,
               s.sample_number
        FROM test_result_parameters p
        INNER JOIN lab_test_results tr ON p.result_id = tr.id
        INNER JOIN lab_samples s ON tr.sample_id = s.id
        ORDER BY tr.result_number ASC, p.sort_order ASC, p.parameter_name ASC
        LIMIT 800";
$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>Test result parameters</h1>
        <p>Line-level measurements and compliance against limits.</p>
    </div>

    <div class="section-card mb-4">
        <div class="section-header">
            <h2><i class="ri-links-line"></i> Related</h2>
        </div>
        <div class="quick-actions">
            <a class="qa-btn" href="lab_test_results.php"><i class="ri-file-chart-line"></i> Results</a>
            <a class="qa-btn" href="lab_samples.php"><i class="ri-test-tube-line"></i> Samples</a>
        </div>
    </div>

    <div class="section-card">
        <div class="section-header">
            <h2><i class="ri-list-settings-line"></i> Parameters</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>Result</th>
                        <th>Sample</th>
                        <th>Parameter</th>
                        <th>Value</th>
                        <th>Min</th>
                        <th>Max</th>
                        <th>Target</th>
                        <th>Unit</th>
                        <th>Compliance</th>
                        <th>Order</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                    <tr class="empty-row"><td colspan="10"><i class="ri-inbox-line me-2"></i>No parameters</td></tr>
                    <?php else: ?>
                        <?php foreach ($rows as $r): ?>
                        <tr>
                            <td class="iscms-mono" style="font-weight:600; color:var(--ssbs-blue)"><?= iscms_h($r['result_number']) ?></td>
                            <td class="iscms-mono"><small><?= iscms_h($r['sample_number']) ?></small></td>
                            <td><small><?= iscms_h((string) ($r['parameter_name'] ?? '')) ?></small></td>
                            <td class="iscms-mono"><small><?= iscms_val($r['test_value'] ?? null) ?></small></td>
                            <td class="iscms-mono"><small><?= iscms_val($r['minimum_value'] ?? null) ?></small></td>
                            <td class="iscms-mono"><small><?= iscms_val($r['maximum_value'] ?? null) ?></small></td>
                            <td class="iscms-mono"><small><?= iscms_val($r['target_value'] ?? null) ?></small></td>
                            <td><small><?= iscms_val($r['unit_of_measure'] ?? null) ?></small></td>
                            <td><?= iscms_status_badge((string) ($r['compliance_status'] ?? '')) ?></td>
                            <td><small class="text-muted"><?= (int) ($r['sort_order'] ?? 0) ?></small></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
<?php
iscms_render_footer();
