<?php require __DIR__ . '/settings_nav.php'; ?>
<div class="mb-3">
    <h2 class="mb-1"><?= h(t('lab_reference.heading')) ?></h2>
    <div class="text-muted"><?= h(t('lab_reference.intro')) ?></div>
</div>
<?php if ($saved): ?><div class="alert alert-success">✅ <?= h(t('lab_reference.saved')) ?></div>
<?php endif; ?><?php if ($error !== ''): ?><div class="alert alert-danger">⚠️ <?= h($error) ?></div><?php endif; ?>
<div class="alert alert-info"><strong><?= h(t('lab_reference.priority_title')) ?></strong>
    <?= h(t('lab_reference.priority_help')) ?></div>
<form method="post" action="/settings/lab-references/save"><input type="hidden" name="csrf"
        value="<?= h(Auth::csrf()) ?>">
    <?php foreach ($groups as $analyzer => $speciesGroups): ?><div class="card mb-3">
        <div class="card-header">
            <div>
                <h3 class="card-title mb-0">🩸 <?= h($analyzer) ?></h3>
                <div class="text-muted small"><?= h(t('lab_reference.manufacturer_ranges')) ?></div>
            </div>
        </div>
        <div class="card-body p-0">
            <ul class="nav nav-tabs px-3 pt-2" role="tablist">
                <?php foreach (array_keys($speciesGroups) as $i => $species): ?><li class="nav-item"><button
                        class="nav-link <?= !$i ? 'active' : '' ?>" data-bs-toggle="tab"
                        data-bs-target="#labref-<?= h(md5($analyzer . $species)) ?>"
                        type="button"><?= h(SpeciesService::label($species)) ?></button></li><?php endforeach; ?></ul>
            <div class="tab-content">
                <?php foreach ($speciesGroups as $iSpecies => $rows): $species = $rows[0]['species_code']; ?><div
                    class="tab-pane <?= $species === array_key_first($speciesGroups) ? 'active show' : '' ?>"
                    id="labref-<?= h(md5($analyzer . $species)) ?>">
                    <div class="table-responsive">
                        <table class="table table-vcenter mb-0">
                            <thead>
                                <tr>
                                    <th><?= h(t('lab_reference.code')) ?></th>
                                    <th><?= h(t('medical_document.parameter')) ?></th>
                                    <th><?= h(t('medical_document.unit')) ?></th>
                                    <th><?= h(t('medical_document.reference_min')) ?></th>
                                    <th><?= h(t('medical_document.reference_max')) ?></th>
                                    <th><?= h(t('lab_reference.source')) ?></th>
                                    <th><?= h(t('settings.active_f')) ?></th>
                                </tr>
                            </thead>
                            <tbody><?php foreach ($rows as $row): ?><tr>
                                    <td><strong><?= h($row['parameter_code']) ?></strong></td>
                                    <td><input class="form-control form-control-sm"
                                            name="ranges[<?= (int)$row['id'] ?>][name]"
                                            value="<?= h($row['parameter_name']) ?>" required></td>
                                    <td><input class="form-control form-control-sm"
                                            name="ranges[<?= (int)$row['id'] ?>][unit]" value="<?= h($row['unit']) ?>"
                                            required></td>
                                    <td><input class="form-control form-control-sm" type="number" step="any"
                                            name="ranges[<?= (int)$row['id'] ?>][min]"
                                            value="<?= h($row['reference_min'] ?? '') ?>"></td>
                                    <td><input class="form-control form-control-sm" type="number" step="any"
                                            name="ranges[<?= (int)$row['id'] ?>][max]"
                                            value="<?= h($row['reference_max'] ?? '') ?>"></td>
                                    <td><input class="form-control form-control-sm"
                                            name="ranges[<?= (int)$row['id'] ?>][source]"
                                            value="<?= h($row['source_note'] ?? '') ?>"></td>
                                    <td><label class="form-check"><input class="form-check-input" type="checkbox"
                                                name="ranges[<?= (int)$row['id'] ?>][active]"
                                                <?= $row['active'] ? 'checked' : '' ?>><span
                                                class="form-check-label"></span></label></td>
                                </tr><?php endforeach; ?></tbody>
                        </table>
                    </div>
                </div><?php endforeach; ?></div>
        </div>
    </div><?php endforeach; ?>
    <div class="d-flex justify-content-end mb-4"><button class="btn btn-primary"><?= h(t('common.save')) ?></button>
    </div>
</form>
