@extends('layouts.app')
@section('title', __('medward::lang.new_admission'))
@section('content')
<section class="content-header"><h1>@lang('medward::lang.new_admission')</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">
  @component('components.widget', ['class' => 'box-primary'])
    <form class="medward-form" method="POST" action="{{ route('medward.admissions.store') }}">
      {{ csrf_field() }}
      <div class="row">
        <div class="col-md-6 form-group">
          <label>@lang('medward::lang.patient') *</label>
          <select name="patient_id" class="form-control" required>
            <option value="">@lang('medward::lang.select_patient')</option>
            @foreach($patients as $id => $name)
              <option value="{{ $id }}">{{ $name }}</option>
            @endforeach
          </select>
        </div>
        <div class="col-md-3 form-group">
          <label>@lang('medward::lang.admitting_doctor')</label>
          <input type="number" name="admitting_doctor_id" class="form-control">
        </div>
        <div class="col-md-3 form-group">
          <label>@lang('medward::lang.expected_discharge')</label>
          <input type="date" name="expected_discharge" class="form-control">
        </div>
      </div>
      <div class="row">
        <div class="col-md-4 form-group">
          <label>@lang('medward::lang.ward') *</label>
          <select name="ward_id" id="mw_ward_select" class="form-control" required>
            <option value="">@lang('medward::lang.select_ward')</option>
            @foreach($wards as $w)
              <option value="{{ $w->id }}" data-icu="{{ $w->isIcuType() ? 1 : 0 }}">{{ $w->name }} ({{ __('medward::lang.ward_type_' . $w->ward_type) }})</option>
            @endforeach
          </select>
        </div>
        <div class="col-md-4 form-group">
          <label>@lang('medward::lang.bed') *</label>
          <select name="bed_id" id="mw_bed_select" class="form-control" required>
            <option value="">@lang('medward::lang.select_bed_ward')</option>
          </select>
        </div>
        <div class="col-md-4 form-group">
          <label>@lang('medward::lang.admission_type') *</label>
          <select name="admission_type" class="form-control" required>
            @foreach($types as $t)
              <option value="{{ $t }}">@lang('medward::lang.admission_type_' . $t)</option>
            @endforeach
          </select>
        </div>
      </div>
      <div class="row">
        <div class="col-md-4 form-group">
          <label>@lang('medward::lang.admission_source') *</label>
          <select name="admission_source" class="form-control" required>
            @foreach($sources as $s)
              <option value="{{ $s }}">@lang('medward::lang.admission_source_' . $s)</option>
            @endforeach
          </select>
        </div>
        <div class="col-md-8 form-group">
          <label>@lang('medward::lang.admitting_diagnosis')</label>
          <input type="text" name="admitting_diagnosis" class="form-control">
        </div>
      </div>

      <div class="checkbox">
        <label><input type="checkbox" name="is_icu" id="mw_is_icu" value="1"> <strong>@lang('medward::lang.is_icu')</strong></label>
      </div>

      <fieldset id="mw_icu_fields" style="display:none; border:1px solid #eee; padding:10px; border-radius:6px;">
        <legend style="font-size:14px;">@lang('medward::lang.icu_details')</legend>
        <div class="row">
          <div class="col-md-3 form-group">
            <label>@lang('medward::lang.icu_type')</label>
            <select name="icu_type" class="form-control">
              @foreach($icuTypes as $it)
                <option value="{{ $it }}">@lang('medward::lang.icu_type_' . $it)</option>
              @endforeach
            </select>
          </div>
          <div class="col-md-2 form-group">
            <label>@lang('medward::lang.apache_ii')</label>
            <input type="number" name="apache_ii" class="form-control" min="0" max="71">
          </div>
          <div class="col-md-2 form-group">
            <label>@lang('medward::lang.sofa')</label>
            <input type="number" name="sofa" class="form-control" min="0" max="24">
          </div>
          <div class="col-md-2 form-group">
            <label>&nbsp;</label><br>
            <label class="checkbox-inline"><input type="checkbox" name="ventilator" value="1"> @lang('medward::lang.ventilator')</label>
          </div>
          <div class="col-md-3 form-group">
            <label>@lang('medward::lang.ventilator_mode')</label>
            <input type="text" name="ventilator_mode" class="form-control" placeholder="AC / SIMV / PSV …">
          </div>
        </div>
      </fieldset>

      <div style="margin-top:12px;">
        <button type="submit" class="btn btn-primary">@lang('medward::lang.admit_patient')</button>
      </div>
    </form>
  @endcomponent
</section>
@endsection
@section('javascript')
@include('medward::partials.crud_scripts')
<script>
$(function () {
    var availUrl = '{{ route('medward.board.available_beds') }}';
    var preselectBed = @json(request()->get('bed_id'));

    function loadBeds(wardId, keep) {
        var $bed = $('#mw_bed_select').empty();
        if (!wardId) { $bed.append('<option value="">@lang('medward::lang.select_bed_ward')</option>'); return; }
        $.get(availUrl, { ward_id: wardId }).done(function (resp) {
            (resp.beds || []).forEach(function (b) {
                $bed.append('<option value="' + b.id + '">#' + b.bed_number + ' (' + b.bed_type + ')</option>');
            });
            if (!$bed.children().length) { $bed.append('<option value="">@lang('medward::lang.no_available_beds')</option>'); }
            if (keep) { $bed.val(keep); }
        });
    }

    $('#mw_ward_select').on('change', function () {
        loadBeds($(this).val());
        var icu = $(this).find(':selected').data('icu') == 1;
        if (icu) { $('#mw_is_icu').prop('checked', true).trigger('change'); }
    });

    $('#mw_is_icu').on('change', function () {
        $('#mw_icu_fields').toggle($(this).is(':checked'));
    });

    // If arriving from the bed board with ?bed_id=, preselect that bed's ward.
    if (preselectBed) {
        $.get(availUrl).done(function (resp) {
            var b = (resp.beds || []).find(function (x) { return x.id == preselectBed; });
            if (b) { $('#mw_ward_select').val(b.ward_id).trigger('change'); setTimeout(function () { loadBeds(b.ward_id, preselectBed); }, 300); }
        });
    }
});
</script>
@endsection
