<%= form_for(AvailableProduct.new, :html => {:class=>"form-horizontal", :id=> "available_products_form"}) do |f| %>
  <div class="portlet-body form">
    <div id="error_explanation"></div>
      <table class="table table-striped table-bordered table-hover tableadvancedprintableAjax" id="pointsale_stock" data-source="<%= stock_by_pointsale_path(format: "json") %>" >
        <thead>
          <tr>
            <th>Imagen</th>
            <th>Producto</th>
            <th>Linea</th>
            <th>Sublínea</th>
            <% if current_user.usertype != 'C' %>
              <th>Stock mínimo</th>
              <th>Stock máximo</th>
            <% end %>
            <th>Existencia</th>
            <% if current_user.usertype != "S" %>
              <th>Precio</th>
            <% end %>
          </tr>
        </thead>
        <tbody>
        </tbody>
      </table>
    </div>
  </div>
<% end %>

<script type="text/javascript">
  $(document).on("page:change", function() {
    App.init();
    table = $('#pointsale_stock').DataTable();
    if($('#pointsale').val()) {
      $('#pointsale').trigger("change");
    }
  });

  $('body').barcodeListener().on('barcode.valid', function(e, code) {
    $('input[type=search]').val(code);
    $('#pointsale_stock input').unbind();
    $('#pointsale_stock').dataTable().fnFilter(code);
  });

  $('#category, #sub_category, #pointsale').on('change', function(e) {
    table.draw();
    getTotalProducts();
    getTotalInvested();
  });

  function getSub_categories() {
    $('#sub_category').attr('disabled', true);
    category =  $('#category').val() ? $('#category').val() : 0;
    $.ajax({
      type: "get",
      url:  '/getcategories/' + category,
      dataType: 'json',
      success: function(data) {
        $('#sub_category').html('');
        for (var i = 0; i < data.length; i++) {
          $('#sub_category').append($('<option>', {
            value: data[i].id,
            text : data[i].category
          }));
        }
        $('#sub_category').select2({ allowClear: true, placeholder: 'Todas'});
        $('#sub_category').attr('disabled', false);
        $('#sub_category').select2('val', null);
      }
    });
  }

  function getTotalProducts() {
    var location = $('#pointsale').val();
    var category = $('#category').val();
    var sub_category = $('#sub_category').val();
    var uri = 'location=' + location;
    if (category) { uri += '&category=' + category; }
    if (sub_category) { uri += '&sub_category=' + sub_category; }

    App.blockUI({
      target: $("#container_total_prods"),
      animate: true
    });

    $.ajax({
      type: "get",
      url:  '/total_products_by_pointsale?' + uri,
      dataType: 'json',
      success: function(data) {
        App.unblockUI($("#container_total_prods"));
        $('#total_prods').html(Math.round(data));
      }
    });
  }

  function getTotalInvested() {
    var location = $('#pointsale').val();
    var category = $('#category').val();
    var sub_category = $('#sub_category').val();
    var uri = 'location=' + location;
    if (category) { uri += '&category=' + category; }
    if (sub_category) { uri += '&sub_category=' + sub_category; }

    App.blockUI({
      target: $("#container_total_invested"),
      animate: true
    });

    App.blockUI({
      target: $("#container_total_invested_dlls"),
      animate: true
    });

    $.ajax({
      type: "get",
      url:  '/total_invested_in_pointsale?' + uri,
      dataType: 'json',
      success: function(data) {
        App.unblockUI($("#container_total_invested"));
        App.unblockUI($("#container_total_invested_dlls"));

        $('#total_invested').html(accounting.formatMoney(data.invested_in_pesos) + ' MXN');
        $('#total_invested_dlls').html(accounting.formatMoney(data.invested_in_dollars) + ' USD');
      }
    });
  }
</script>
