{{-- Visit detail drawer: header + timeline + triage + vitals + disposition. --}}
@php
    $color = $visit->triage_level ? ($triageColors[$visit->triage_level] ?? '#94a3b8') : '#94a3b8';
    $mins  = $visit->timeInDeptMinutes();
    $canDispose = auth()->user()->can('mder.disposition');
@endphp
<div class="modal-dialog modal-lg" role="document">
  <div class="modal-content">
    <div class="modal-header" style="border-top:4px solid {{ $color }};">
      <button type="button" class="close" data-dismiss="modal">&times;</button>
      <h4 class="modal-title">
        {{ $visit->patient_name ?: ('#' . $visit->id) }}
        <small>{{ $visit->visit_no }}</small>
      </h4>
    </div>
    <div class="modal-body">
      <div class="row">
        {{-- Left: facts + triage + vitals --}}
        <div class="col-md-6">
          <table class="table table-condensed">
            <tr><th>@lang('medemergency::lang.arrival_at')</th>
                <td>{{ optional($visit->arrival_at)->format('Y-m-d H:i') ?? '—' }}
                    ({{ __('medemergency::lang.mode_' . $visit->arrival_mode) }})</td></tr>
            <tr><th>@lang('medemergency::lang.status')</th>
                <td><span class="label label-default">@lang('medemergency::lang.board_' . $visit->board_status)</span></td></tr>
            <tr><th>@lang('medemergency::lang.time_in_dept')</th>
                <td>{{ $mins !== null ? __('medemergency::lang.minutes_short', ['n' => $mins]) : '—' }}</td></tr>
            <tr><th>@lang('medemergency::lang.chief_complaint')</th>
                <td>{{ $visit->chief_complaint ?: '—' }}</td></tr>
            <tr><th>@lang('medemergency::lang.assigned_doctor')</th>
                <td>{{ $doctorName ?: '—' }}</td></tr>
          </table>

          <h5><strong>@lang('medemergency::lang.triage_details')</strong></h5>
          @if($visit->triage_level)
            <p>
              <span class="mder-triage-badge" style="color:#fff;font-size:11px;padding:2px 8px;border-radius:8px;background:{{ $color }};">
                @lang('medemergency::lang.level_' . $visit->triage_level)
              </span>
              <small class="text-muted">
                {{ strtoupper($visit->triage_system) }} ·
                @lang('medemergency::lang.triaged_at') {{ optional($visit->triaged_at)->format('Y-m-d H:i') }}
              </small>
            </p>
            @if($vitals)
              <table class="table table-condensed table-bordered" style="font-size:12px;">
                <tr><th>@lang('medemergency::lang.temperature')</th><td>{{ $vitals->temperature ?? '—' }}</td>
                    <th>@lang('medemergency::lang.heart_rate')</th><td>{{ $vitals->heart_rate ?? '—' }}</td></tr>
                <tr><th>@lang('medemergency::lang.respiratory_rate')</th><td>{{ $vitals->respiratory_rate ?? '—' }}</td>
                    <th>@lang('medemergency::lang.spo2')</th><td>{{ $vitals->spo2 ?? '—' }}</td></tr>
                <tr><th>BP</th><td>{{ ($vitals->bp_systolic ?? '—') . '/' . ($vitals->bp_diastolic ?? '—') }}</td>
                    <th>GCS</th><td>{{ $vitals->gcs_total ?? '—' }}</td></tr>
              </table>
            @else
              <p class="text-muted">@lang('medemergency::lang.no_vitals')</p>
            @endif
          @else
            <p class="text-muted">@lang('medemergency::lang.not_triaged')</p>
          @endif
        </div>

        {{-- Right: timeline --}}
        <div class="col-md-6">
          <h5><strong>@lang('medemergency::lang.timeline')</strong></h5>
          <ul class="list-unstyled" style="font-size:12px;">
            @foreach($visit->events as $ev)
              <li style="padding:4px 0;border-bottom:1px solid #f1f5f9;">
                <span class="label label-info">@lang('medemergency::lang.event_' . $ev->event)</span>
                {{ $ev->detail }}
                <br><small class="text-muted">{{ optional($ev->created_at)->format('Y-m-d H:i') }}</small>
              </li>
            @endforeach
          </ul>
        </div>
      </div>

      {{-- Disposition --}}
      @if(! $visit->isClosed() && $canDispose)
        <hr>
        <h5><strong>@lang('medemergency::lang.record_disposition')</strong></h5>
        <form id="mder_disposition_form" action="{{ route('mder.disposition.store', $visit->id) }}" method="POST">
          {{ csrf_field() }}
          <div class="row">
            <div class="col-md-4 form-group">
              <select name="disposition" class="form-control" required>
                <option value="">—</option>
                @foreach($dispositions as $d)
                  <option value="{{ $d }}">@lang('medemergency::lang.disp_' . $d)</option>
                @endforeach
              </select>
            </div>
            <div class="col-md-6 form-group">
              <input type="text" name="disposition_note" class="form-control" placeholder="@lang('medemergency::lang.disposition_note')">
            </div>
            <div class="col-md-2 form-group">
              <button type="submit" class="btn btn-primary btn-block">@lang('medemergency::lang.save')</button>
            </div>
          </div>
        </form>
      @elseif($visit->disposition)
        <hr>
        <p><strong>@lang('medemergency::lang.disposition'):</strong>
           @lang('medemergency::lang.disp_' . $visit->disposition)
           <small class="text-muted">{{ optional($visit->disposition_at)->format('Y-m-d H:i') }}</small>
           @if($visit->medward_admission_id)
             · MedWard #{{ $visit->medward_admission_id }}
           @endif
        </p>
        @if($visit->disposition_note)<p class="text-muted">{{ $visit->disposition_note }}</p>@endif
      @endif
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-default" data-dismiss="modal">@lang('medemergency::lang.close')</button>
    </div>
  </div>
</div>
