{{-- Review + bill modal for one appointment's draft charges. --}}
<div class="modal-dialog modal-lg" role="document">
  <div class="modal-content">
    <form class="medbill-form" action="{{ route('medbill.worklist.bill') }}" method="POST" id="medbill_bill_form">
      {{ csrf_field() }}
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">@lang('medbilling::lang.review_bill') — @lang('medbilling::lang.appointment') #{{ $appointmentId }}</h4>
      </div>
      <div class="modal-body">
        @if($charges->isEmpty())
          <p class="text-muted">@lang('medbilling::lang.no_billable_charges')</p>
        @else
        <table class="table table-bordered table-condensed">
          <thead><tr>
            <th></th>
            <th>@lang('medbilling::lang.description')</th>
            <th class="text-right">@lang('medbilling::lang.qty')</th>
            <th class="text-right">@lang('medbilling::lang.unit_price')</th>
            <th class="text-right">@lang('medbilling::lang.line_total')</th>
            <th class="text-center">@lang('medbilling::lang.billable')</th>
          </tr></thead>
          <tbody>
          @foreach($charges as $c)
            <tr>
              <td>
                <input type="checkbox" name="charge_ids[]" value="{{ $c->id }}"
                    class="medbill-charge-cb" data-total="{{ $c->lineTotal() }}"
                    {{ $c->isBillable() ? 'checked' : 'disabled' }}>
              </td>
              <td>{{ $c->description }}</td>
              <td class="text-right">{{ rtrim(rtrim(number_format($c->qty, 2), '0'), '.') }}</td>
              <td class="text-right">{{ number_format($c->unit_price, 2) }}</td>
              <td class="text-right">{{ number_format($c->lineTotal(), 2) }}</td>
              <td class="text-center">
                @if($c->isBillable())
                  <i class="fa fa-check text-success"></i>
                @else
                  <i class="fa fa-times text-muted" title="@lang('medbilling::lang.not_billable_hint')"></i>
                @endif
              </td>
            </tr>
          @endforeach
          </tbody>
          <tfoot><tr>
            <th colspan="4" class="text-right">@lang('medbilling::lang.total')</th>
            <th class="text-right"><span id="medbill_selected_total">0.00</span></th>
            <th></th>
          </tr></tfoot>
        </table>

        <div class="row">
          <div class="col-md-6 form-group">
            <label>@lang('medbilling::lang.location')</label>
            <select name="location_id" class="form-control" required>
              @foreach($locations as $id => $name)
                <option value="{{ $id }}">{{ $name }}</option>
              @endforeach
            </select>
          </div>
          <div class="col-md-6 form-group">
            <label>@lang('medbilling::lang.apply_policy')</label>
            <select name="policy_id" class="form-control" id="medbill_policy_select">
              <option value="">@lang('medbilling::lang.no_policy')</option>
              @foreach($policies as $p)
                <option value="{{ $p->id }}"
                    data-coverage="{{ $p->coverage_pct }}"
                    data-copay="{{ $p->copay_fixed }}">
                  {{ optional($p->payer)->name }} — {{ $p->policy_number }}
                  ({{ number_format($p->coverage_pct, 0) }}%{{ $p->copay_fixed > 0 ? ', copay ' . number_format($p->copay_fixed, 2) : '' }})
                </option>
              @endforeach
            </select>
          </div>
        </div>

        <div class="callout callout-info" id="medbill_split_preview" style="display:none;">
          <strong>@lang('medbilling::lang.split_preview'):</strong>
          @lang('medbilling::lang.patient_share'): <span id="medbill_patient_share">0.00</span> ·
          @lang('medbilling::lang.payer_share'): <span id="medbill_payer_share">0.00</span>
        </div>
        @endif
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">@lang('medbilling::lang.cancel')</button>
        @if(! $charges->isEmpty())
          <button type="submit" class="btn btn-primary"><i class="fa fa-file-invoice-dollar"></i> @lang('medbilling::lang.bill_now')</button>
        @endif
      </div>
    </form>
  </div>
</div>
<script>
$(function () {
    function recalc() {
        var total = 0;
        $('.medbill-charge-cb:checked').each(function () { total += parseFloat($(this).data('total')) || 0; });
        $('#medbill_selected_total').text(total.toFixed(2));

        var $opt = $('#medbill_policy_select option:selected');
        var coverage = parseFloat($opt.data('coverage'));
        var copay = parseFloat($opt.data('copay')) || 0;
        if ($('#medbill_policy_select').val() && !isNaN(coverage)) {
            var payCopay = Math.min(copay, total);
            var coverable = Math.max(0, total - payCopay);
            var payer = coverable * (coverage / 100);
            var patient = total - payer;
            $('#medbill_patient_share').text(Math.max(0, patient).toFixed(2));
            $('#medbill_payer_share').text(Math.max(0, payer).toFixed(2));
            $('#medbill_split_preview').show();
        } else {
            $('#medbill_split_preview').hide();
        }
    }
    $(document).on('change', '.medbill-charge-cb, #medbill_policy_select', recalc);
    recalc();
});
</script>
