@extends('layouts.app')
@section('title', 'Vital-Sign Reference Ranges')

@section('content')
<section class="content-header">
    <h1><i class="fa fa-thermometer-three-quarters"></i> Vital-Sign Reference Ranges
        <small>Per profile type + life stage</small></h1>
    <ol class="breadcrumb">
        <li><a href="{{ route('clinic.dashboard') }}">Clinic</a></li>
        <li class="active">Vital Ranges</li>
    </ol>
</section>

<section class="content">
    <div class="callout callout-info" style="font-size:12.5px;">
        <p style="margin:0 0 4px;">
            <i class="fa fa-info-circle"></i> These ranges power the
            <strong>inline vital warnings on the appointment form</strong> and the
            <strong>Medical Critical Check report</strong>. The system ships default
            ranges; add your own rows below for additional profile types or to
            override the defaults for your clinic.
        </p>
        <p style="margin:0;font-size:11.5px;color:#475569;">
            <strong>Critical</strong> = value is &gt;15% outside the band (red
            border, flagged in the critical report). <strong>Abnormal</strong> =
            inside the 15% buffer but outside normal (amber border).
        </p>
    </div>

    <div class="box box-primary">
        <div class="box-header">
            <h3 class="box-title">
                <i class="fa fa-list"></i> Configured Ranges
                <span class="badge" style="background:#2980b9;margin-left:6px;">{{ $rows->count() }}</span>
            </h3>
            <div class="box-tools pull-right">
                <a href="{{ route('clinic.vital-ranges.create') }}" class="btn btn-sm btn-primary">
                    <i class="fa fa-plus"></i> Add range
                </a>
            </div>
        </div>
        <div class="box-body">
            <table class="table table-condensed table-striped" style="font-size:12.5px;">
                <thead>
                    <tr style="background:#f1f5f9;">
                        <th>Profile type</th>
                        <th>Life stage</th>
                        <th>Source</th>
                        <th>Temperature (°C)</th>
                        <th>Heart rate (bpm)</th>
                        <th>Resp. rate (/min)</th>
                        <th>Weight (kg)</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                @forelse($rows as $r)
                    @php $isSystem = $r->business_id === null; @endphp
                    <tr style="{{ $isSystem ? 'background:#fafafa;' : '' }}">
                        <td><strong>{{ ucfirst($r->species) }}</strong></td>
                        <td>{{ ucfirst($r->life_stage) }}</td>
                        <td>
                            @if($isSystem)
                                <span class="label label-default" title="System default — read-only. Add a business row to override.">SYSTEM</span>
                            @else
                                <span class="label label-success">BUSINESS</span>
                            @endif
                        </td>
                        <td>
                            @if($r->temperature_low !== null || $r->temperature_high !== null)
                                {{ $r->temperature_low ?? '?' }} – {{ $r->temperature_high ?? '?' }}
                            @else <span class="text-muted">—</span>@endif
                        </td>
                        <td>
                            @if($r->heart_rate_low !== null || $r->heart_rate_high !== null)
                                {{ $r->heart_rate_low ?? '?' }} – {{ $r->heart_rate_high ?? '?' }}
                            @else <span class="text-muted">—</span>@endif
                        </td>
                        <td>
                            @if($r->respiratory_rate_low !== null || $r->respiratory_rate_high !== null)
                                {{ $r->respiratory_rate_low ?? '?' }} – {{ $r->respiratory_rate_high ?? '?' }}
                            @else <span class="text-muted">—</span>@endif
                        </td>
                        <td>
                            @if($r->weight_low_kg !== null || $r->weight_high_kg !== null)
                                {{ $r->weight_low_kg ?? '?' }} – {{ $r->weight_high_kg ?? '?' }}
                            @else <span class="text-muted">—</span>@endif
                        </td>
                        <td>
                            @if(!$isSystem)
                                <a href="{{ route('clinic.vital-ranges.edit', $r->id) }}" class="btn btn-xs btn-warning">
                                    <i class="fa fa-edit"></i>
                                </a>
                                <form method="POST" action="{{ route('clinic.vital-ranges.destroy', $r->id) }}"
                                      style="display:inline;" onsubmit="return confirm('Delete this reference range? Existing appointments keep their snapshotted critical flags.');">
                                    @csrf @method('DELETE')
                                    <button type="submit" class="btn btn-xs btn-danger"><i class="fa fa-trash"></i></button>
                                </form>
                            @endif
                        </td>
                    </tr>
                @empty
                    <tr><td colspan="8" class="text-center text-muted" style="padding:30px;">
                        No reference ranges configured. The system defaults didn't load — try running the migrations.
                    </td></tr>
                @endforelse
                </tbody>
            </table>
        </div>
    </div>

    <div class="callout callout-warning" style="font-size:12.5px;">
        <h4 style="margin-top:0;"><i class="fa fa-language"></i> Profile type naming</h4>
        <p style="margin:0;">
            Profile-type names are normalised on save and matched case-insensitively.
            Use <strong>all</strong> to apply a range to every patient, or define your own
            profile keys — the matcher resolves each patient to the closest configured range.
        </p>
    </div>
</section>
@endsection
