<div class="page-container">
  <div class="page-content-wrapper">
    <div class="page-head">
      <div class="container-fluid">
        <div class="page-title">
          <h1>Reporte de mínimos y máximos por punto de venta</h1>
        </div>
      </div>
    </div>
    <div class="page-content">
      <div class="container-fluid">
        <ul class="page-breadcrumb breadcrumb">

        </ul>
        <div class="page-content-inner">
          <div id="notice"><%= notice %></div>
          <div class="portlet light hidden-print">
            <div class="portlet-body">
              <div class="row">
                <div class="col-md-12">
                  <div class="booking-search">
                    <%= form_tag(min_max_path, method: "get", remote: true, id: "minmax_form") do %>
                      <div class="row form-group">
                        <!-- punto de venta -->
                        <%= label :pointsale_id, "", { class: "col-md-1 col-sm-2 control-label" } do %>Punto de venta<% end %>
                        <div class="col-md-4 col-sm-5 select2-bootstrap-prepend">
                          <%= select_tag :pointsale_id, options_from_collection_for_select(@pointsales, :id, :name, selected: @pointsales.first), class: "form-control select2" %>
                        </div>
                      </div>
                      <div class="row form-group">
                        <!-- categoria -->
                        <%= label :category, "", { class: "col-md-1 col-sm-1 control-label" } do %>Línea <% end %>
                        <div class="col-md-4 col-sm-4 select2-bootstrap-prepend">
                          <%= select_tag :category, options_for_select(Category.activos_padre.collect{ |p| [ "#{p.category} - #{p.description}", p.id ] }), include_blank: "Todas", class: "form-control select2-allow-clear-todas input-medium", onchange: 'getSubCategories($(this).val())' %>
                        </div>
                        <!-- sub- categoria -->
                        <%= label :subcategory, "", { class: "col-md-1 col-sm-1 control-label" } do %>Sublínea <% end %>
                        <div class="col-md-4 col-sm-4 select2-bootstrap-prepend">
                          <div class="select2-bootstrap-prepend">
                            <%= collection_select("", :subcategory, ({}), :id, :category_and_description, options = { include_blank: "Todas" }, html_options = { class: 'form-control select2-allow-clear-todas input-medium' }) %>
                          </div>
                        </div>
                        <div class="col-md-2 col-sm-1">
                          <button type="button" onclick="applyFilter()" class="btn blue">Buscar</button>
                        </div>
                      </div>
                    <% end %>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="portlet light">
            <div class="portlet-title">
              <div class="caption">
                <i class="fa fa-list "></i>
                <span class="caption-subject bold uppercase">Lista de productos</span>
              </div>
            </div>
            <div class="portlet-body ">
              <div id="error_explanation"></div>
              <%= hidden_field_tag 'title_for_print' %>
              <!-- lista de productos -->
              <table class="table table-hover table-bordered table-striped tableadvancedprintable" id="minmax_table">
                <thead>
                  <th>#</th>
                  <th width="25%">Producto</th>
                  <th>Precio<br>unitario</th>
                  <th width="25%">Línea</th>
                  <th>Sublínea</th>
                  <th class="success">Cantidad recomendada<br> a comprar</th>
                  <th>Total</th>
                </thead>
                <tbody>
                  <% total = 0 %>
                  <% @products.each_with_index do |available, key| %>
                    <tr>
                      <td><%= key += 1 %></td>
                      <td><%= available.product.full_display %></td>
                      <td><%= number_to_currency(available.product.price_base, precision: 2) %></td>
                      <td><%= available.product.category.parent_id.zero? ? available.product.category.category : available.product.category.parent.category %></td>
                      <td><%= available.product.category.parent_id != 0 ? available.product.category.category : ' ' %></td>
                      <td class="success"><%= quantity = calculate_restock(available) %></td>
                      <td><%= number_to_currency((quantity * available.product.price_base), precision: 2) %></td>
                    </tr>
                    <% total += (quantity * available.product.price_base) %>
                  <% end %>
                  <%= hidden_field_tag :total, total %>
                </tbody>
              </table>
            </div>
          </div>
          <div class="row">
            <div class="col-md-offset-4 col-md-3">
              <div class="text-center well" id="container_total_prods" style="margin-bottom: 0px">
                <div class="font-grey-mint font-sm">Total </div>
                <div class="uppercase font-hg font-blue-sharp" id="total_prods"></div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<script type="text/javascript">
  $(document).on("page:change", function() {
    $('#total_prods').html(accounting.formatMoney($("#total").val()) + ' MXN');
    $('#title_for_print').val('Reporte de mínimos y máximos por punto de venta');
  });


  function applyFilter() {
    $("#minmax_form").submit();
  }

  function getSubCategories(value) {
    $('#_subcategory').html('');
    $('#_subcategory').select2("destroy").select2({
      placeholder: 'Seleccione',
      "language": {
        "noResults": function(){
          return "No se encontraron coincidencias";
        }
      }
    });
    $('#_subcategory').select2('val', null);
    if(value) {
      $.ajax({
        type: "get",
        url: '/getcategories/' + value,
        dataType: 'json',
        success: function(data) {
          if(data.length > 0) {
            $('#_subcategory').append('<option></option>')
            for(i in data) {
              $('#_subcategory').append("<option value='" + data[i].id + "'>" + data[i].category + "</option>")
            }
            $('#_subcategory').select2({
              allowClear: true,
              placeholder: 'Todas',
              "language": {
                "noResults": function(){
                  return "No se encontraron coincidencias";
                }
              }
            });
          }
        }
      });
    }
  }
</script>
