<%= form_for(OpenCashRegister.new, remote: true, :html => {:class=>"form-horizontal", :id=> "open_cash_register_form"}) do |f| %>
  <div class="portlet-body form">
    <div id="error_explanation"></div>
    <div class="row">
      <div class="col-md-12">
        <%
          available_cash = Array.new
          open_cash_registers = current_user.pointsale.open_cash_registers.abiertas

          current_user.pointsale.cash_registers.activos.each do |cash|
            is_opened = OpenCashRegister.where(:cash_register_id => cash.id, :status => '0').any?
            available_cash << cash if is_opened == false
          end
        %>
        <% unless open_cash_registers.blank? %>
          <div class="alert alert-warning">
            <strong>Ya hay caja(s) abiertas en el punto de venta:</strong><br>
              <% open_cash_registers.each do |open_cash| %>
              -<%= open_cash.cash_register.name %> <br>
              <% end %>
          </div>
        <% end %>
        <!-- caja registradora -->
        <div class="form-group">
          <%= f.label :cash_register_id, "Caja registradora", {:class=>"col-md-offset-1 col-md-3 control-label"} do %> Caja registradora
          <span class="required">*</span>
          <% end %>
          <div class="input-group col-md-4 select2-bootstrap-prepend">
            <%= f.collection_select :cash_register_id, available_cash, :id, :name, {:prompt => "Seleccione", :selected => (available_cash[0] if available_cash.size == 1)}, {:class => "form-control select2", :disabled =>  (true if available_cash.size == 1) } %>
            <%= f.hidden_field :cash_register_id, {:id=>'cash_id'} %>
          </div>
        </div>
        <!-- initial cash -->
        <div class="form-group">
          <%= f.label :initial_cash, "Efectivo inicial", {:class=>"col-md-offset-1 col-md-3 control-label"} do %> Efectivo inicial <span class="required">*</span>
          <% end %>
          <div class="col-md-4" style="padding-left: 0px;padding-right:0px">
            <%= f.number_field :initial_cash, {:class=>"form-control"} %>
          </div>
        </div>
      </div>
    </div>
    <h4 class="form-section" style="margin:20px 0px 10px 0px"></h4>
    <div class="actions">
      <%= f.submit 'guardar', {:class=>"hidden"} %>
      <button type="button" class="btn btn-success" onclick="submitForm()" id="openCashButton" disabled>Abrir caja</button>
    </div>
  </div>
<% end %>
<script type="text/javascript">
  toastr.options = {
    "closeButton": false,
    "debug": false,
    "positionClass": "toast-top-center",
    "onclick": null,
    "showDuration": "1000",
    "hideDuration": "1000",
    "timeOut": "5000",
    "extendedTimeOut": "1000",
    "showEasing": "swing",
    "hideEasing": "linear",
    "showMethod": "fadeIn",
    "hideMethod": "fadeOut"
  }

  if ($('#open_cash_register_cash_register_id').val()) {
    getCashFund($('#open_cash_register_cash_register_id').val());
    $('#cash_id').val($('#open_cash_register_cash_register_id').val());
  }

  $('#open_cash_register_cash_register_id').on('change', function() {
    if ($(this).val() != "") {
      getCashFund($(this).val());
      $('#cash_id').val($('#open_cash_register_cash_register_id').val());
    } else {
      $('#openCashButton').attr('disabled', true);
    }
  });


  function submitForm() {
    if ($('#open_cash_register_cash_register_id').val() != "") {
      $('#open_cash_register_form').submit();
    } else {
      toastr["warning"]("Se debe indicar efectivo inicial.");
    }
  }

  function getCashFund(id) {
    $.ajax({
      type: "get",
      url:  '/cash_registers/' + id +'/get_cash_fund',
      dataType: 'json',
      success: function(data) {
        $('#open_cash_register_initial_cash').val(data);
        $('#openCashButton').attr('disabled', false);
      },
    });
  }
</script>
