<!-- BEGIN FORM-->
<%= form_for(@expense, :html => {:class=>"form-horizontal"}) do |f| %>
<div class="portlet-body form">
  <% if @expense.errors.any? %>
    <div class="alert alert-danger">
      <strong>Tiene <%= pluralize(@expense.errors.count, "error") %> no se puede guardar el gasto</strong><br>
    </div>
  <% end %>
  <div class="form-body">
    <div class="row">
      <div class="col-md-8">
        <%= hidden_field_tag :concept_purchase_payment, @concept_purchase_payment.id %>
        <div class="form-group">
          <%= f.label :expense_code, {:class=>"col-md-3 control-label"} do %> Código de egreso
          <span class="required">*</span>
          <% end %>
          <div class="col-md-4 input-group">
            <span class="input-group-addon"><i class="fa fa-barcode"></i></span>
            <%= f.text_field :expense_code, { :class=>"form-control", :readonly => true} %>
          </div>
        </div>
        <% if current_user.usertype == "A" %>
          <div class="form-group">
            <%= f.label :expense_date, "Fecha", {:class=>"col-md-3 control-label"} do %> Fecha
            <span class="required">*</span>
            <% end %>
                <div class="col-sm-4" style="padding-left:0px;padding-right:0px;">
                    <div class='input-group date' id='datetimepicker1'>
                        <span class="input-group-addon">
                            <span class="glyphicon glyphicon-calendar"></span>
                        </span>
                        <%= f.text_field :expense_date, class: 'form-control'%>
                    </div>
                </div>
          </div>
        <% end %>
        <% if current_user.usertype != "A" %>
          <div class="form-group">
            <%= f.label :open_cash_register_id, "Caja registradora", {:class=>"col-md-3 control-label"} do %> Caja registradora
            <span class="required">*</span>
            <% end %>
            <div class="input-group col-md-4 select2-bootstrap-prepend">
              <%= f.select :open_cash_register_id, @current_user.pointsale.open_cash_registers.abiertas.map{|o| [o.cash_register.name, o.id]}, {:prompt => "Seleccione"}, { :class => 'form-control select2', :disabled => @is_cashier} %>
              <% if current_user.usertype == 'C' %>
                <%= f.hidden_field :open_cash_register_id %>
              <% end %>
            </div>
          </div>
        <% end %>
        <div class="form-group">
          <%= f.label :expensesconcept_id, "Concepto", {:class=>"col-md-3 control-label"} do %>Concepto de egreso
          <span class="required">*</span>
          <% end %>
          <div class="input-group col-md-4 select2-bootstrap-prepend">
            <%= f.collection_select :expensesconcept_id, @expenses_concepts, :id, :name, {:prompt => "Seleccione"}, {:class => "form-control select2", :style => "width: 100%"} %>
          </div>
        </div>

        <div class="form-group hidden" id="purchases_div">
          <%= f.label :purchases, "Concepto", {:class=>"col-md-3 control-label"} do %>Compras
          <span class="required">*</span>
          <% end %>
          <div class="input-group col-md-4 select2-bootstrap-prepend">
            <% if current_user.usertype == 'A' %>
              <%= select_tag "purchases", options_from_collection_for_select(Purchase.notpaid, :id, :code_with_price), :include_blank => "Seleccione",  class: "form-control select2" %>
            <% else %>
              <%= select_tag "purchases", options_from_collection_for_select(Purchase.notpaid.where(:pointsale_id => current_user.pointsale_id), :id, :code_with_price), :include_blank => "Seleccione",  class: "form-control select2" %>
            <% end %>
          </div>
        </div>
        <div class="form-group">
          <%= f.label :quantity,  {:class=>"col-md-3 control-label"} do %>Cantidad <span class="required">*</span>
          <% end %>
          <div class="col-md-4" style="padding-left:0px;padding-right:0px">
            <%= f.number_field :quantity, {:class=>"form-control" }  %>
          </div>
        </div>
        <div class="form-group">
          <%= f.label :observations, "Observaciones", {:class=>"col-md-3 control-label"} %>
          <div class="col-md-9" style="padding-left:0px">
            <%= f.text_area :observations, {:class=>"form-control", :rows=>5} %>
          </div>
        </div>
    </div>
  </div>
  <div class="form-actions">
    <div class="row">
      <div class="col-md-9">
        <%= f.submit 'Guardar', {:class=>"btn green"} %>
        <%= link_to 'Cancelar', expenses_path, {:class=>"btn default"} %>
      </div>
    </div>
  </div>
</div>
<% end %>
<script type="text/javascript">
  $(document).on('page:change', function() {
    App.init();
    generateExpenseCode();
    $("select.select2").select2({
      allowClear: true,
      placeholder: 'Todas'
    });

     $('#datetimepicker1').datetimepicker({
      icons: {
        date: "fa fa-calendar"
      },
      format: "DD/MM/YYYY",
      defaultDate: new Date()
     });
  });

  $('#expense_expensesconcept_id').on('change', function() {
    if( $('#concept_purchase_payment').val() == $(this).val() ) {
      $('#purchases_div').removeClass('hidden');
      $("#purchases").select2();
    } else {
      $('#purchases_div').addClass('hidden');
      $('#purchases').select2('val', null);
    }
  });

    $('#expense_open_cash_register_id').on('change', function() {
      generateExpenseCode();
    });

  function generateExpenseCode() {
    <% if current_user.usertype == 'A' %>
      $.ajax({
        type: "get",
        url:  '/get_next_expense_code/0',
        dataType: 'text',
        success: function(data) {
          $('#expense_expense_code').val(data);
        },
      });
    <% else %>
      if($('#expense_open_cash_register_id').length > 0 && $('#expense_open_cash_register_id').val()) {
        $.ajax({
          type: "get",
          url:  '/get_next_expense_code/' + $('#expense_open_cash_register_id').val(),
          dataType: 'text',
          success: function(data) {
            $('#expense_expense_code').val(data);
          },
        });
      }
    <% end %>
  }
</script>

