<%= form_for(@commission, :html => {:class=>"form-horizontal"}) do |f| %>
<div class="portlet-body form">
    <div class="form-body">
      <div class="row">
        <%= f.hidden_field :pointsale_id %>
        <div class="form-group">
          <%= f.label :pointsale, "Punto de venta", {:class=>"col-md-2 control-label"} do %> Punto de venta <span class="required">*</span>
          <% end %>
          <div class="col-md-3" style="padding-left:0px;padding-right:0px">
            <%= text_field_tag :pointsale, @commission.pointsale.name, class: "form-control", disabled: true %>
          </div>
        </div>
        <div class="form-group">
          <%= f.label :date, "Fecha", {:class=>"col-md-2 control-label"} do %> Fecha <span class="required">*</span>
          <% end %>
          <div class="col-md-3" style="padding-left:0px;padding-right:0px">
              <div class='input-group date' id='date'>
                  <input id="start" type='text' class="form-control" readonly="true"/>
                  <span class="input-group-addon">
                      <span class="glyphicon glyphicon-calendar"></span>
                  </span>
              </div>
          </div>
        </div>

        <div class="form-group">
          <%= f.label :initial_date, "Inicio", {:class=>"col-md-2 control-label"} do %>Inicio del periodo<span class="required">*</span>
          <% end %>
          <div class="col-md-3" style="padding-left: 0px;padding-right: 0px">
            <%= text_field_tag :initial_date_display, l(@commission.initial_date, :format => '%d/%m/%Y'), class: "form-control", disabled: true %>
            <%= f.hidden_field :initial_date %>
          </div>
        </div>

        <div class="form-group">
          <%= f.label :final_date, "Fin", {:class=>"col-md-2 control-label"} do %> Fin del periodo<span class="required">*</span>
          <% end %>
          <div class="col-md-3" style="padding-left: 0px;padding-right: 0px">
            <%= text_field_tag :final_date_display, l(@commission.final_date, :format => '%d/%m/%Y'), class: "form-control", disabled: true %>
            <%= f.hidden_field :final_date %>
          </div>
        </div>

        <div class="form-group">
          <%= f.label :commission_total, "Total", {:class=>"col-md-2 control-label"} do %> Total vendido<span class="required">*</span>
          <% end %>
          <div class="col-md-3" style="padding-left: 0px;padding-right: 0px">
            <%= text_field_tag :commission_total_display, number_to_currency(@commission.commission_total, precision: 2), class: "form-control", disabled: true %>
            <%= f.hidden_field :commission_total %>
          </div>
        </div>

      </div>

      <h4 class="form-section">Lista de comisiones a pagar</h4>
                        <table class="table table-striped table-bordered table-hover tableadvanced" id="products_table">
                          <thead>
                              <tr>
                                <th>Vendedor</th>
                                <th>Ventas en <br>el periodo</th>
                                <th>Total vendido <br>contado</th>
                                <th>Total vendido <br>créditos <br>y apartados</th>
                                <th>Total <br>vendido</th>
                                <th>$ ingresado<br>contado</th>
                                <th>$ ingresado<br>créditos <br>y apartados</th>
                                <th>Total <br>ingresado</th>
                                <th>Pago <br>recomendado</th>
                                <th>A pagar</th>
                              </tr>
                          </thead>

                          <tbody id="sellers_table">
                            <% @sellers.each_with_index do |seller| %>
                              <tr>
                                <% data = seller.get_data_for_commissions(@initial_date, @final_date) %>
                                <% commission_calculated = ((@pos_config.commission_percent / 100.0) * (data['income_by_cash'] + data['income_by_reserved_and_credits'])).round(2) %>
                                <td> <%= seller.name %> </td>
                                <td> <%= data['sales_count'] %> </td>
                                <td> <%= number_to_currency(data['total_sold_cash'], precision: 2) %> </td>
                                <td> <%= number_to_currency(data['total_sold_credits_apartments'], precision: 2) %> </td>
                                <td> <%= number_to_currency(data['total_sold_cash'] + data['total_sold_credits_apartments'], precision: 2) %> </td>
                                <td> <%= number_to_currency(data['income_by_cash'], precision: 2) %> </td>
                                <td><%= number_to_currency(data['income_by_reserved_and_credits'], precision: 2) %></td>
                                <td><%= number_to_currency(data['income_by_cash'] + data['income_by_reserved_and_credits'], precision: 2) %></td>
                                <td><%= number_to_currency(commission_calculated, precision: 2) %></td>
                                <td>
                                  <%= f.fields_for :sellerscommissions do |b| %>
                                    <%= b.number_field :commission_paid, {:class =>"form-control", :value => commission_calculated, input_html: {min: 0}, :step =>"any", :pattern =>"^[0-9]*[1-9][0-9]*$"} %>
                                    <%= b.hidden_field :seller_id, :value => seller.id %>
                                    <%= b.hidden_field :num_sales, :value => data['sales_count'] %>
                                    <%= b.hidden_field :sales_total, :value => (data['total_sold_cash'] + data['total_sold_credits_apartments']) %>
                                    <%= b.hidden_field :commission_calculated, :value => commission_calculated %>
                                    <%= b.hidden_field :commission_percent, :value => @pos_config.commission_percent %>
                                    <%= b.hidden_field :total_sold_cash, :value => data['total_sold_cash'] %>
                                    <%= b.hidden_field :total_sold_credits_apartments, :value => data['total_sold_credits_apartments'] %>
                                    <%= b.hidden_field :income_by_cash, :value => data['income_by_cash'] %>
                                    <%= b.hidden_field :income_by_reserved_and_credits, :value => data['income_by_reserved_and_credits'] %>
                                  <% end %>
                                </td>
                              </tr>
                            <% end %>
                          </tbody>
                        </table>

    </div>
    <div class="form-actions">
      <div class="row">
        <div class="col-md-9" style="padding-left: 0px;padding-right: 0px">
          <%= f.submit 'Guardar', {:class=>"btn green"} %>
          <%= link_to 'Cancelar', commissions_path(:filter => @filter, :current_page => @current_page), {:class=>"btn default"} %>
        </div>
      </div>
    </div>
  </div>
<% end %>

<script type="text/javascript">

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

  $(document).ready(function() {
    handleInputMasks();
    App.init();
  });

</script>
