{{-- Manual journal-entry create modal with dynamic debit/credit lines. --}}
<div class="modal-dialog modal-lg" role="document">
  <div class="modal-content">
    <form class="medacc-form" action="{{ route('medacc.journal.store') }}" method="POST">
      {{ csrf_field() }}
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">@lang('medaccounting::lang.new_journal_entry')</h4>
      </div>
      <div class="modal-body">
        <div class="row">
          <div class="col-md-4 form-group">
            <label>@lang('medaccounting::lang.date') *</label>
            <input type="date" name="date" class="form-control" required value="{{ now()->toDateString() }}">
          </div>
          <div class="col-md-8 form-group">
            <label>@lang('medaccounting::lang.description')</label>
            <input type="text" name="description" class="form-control">
          </div>
        </div>

        <table class="table table-bordered" id="medacc_je_lines">
          <thead><tr>
            <th style="width:32%">@lang('medaccounting::lang.account')</th>
            <th style="width:20%">@lang('medaccounting::lang.cost_center')</th>
            <th>@lang('medaccounting::lang.description')</th>
            <th style="width:14%">@lang('medaccounting::lang.debit')</th>
            <th style="width:14%">@lang('medaccounting::lang.credit')</th>
            <th style="width:36px"></th>
          </tr></thead>
          <tbody>
            {{-- two starter rows --}}
            @for($i = 0; $i < 2; $i++)
            <tr class="medacc-je-row">
              <td>
                <select name="lines[{{ $i }}][account_id]" class="form-control input-sm">
                  <option value="">—</option>
                  @foreach($accounts as $id => $label)
                    <option value="{{ $id }}">{{ $label }}</option>
                  @endforeach
                </select>
              </td>
              <td>
                <select name="lines[{{ $i }}][cost_center_id]" class="form-control input-sm">
                  <option value="">—</option>
                  @foreach($costCenters as $id => $name)
                    <option value="{{ $id }}">{{ $name }}</option>
                  @endforeach
                </select>
              </td>
              <td><input type="text" name="lines[{{ $i }}][description]" class="form-control input-sm"></td>
              <td><input type="number" step="0.01" min="0" name="lines[{{ $i }}][debit]" class="form-control input-sm medacc-debit" value="0"></td>
              <td><input type="number" step="0.01" min="0" name="lines[{{ $i }}][credit]" class="form-control input-sm medacc-credit" value="0"></td>
              <td class="text-center"><button type="button" class="btn btn-xs btn-danger medacc-je-remove"><i class="fa fa-times"></i></button></td>
            </tr>
            @endfor
          </tbody>
          <tfoot>
            <tr>
              <th colspan="3" class="text-right">@lang('medaccounting::lang.totals')</th>
              <th class="text-right"><span id="medacc_total_debit">0.00</span></th>
              <th class="text-right"><span id="medacc_total_credit">0.00</span></th>
              <th></th>
            </tr>
            <tr>
              <td colspan="6">
                <button type="button" class="btn btn-xs btn-default" id="medacc_add_line">
                  <i class="fa fa-plus"></i> @lang('medaccounting::lang.add_line')
                </button>
                <span class="pull-right" id="medacc_balance_hint"></span>
              </td>
            </tr>
          </tfoot>
        </table>
        <p class="help-block">@lang('medaccounting::lang.balance_hint')</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">@lang('medaccounting::lang.cancel')</button>
        <button type="submit" class="btn btn-primary">@lang('medaccounting::lang.post_entry')</button>
      </div>
    </form>
  </div>
</div>

<script>
(function () {
    var idx = 2;
    var $table = $('#medacc_je_lines');

    function recalc() {
        var d = 0, c = 0;
        $table.find('.medacc-debit').each(function () { d += parseFloat($(this).val()) || 0; });
        $table.find('.medacc-credit').each(function () { c += parseFloat($(this).val()) || 0; });
        $('#medacc_total_debit').text(d.toFixed(2));
        $('#medacc_total_credit').text(c.toFixed(2));
        var hint = $('#medacc_balance_hint');
        if (Math.abs(d - c) < 0.005 && d > 0) {
            hint.html('<span class="label label-success">{{ __('medaccounting::lang.balanced') }}</span>');
        } else {
            hint.html('<span class="label label-warning">{{ __('medaccounting::lang.not_balanced') }}</span>');
        }
    }

    $table.on('input', '.medacc-debit, .medacc-credit', recalc);

    $('#medacc_add_line').on('click', function () {
        var $first = $table.find('tbody tr.medacc-je-row').first();
        var $row = $first.clone();
        $row.find('select, input').each(function () {
            var name = $(this).attr('name');
            if (name) { $(this).attr('name', name.replace(/lines\[\d+\]/, 'lines[' + idx + ']')); }
            if ($(this).is('input[type=number]')) { $(this).val('0'); }
            else if ($(this).is('input')) { $(this).val(''); }
            else { $(this).val(''); }
        });
        $table.find('tbody').append($row);
        idx++;
        recalc();
    });

    $table.on('click', '.medacc-je-remove', function () {
        if ($table.find('tbody tr.medacc-je-row').length > 2) {
            $(this).closest('tr').remove();
            recalc();
        }
    });

    recalc();
})();
</script>
