@extends('layouts.app')
@section('title', __('medward::lang.admission') . ' ' . $admission->admission_no)
@section('content')
@php
    $active = $admission->isActive();
    $canDischarge = auth()->user()->can('medward.admission.discharge');
    $canMar = auth()->user()->can('medward.mar.administer');
    $canIcu = auth()->user()->can('medward.icu.chart');
@endphp
<section class="content-header">
    <h1>@lang('medward::lang.admission') {{ $admission->admission_no }}
        <span class="label {{ ['admitted'=>'label-info','discharge_pending'=>'label-warning','discharged'=>'label-default'][$admission->status] ?? 'label-default' }}">
            @lang('medward::lang.adm_status_' . $admission->status)
        </span>
        @if($admission->is_icu)<span class="label label-danger">ICU</span>@endif
    </h1>
    <p><a href="{{ route('medward.admissions.index') }}"><i class="fa fa-arrow-left"></i> @lang('medward::lang.admissions')</a></p>
</section>
<section class="content">
  <div class="nav-tabs-custom">
    <ul class="nav nav-tabs">
      <li class="active"><a href="#tab_overview" data-toggle="tab">@lang('medward::lang.tab_overview')</a></li>
      <li><a href="#tab_mar" data-toggle="tab">@lang('medward::lang.tab_mar')</a></li>
      <li><a href="#tab_fluid" data-toggle="tab">@lang('medward::lang.tab_fluid')</a></li>
      @if($admission->is_icu)<li><a href="#tab_icu" data-toggle="tab">@lang('medward::lang.tab_icu')</a></li>@endif
      <li><a href="#tab_discharge" data-toggle="tab">@lang('medward::lang.tab_discharge')</a></li>
    </ul>
    <div class="tab-content">

      {{-- OVERVIEW --}}
      <div class="tab-pane active" id="tab_overview">
        <table class="table table-bordered">
          <tr><th style="width:220px;">@lang('medward::lang.patient')</th><td>{{ $patientName }}</td></tr>
          <tr><th>@lang('medward::lang.ward_bed')</th><td>{{ optional($admission->ward)->name }} / {{ optional($admission->bed)->bed_number }}</td></tr>
          <tr><th>@lang('medward::lang.admission_type')</th><td>@lang('medward::lang.admission_type_' . $admission->admission_type) · @lang('medward::lang.admission_source_' . $admission->admission_source)</td></tr>
          <tr><th>@lang('medward::lang.admitted_at')</th><td>{{ optional($admission->admitted_at)->format('Y-m-d H:i') }}</td></tr>
          <tr><th>@lang('medward::lang.admitting_diagnosis')</th><td>{{ $admission->admitting_diagnosis ?: '—' }}</td></tr>
          <tr><th>@lang('medward::lang.expected_discharge')</th><td>{{ optional($admission->expected_discharge)->format('Y-m-d') ?: '—' }}</td></tr>
          @if($admission->is_icu)
          <tr><th>@lang('medward::lang.icu_details')</th><td>
            @lang('medward::lang.icu_type_' . ($admission->icu_type ?: 'medical'))
            · APACHE II: {{ $admission->apache_ii ?? '—' }} · SOFA: {{ $admission->sofa ?? '—' }}
            @if($admission->ventilator) · @lang('medward::lang.ventilator'): {{ $admission->ventilator_mode ?: 'yes' }} @endif
          </td></tr>
          @endif
          @if($admission->status === 'discharged')
          <tr><th>@lang('medward::lang.discharge')</th><td>
            @lang('medward::lang.discharge_type_' . ($admission->discharge_type ?: 'other')) — {{ optional($admission->discharged_at)->format('Y-m-d H:i') }}
            <div style="white-space:pre-wrap;">{{ $admission->discharge_summary }}</div>
          </td></tr>
          @endif
        </table>
      </div>

      {{-- MAR --}}
      <div class="tab-pane" id="tab_mar">
        <p class="text-muted">@lang('medward::lang.mar_help')</p>
        @if($canMar && $active)
        <form class="medward-form" method="POST" action="{{ route('medward.admissions.mar.generate', $admission->id) }}" style="margin-bottom:10px;">
          {{ csrf_field() }}
          <input type="date" name="day" value="{{ now()->format('Y-m-d') }}" class="form-control" style="display:inline-block;width:auto;">
          <button type="submit" class="btn btn-default btn-sm"><i class="fa fa-magic"></i> @lang('medward::lang.generate_mar')</button>
        </form>
        @endif
        <table class="table table-bordered table-striped">
          <thead><tr>
            <th>@lang('medward::lang.scheduled')</th><th>@lang('medward::lang.medicine')</th>
            <th>@lang('medward::lang.dose')</th><th>@lang('medward::lang.route')</th>
            <th>@lang('medward::lang.status')</th><th>@lang('medward::lang.administered')</th>
            @if($canMar && $active)<th>@lang('medward::lang.actions')</th>@endif
          </tr></thead>
          <tbody>
          @forelse($marEntries as $m)
            <tr>
              <td>{{ optional($m->scheduled_at)->format('Y-m-d H:i') }}</td>
              <td>{{ $m->medicine_name }}</td>
              <td>{{ $m->dose ?: '—' }}</td>
              <td>{{ $m->route ?: '—' }}</td>
              <td><span class="label {{ $m->statusClass() }}">@lang('medward::lang.mar_status_' . $m->status)</span>
                  @if($m->reason_if_held)<br><small class="text-muted">{{ $m->reason_if_held }}</small>@endif</td>
              <td>{{ optional($m->administered_at)->format('H:i') ?: '—' }}</td>
              @if($canMar && $active)
              <td>
                @if($m->status === 'scheduled' || $m->status === 'missed')
                  <button class="btn btn-xs btn-success mw-give" data-id="{{ $m->id }}"><i class="fa fa-check"></i> @lang('medward::lang.give')</button>
                  <button class="btn btn-xs btn-warning mw-hold" data-id="{{ $m->id }}">@lang('medward::lang.hold')</button>
                @endif
              </td>
              @endif
            </tr>
          @empty
            <tr><td colspan="7" class="text-center text-muted">@lang('medward::lang.no_mar')</td></tr>
          @endforelse
          </tbody>
        </table>
      </div>

      {{-- FLUID BALANCE --}}
      <div class="tab-pane" id="tab_fluid">
        <p class="text-muted">@lang('medward::lang.fluid_help')</p>
        @if($canMar && $active)
        <form class="medward-form" method="POST" action="{{ route('medward.admissions.fluid.store', $admission->id) }}" style="margin-bottom:12px;">
          {{ csrf_field() }}
          <div class="row">
            <div class="col-md-2 form-group"><label>@lang('medward::lang.shift')</label>
              <select name="shift" class="form-control">
                @foreach(['morning','evening','night'] as $s)<option value="{{ $s }}">@lang('medward::lang.shift_' . $s)</option>@endforeach
              </select></div>
            <div class="col-md-1 form-group"><label>@lang('medward::lang.intake_oral')</label><input type="number" step="0.01" name="intake_oral" class="form-control"></div>
            <div class="col-md-1 form-group"><label>@lang('medward::lang.intake_iv')</label><input type="number" step="0.01" name="intake_iv" class="form-control"></div>
            <div class="col-md-1 form-group"><label>@lang('medward::lang.intake_other')</label><input type="number" step="0.01" name="intake_other" class="form-control"></div>
            <div class="col-md-1 form-group"><label>@lang('medward::lang.output_urine')</label><input type="number" step="0.01" name="output_urine" class="form-control"></div>
            <div class="col-md-1 form-group"><label>@lang('medward::lang.output_drain')</label><input type="number" step="0.01" name="output_drain" class="form-control"></div>
            <div class="col-md-1 form-group"><label>@lang('medward::lang.output_other')</label><input type="number" step="0.01" name="output_other" class="form-control"></div>
            <div class="col-md-2 form-group"><label>&nbsp;</label><br><button type="submit" class="btn btn-primary btn-sm">@lang('medward::lang.add_fluid')</button></div>
          </div>
        </form>
        @endif
        <table class="table table-bordered table-striped">
          <thead><tr>
            <th>@lang('medward::lang.recorded_at')</th><th>@lang('medward::lang.shift')</th>
            <th>@lang('medward::lang.intake_total')</th><th>@lang('medward::lang.output_total')</th><th>@lang('medward::lang.balance')</th>
          </tr></thead>
          <tbody>
          @forelse($fluidRows as $f)
            <tr>
              <td>{{ optional($f->recorded_at)->format('Y-m-d H:i') }}</td>
              <td>@lang('medward::lang.shift_' . $f->shift)</td>
              <td>{{ number_format($f->intake_total, 0) }}</td>
              <td>{{ number_format($f->output_total, 0) }}</td>
              <td class="{{ $f->balance < 0 ? 'text-red' : 'text-green' }}"><strong>{{ number_format($f->balance, 0) }}</strong></td>
            </tr>
          @empty
            <tr><td colspan="5" class="text-center text-muted">@lang('medward::lang.no_fluid')</td></tr>
          @endforelse
          </tbody>
        </table>
      </div>

      {{-- ICU FLOW-SHEET --}}
      @if($admission->is_icu)
      <div class="tab-pane" id="tab_icu">
        <p class="text-muted">{{ __('medward::lang.icu_flowsheet_help', ['map' => config('medward.icu_critical.map_min'), 'spo2' => config('medward.icu_critical.spo2_min')]) }}</p>
        @if($canIcu && $active)
        <form class="medward-form" method="POST" action="{{ route('medward.icu.chart', $admission->id) }}" style="margin-bottom:12px;">
          {{ csrf_field() }}
          <div class="row">
            <div class="col-md-1 form-group"><label>@lang('medward::lang.gcs')</label><input type="number" name="gcs_total" class="form-control" min="3" max="15"></div>
            <div class="col-md-1 form-group"><label>@lang('medward::lang.rass')</label><input type="number" name="rass" class="form-control" min="-5" max="4"></div>
            <div class="col-md-1 form-group"><label>@lang('medward::lang.hr')</label><input type="number" name="hr" class="form-control"></div>
            <div class="col-md-1 form-group"><label>@lang('medward::lang.bp_sys')</label><input type="number" name="bp_sys" class="form-control"></div>
            <div class="col-md-1 form-group"><label>@lang('medward::lang.bp_dia')</label><input type="number" name="bp_dia" class="form-control"></div>
            <div class="col-md-1 form-group"><label>@lang('medward::lang.spo2')</label><input type="number" step="0.1" name="spo2" class="form-control"></div>
            <div class="col-md-1 form-group"><label>@lang('medward::lang.rr')</label><input type="number" name="rr" class="form-control"></div>
            <div class="col-md-1 form-group"><label>@lang('medward::lang.temp')</label><input type="number" step="0.1" name="temp" class="form-control"></div>
            <div class="col-md-1 form-group"><label>@lang('medward::lang.fio2')</label><input type="number" step="0.1" name="fio2" class="form-control"></div>
            <div class="col-md-1 form-group"><label>@lang('medward::lang.peep')</label><input type="number" step="0.1" name="peep" class="form-control"></div>
            <div class="col-md-2 form-group"><label>@lang('medward::lang.pupils')</label><input type="text" name="pupils" class="form-control"></div>
          </div>
          <div class="row">
            <div class="col-md-8 form-group"><label>@lang('medward::lang.interventions')</label><input type="text" name="interventions" class="form-control"></div>
            <div class="col-md-2 form-group"><label>@lang('medward::lang.labs_note')</label><input type="text" name="labs_note" class="form-control"></div>
            <div class="col-md-2 form-group"><label>&nbsp;</label><br><button type="submit" class="btn btn-primary btn-sm">@lang('medward::lang.chart_flowsheet')</button></div>
          </div>
        </form>
        @endif
        <table class="table table-bordered table-striped table-condensed">
          <thead><tr>
            <th>@lang('medward::lang.recorded_at')</th><th>@lang('medward::lang.gcs')</th><th>@lang('medward::lang.rass')</th>
            <th>@lang('medward::lang.hr')</th><th>@lang('medward::lang.bp')</th><th>@lang('medward::lang.map')</th>
            <th>@lang('medward::lang.spo2')</th><th>@lang('medward::lang.rr')</th><th>@lang('medward::lang.temp')</th><th>@lang('medward::lang.interventions')</th>
          </tr></thead>
          <tbody>
          @forelse($flowsheets as $fs)
            <tr>
              <td>{{ optional($fs->recorded_at)->format('m-d H:i') }}</td>
              <td>{{ $fs->gcs_total ?? '—' }}</td>
              <td>{{ $fs->rass ?? '—' }}</td>
              <td>{{ $fs->hr ?? '—' }}</td>
              <td>{{ ($fs->bp_sys ?? '—') . '/' . ($fs->bp_dia ?? '—') }}</td>
              <td class="{{ ($fs->map_calc !== null && $fs->map_calc < config('medward.icu_critical.map_min')) ? 'text-red' : '' }}"><strong>{{ $fs->map_calc !== null ? number_format($fs->map_calc, 0) : '—' }}</strong></td>
              <td class="{{ ($fs->spo2 !== null && $fs->spo2 < config('medward.icu_critical.spo2_min')) ? 'text-red' : '' }}">{{ $fs->spo2 !== null ? number_format($fs->spo2, 0) : '—' }}</td>
              <td>{{ $fs->rr ?? '—' }}</td>
              <td>{{ $fs->temp !== null ? number_format($fs->temp, 1) : '—' }}</td>
              <td>{{ $fs->interventions ?: '—' }}</td>
            </tr>
          @empty
            <tr><td colspan="10" class="text-center text-muted">@lang('medward::lang.no_flowsheet')</td></tr>
          @endforelse
          </tbody>
        </table>
      </div>
      @endif

      {{-- DISCHARGE --}}
      <div class="tab-pane" id="tab_discharge">
        <p class="text-muted">@lang('medward::lang.discharge_help')</p>
        @if($admission->status === 'discharged')
          <div class="alert alert-success">@lang('medward::lang.already_discharged')</div>
        @elseif(! $canDischarge)
          <div class="alert alert-info">—</div>
        @else
          @if($admission->status === 'admitted')
          <form class="medward-form" method="POST" action="{{ route('medward.admissions.discharge.request', $admission->id) }}" style="margin-bottom:14px;">
            {{ csrf_field() }}
            <button type="submit" class="btn btn-warning btn-sm"><i class="fa fa-sign-out"></i> @lang('medward::lang.request_discharge')</button>
          </form>
          @endif
          <form class="medward-form" method="POST" action="{{ route('medward.admissions.discharge', $admission->id) }}">
            {{ csrf_field() }}
            <div class="row">
              <div class="col-md-4 form-group"><label>@lang('medward::lang.discharge_type') *</label>
                <select name="discharge_type" class="form-control" required>
                  @foreach($dischargeTypes as $dt)<option value="{{ $dt }}">@lang('medward::lang.discharge_type_' . $dt)</option>@endforeach
                </select></div>
              <div class="col-md-4 form-group"><label>@lang('medward::lang.followup_date')</label><input type="date" name="followup_date" class="form-control"></div>
            </div>
            <div class="form-group"><label>@lang('medward::lang.med_reconciliation')</label><textarea name="med_reconciliation" class="form-control" rows="2"></textarea></div>
            <div class="form-group"><label>@lang('medward::lang.discharge_summary') *</label><textarea name="discharge_summary" class="form-control" rows="4" required></textarea></div>
            <div class="form-group"><label>@lang('medward::lang.discharge_instructions')</label><textarea name="discharge_instructions" class="form-control" rows="3"></textarea></div>
            <button type="submit" class="btn btn-primary">@lang('medward::lang.complete_discharge')</button>
          </form>
        @endif
      </div>

    </div>
  </div>

  {{-- Hold-dose modal --}}
  <div class="modal fade" id="mw_hold_modal" tabindex="-1" role="dialog">
    <div class="modal-dialog"><div class="modal-content">
      <form class="medward-form" method="POST" id="mw_hold_form">
        {{ csrf_field() }}
        <div class="modal-header"><button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">@lang('medward::lang.hold')</h4></div>
        <div class="modal-body">
          <div class="form-group"><label>@lang('medward::lang.status')</label>
            <select name="status" class="form-control">
              <option value="held">@lang('medward::lang.mar_status_held')</option>
              <option value="refused">@lang('medward::lang.mar_status_refused')</option>
              <option value="missed">@lang('medward::lang.mar_status_missed')</option>
            </select></div>
          <div class="form-group"><label>@lang('medward::lang.held_reason') *</label><input type="text" name="reason" class="form-control" required></div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">@lang('medward::lang.cancel')</button>
          <button type="submit" class="btn btn-primary">@lang('medward::lang.save')</button>
        </div>
      </form>
    </div></div>
  </div>

  {{-- Give-dose modal (dose/route/witness) --}}
  <div class="modal fade" id="mw_give_modal" tabindex="-1" role="dialog">
    <div class="modal-dialog"><div class="modal-content">
      <form class="medward-form" method="POST" id="mw_give_form">
        {{ csrf_field() }}
        <div class="modal-header"><button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">@lang('medward::lang.give')</h4></div>
        <div class="modal-body">
          <div class="row">
            <div class="col-md-6 form-group"><label>@lang('medward::lang.dose')</label><input type="text" name="dose" class="form-control"></div>
            <div class="col-md-6 form-group"><label>@lang('medward::lang.route')</label><input type="text" name="route" class="form-control"></div>
          </div>
          <div class="form-group"><label>@lang('medward::lang.witness')</label><input type="number" name="witness_user_id" class="form-control"></div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">@lang('medward::lang.cancel')</button>
          <button type="submit" class="btn btn-success">@lang('medward::lang.give')</button>
        </div>
      </form>
    </div></div>
  </div>
</section>
@endsection
@section('javascript')
@include('medward::partials.crud_scripts')
<script>
$(function () {
    var giveBase = '{{ url('med-ward/mar') }}';
    window.medWardAfterSave = function () { window.location.reload(); };

    $('.mw-give').on('click', function () {
        $('#mw_give_form').attr('action', giveBase + '/' + $(this).data('id') + '/administer');
        $('#mw_give_modal').modal('show');
    });
    $('.mw-hold').on('click', function () {
        $('#mw_hold_form').attr('action', giveBase + '/' + $(this).data('id') + '/hold');
        $('#mw_hold_modal').modal('show');
    });
});
</script>
@endsection
