@extends('layouts.app')
@section('title', __('medward::lang.icu_dashboard'))
@section('content')
<section class="content-header">
    <h1>@lang('medward::lang.icu_dashboard')</h1>
    <p>{{ __('medward::lang.icu_dashboard_help', ['sec' => $refreshSeconds]) }}</p>
</section>
<section class="content">
  @if($strips->isEmpty())
    <div class="alert alert-info">@lang('medward::lang.no_icu_admissions')</div>
  @else
    @foreach($strips as $strip)
      @php $adm = $strip['admission']; $latest = $strip['latest']; @endphp
      <div class="box box-solid">
        <div class="box-header with-border">
          <h3 class="box-title">
            <a href="{{ route('medward.admissions.show', $adm->id) }}">{{ $strip['patient_name'] }}</a>
            <small>· {{ $adm->admission_no }} · {{ optional($adm->ward)->name }}/{{ optional($adm->bed)->bed_number }}
              · @lang('medward::lang.icu_type_' . ($adm->icu_type ?: 'medical'))
              @if($adm->ventilator) · <span class="label label-info">VENT {{ $adm->ventilator_mode }}</span>@endif
            </small>
          </h3>
        </div>
        <div class="box-body">
          @if($latest)
            <div class="row" style="text-align:center;">
              @php
                $mapMin = config('medward.icu_critical.map_min');
                $spo2Min = config('medward.icu_critical.spo2_min');
              @endphp
              <div class="col-xs-2"><small class="text-muted">@lang('medward::lang.gcs')</small><br><strong>{{ $latest->gcs_total ?? '—' }}</strong></div>
              <div class="col-xs-2"><small class="text-muted">@lang('medward::lang.hr')</small><br><strong>{{ $latest->hr ?? '—' }}</strong></div>
              <div class="col-xs-2"><small class="text-muted">@lang('medward::lang.bp')</small><br><strong>{{ ($latest->bp_sys ?? '—') . '/' . ($latest->bp_dia ?? '—') }}</strong></div>
              <div class="col-xs-2"><small class="text-muted">@lang('medward::lang.map')</small><br>
                <strong class="{{ ($latest->map_calc !== null && $latest->map_calc < $mapMin) ? 'text-red' : '' }}">{{ $latest->map_calc !== null ? number_format($latest->map_calc, 0) : '—' }}</strong></div>
              <div class="col-xs-2"><small class="text-muted">@lang('medward::lang.spo2')</small><br>
                <strong class="{{ ($latest->spo2 !== null && $latest->spo2 < $spo2Min) ? 'text-red' : '' }}">{{ $latest->spo2 !== null ? number_format($latest->spo2, 0) . '%' : '—' }}</strong></div>
              <div class="col-xs-2"><small class="text-muted">@lang('medward::lang.rr')</small><br><strong>{{ $latest->rr ?? '—' }}</strong></div>
            </div>
            <p class="text-muted" style="margin-top:8px;"><small>@lang('medward::lang.latest'): {{ optional($latest->recorded_at)->format('Y-m-d H:i') }}</small></p>
          @else
            <p class="text-muted">@lang('medward::lang.never_charted')</p>
          @endif

          @if(!empty($strip['alerts']))
            <div class="callout callout-danger" style="margin-bottom:0;">
              <h4><i class="fa fa-warning"></i> @lang('medward::lang.alerts')</h4>
              <ul style="margin-bottom:0;">
                @foreach($strip['alerts'] as $a)
                  <li><strong>{{ $a['title'] }}</strong>{{ !empty($a['details']) ? ' — ' . $a['details'] : '' }}</li>
                @endforeach
              </ul>
            </div>
          @endif
        </div>
      </div>
    @endforeach
  @endif
</section>
@endsection
@section('javascript')
<script>
$(function () {
    // Simple full-page auto-refresh keeps the ICU strips current.
    setTimeout(function () { window.location.reload(); }, {{ $refreshSeconds * 1000 }});
});
</script>
@endsection
