{{-- Donor create/edit modal form. A donor IS an ERP contact (type customer) with
     this satellite; the contact picker searches ERP contacts via mbb.donors.search. --}}
@php $isEdit = $donor->exists; @endphp
<div class="modal-dialog" role="document">
  <div class="modal-content">
    <form class="mbb-form"
        action="{{ $isEdit ? route('mbb.donors.update', $donor->id) : route('mbb.donors.store') }}"
        method="POST">
      {{ csrf_field() }}
      @if($isEdit) {{ method_field('PUT') }} @endif
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">{{ $isEdit ? __('medbloodbank::lang.edit_donor') : __('medbloodbank::lang.add_donor') }}</h4>
      </div>
      <div class="modal-body">
        <div class="row">
          <div class="col-md-8 form-group">
            <label>@lang('medbloodbank::lang.contact') *</label>
            @if($isEdit)
              {{-- contact_id is immutable on edit (the donor's identity). --}}
              <input type="text" class="form-control" value="{{ $contactName }}" disabled>
            @else
              <select name="contact_id" id="mbb_contact_id" class="form-control" style="width:100%" required></select>
              <small class="text-muted">@lang('medbloodbank::lang.contact_picker_hint')</small>
            @endif
          </div>
          <div class="col-md-4 form-group">
            <label>@lang('medbloodbank::lang.blood_group')</label>
            <select name="blood_group" class="form-control">
              <option value="">@lang('medbloodbank::lang.unknown')</option>
              @foreach($bloodGroups as $bg)
                <option value="{{ $bg }}" @selected($donor->blood_group === $bg)>{{ $bg }}</option>
              @endforeach
            </select>
          </div>
        </div>
        <div class="row">
          <div class="col-md-4 form-group">
            <label>@lang('medbloodbank::lang.deferred_until')</label>
            <input type="date" name="deferred_until" class="form-control"
                value="{{ optional($donor->deferred_until)->toDateString() }}">
          </div>
          <div class="col-md-8 form-group">
            <label>@lang('medbloodbank::lang.deferral_reason')</label>
            <input type="text" name="deferral_reason" class="form-control" value="{{ $donor->deferral_reason }}">
          </div>
        </div>
        <div class="row">
          <div class="col-md-12 form-group">
            <label>@lang('medbloodbank::lang.notes')</label>
            <textarea name="notes" class="form-control" rows="2">{{ $donor->notes }}</textarea>
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">@lang('medbloodbank::lang.cancel')</button>
        <button type="submit" class="btn btn-primary">@lang('medbloodbank::lang.save')</button>
      </div>
    </form>
  </div>
</div>
@if(!$isEdit)
<script>
$(function () {
    $('#mbb_contact_id').select2({
        dropdownParent: $('.mbb_modal'),
        width: '100%',
        minimumInputLength: 1,
        ajax: {
            url: '{{ route('mbb.donors.search') }}',
            dataType: 'json',
            delay: 300,
            data: function (params) { return { q: params.term }; },
            processResults: function (data) { return { results: data }; }
        },
        placeholder: '{{ __('medbloodbank::lang.search_contact') }}',
        language: {
            inputTooShort: function () { return '{{ __('medbloodbank::lang.type_to_search') }}'; },
            noResults: function () { return '{{ __('medbloodbank::lang.no_contacts') }}'; }
        }
    });
});
</script>
@endif
