<!-- BEGIN CONTAINER -->
<div class="page-container">
  <!-- BEGIN CONTENT -->
  <div class="page-content-wrapper">
    <!-- BEGIN CONTENT BODY -->
    <!-- BEGIN PAGE HEAD-->
    <div class="page-head">
      <div class="container-fluid">
        <!-- BEGIN PAGE TITLE -->
        <div class="page-title">
          <h1>Reporte de utilidades por periodo </h1>
        </div>
        <!-- END PAGE TITLE -->
      </div>
    </div>
    <!-- END PAGE HEAD-->
    <!-- BEGIN PAGE CONTENT BODY -->
    <div class="page-content">
      <div class="container-fluid">
        <!-- BEGIN PAGE BREADCRUMBS -->
        <ul class="page-breadcrumb breadcrumb">
          <%= render_breadcrumbs :tag => :li, :separator => ' <i class="fa fa-circle"></i> ' %>
        </ul>
        <!-- END PAGE BREADCRUMBS -->
        <div class="search-page search-content-3">
          <div class="row">
            <div class="col-md-12">
              <%= form_tag(utilities_report_path, method: :get, remote: true) do |f| %>
                <div class="search-filter bordered">
                <!-- <h4 class=" uppercase form-section"> Búsqueda de producto </h4> <hr> -->
                  <div class="form-group">
                    <div class="row">
                      <div class="col-md-3 col-sm-4">
                        <div class="search-label uppercase">Punto de venta</div>
                        <div class="col-md-12">
                          <%= select_tag "pointsale", options_from_collection_for_select(Pointsale.vigentes, :id, :name), include_blank: "Todos", class: "form-control  input-medium" %>
                        </div>
                      </div>
                      <div class="col-md-3 col-sm-4">
                        <div class="search-label uppercase"> Del </div>
                        <div class="col-md-6">
                          <div class="input-group input-medium date" data-date-format="dd/mm/yyyy">
                            <%= text_field_tag "start_date", l(Date.today.beginning_of_month, format: '%d/%m/%Y'), class: "form-control" %>
                            <span class="input-group-addon">
                              <span class="glyphicon glyphicon-calendar"></span>
                            </span>
                          </div>
                        </div>
                      </div>
                      <div class="col-md-3 col-sm-4">
                        <div class="search-label uppercase"> Al </div>
                        <div class="col-md-6">
                          <div class="input-group input-medium date" data-date-format="dd/mm/yyyy">
                            <%= text_field_tag "end_date", l(Date.today.end_of_month, format: '%d/%m/%Y'), class: "form-control" %>
                            <span class="input-group-addon">
                              <span class="glyphicon glyphicon-calendar"></span>
                            </span>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                  <div class="form-group">
                    <div class="row">
                      <div class="col-md-3 col-sm-4">
                        <!-- categoria -->
                        <div class="search-label uppercase">Línea</div>
                        <div class="col-md-12">
                          <div class="select2-bootstrap-prepend">
                            <%= select_tag "category", options_from_collection_for_select(Category.activos_padre, :id, :category), include_blank: "Todas",  class: "form-control select2-allow-clear-todas", onchange: 'getSubCategories($(this).val());'%>
                          </div>
                        </div>
                      </div>
                      <div class="col-md-3 col-sm-4">
                        <!-- sub- categoria -->
                        <div class="search-label uppercase">Sublínea</div>
                        <div class="col-md-12">
                          <div class="select2-bootstrap-prepend">
                            <%= collection_select("", :subcategory, ({}), :id, :category, options = { include_blank: "Todas" }, html_options = { class: 'form-control select2-allow-clear-todas input-medium' }) %>
                          </div>
                        </div>
                      </div>
                      <div class="col-md-3 col-sm-4" style="margin-top: 20px">
                        <%= submit_tag 'Filtrar', { class: "btn green bold btn-block", style: 'margin:0px' } %>
                      </div>
                    </div>
                  </div>
                </div>
              <% end %>
            </div>
          </div>
        </div>
        <!-- BEGIN PAGE CONTENT INNER -->
        <div class="page-content-inner">
          <div class="portlet light">
            <div class="portlet-title">
              <div class="caption">
                <span class="caption-subject font-blue-dark bold uppercase">Ventas e ingresos</span>
              </div>
            </div>
            <div class="portlet-body form" id="sales_content">
              <div class="product_track_search">
                <div class="row">
                  <div class="col-md-12">
                    <div class="note note-success hidden" id="note_info">
                      <h4 class="block">Detalle de utilidades del periodo: <strong><span id="start_date_display"></span></strong> al <strong><span id="end_date_display"></span></strong></h4>
                    </div>
                    <div class="col-xs-offset-1 col-xs-5">
                      <div class="text-center well" id="container_total_prods" style="margin-bottom: 0px">
                        <div class="font-grey-mint font-sm">NÚMERO DE VENTAS</div>
                        <div class="uppercase font-hg font-blue-sharp" id="sales_quantity">0</div>
                      </div>
                    </div>
                    <div class="col-xs-offset-1 col-xs-5">
                      <div class="text-center well" id="container_total_prods" style="margin-bottom: 0px">
                        <div class="font-grey-mint font-sm">PRODUCTOS VENDIDOS</div>
                        <div class="uppercase font-hg font-blue-sharp" id="prods_total">0</div>
                      </div>
                    </div>
                  </div>
                </div>
                <div class="row" style="margin-top: 20px">
                  <div class="col-md-12">
                    <div class="col-xs-offset-1 col-xs-5">
                      <div class="text-center well" id="container_total_prods" style="margin-bottom: 0px">
                        <div class="font-grey-mint font-sm">TOTAL UTILIDAD</div>
                        <div class="uppercase font-hg font-green" id="total_utility">0</div>
                      </div>
                    </div>
                  </div>
                </div>

                <br>
                <div class="row">
                  <div class="col-md-12">
                    <h4 class="form-section">Desglose de ventas</h4>
                    <table class="table table-striped table-bordered table-hover tableadvanced" id="utility_report_table">
                      <thead>
                        <tr>
                          <th>Fecha</th>
                          <th>Folio</th>
                          <th>Punto<br>de<br>venta</th>
                          <th>Productos</th>
                          <th>Tipo</th>
                          <th>Status</th>
                          <th>Subtotal</th>
                          <th>IVA</th>
                          <th>Descto.</th>
                          <th>Total</th>
                          <th>Costo<br>total</th>
                          <th>Utilidad</th>
                        </tr>
                      </thead>
                      <tbody>
                      </tbody>
                    </table>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <!-- END PAGE CONTENT INNER -->
      </div>
    </div>
    <!-- END PAGE CONTENT BODY -->
    <!-- END CONTENT BODY -->
  </div>
  <!-- END CONTENT -->
</div>
<script>
  $(document).on("page:change", function() {
    App.init();
     $('#start_date').datetimepicker({
      icons: {
        date: "fa fa-calendar"
      },
      format: "DD/MM/YYYY"
    });

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

  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>
