<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>Apartados</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 -->
        <!-- BEGIN PAGE CONTENT INNER -->
        <div class="page-content-inner">
          <div id="notice">
          <% if success %>
            <div class="alert alert-success">
              <p><%= success %></p>
            </div>
          <% elsif warning %>
            <div class="alert alert-warning">
              <p><%= warning %></p>
            </div>
          <% end %>
          </div>
          <div class="row">
            <div class="col-md-12">
              <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 apartados</span>
                  </div>
                </div>
                <div class="portlet-body">
                  <div class="form-horizontal" style="margin-bottom:35px">
                    <div class="row">
                      <div class="col-md-11">
                          <%= hidden_field_tag 'title_for_print' %>
                        <div class="form-group">
                          <%= label_tag :begin_date, "Fecha", {:class=>"col-md-1 control-label"} do %> Desde
                          <% end %>
                          <div class="col-sm-2" style="padding-left:0px;padding-right:0px;">
                              <div class='input-group date' id='begin_date'>
                                  <input id="start" type='text' class="form-control"/>
                                  <span class="input-group-addon">
                                      <span class="glyphicon glyphicon-calendar"></span>
                                  </span>
                              </div>
                          </div>
                          <%= label_tag :end_date, "Fecha", {:class=>"col-md-1 control-label"} do %> Hasta
                          <% end %>
                          <div class="col-sm-2" style="padding-left:0px;padding-right:0px;">
                              <div class='input-group date' id='end_date'>
                                  <input id="end" type='text' class="form-control"/>
                                  <span class="input-group-addon">
                                      <span class="glyphicon glyphicon-calendar"></span>
                                  </span>
                              </div>
                          </div>
                          <button class="btn btn-icon-only blue" style="margin-left:25px" onclick="salesByDate()">
                            <i class="fa fa-search"></i>
                          </button>
                        </div>
                      </div>
                    </div>
                  </div>
                  <input type='hidden' name='filter' id='filter' value='<%= @filter %>' >
                  <input type='hidden' name='current_page' id='current_page' value='<%= @current_page %>' >
                  <table class="table table-striped table-bordered table-hover tableadvancedprintable" id="sales_table">
                    <thead>
                      <tr>
                        <th>#</th>
                        <th>Folio</th>
                        <% if current_user.usertype == 'A' %>
                          <th>Punto de venta</th>
                        <% end %>
                        <th>Cliente</th>
                        <th>Vendedor</th>
                        <th>Fecha</th>
                        <th>Vence</th>
                        <th>Productos</th>
                        <th>Status</th>
                        <th>Total</th>
                        <th>Adeudo</th>
                        <th>Acciones</th>
                      </tr>
                    </thead>
                    <tbody>
                      <% @sales.each_with_index do |sale, key| %>
                      <tr class=<%= (sale.cancelled? ? 'danger' : '') %>>
                        <td><%= sale.id %></td>
                        <td><%= sale.sale_code %> </td>
                        <% if current_user.usertype == 'A' %>
                          <td> <%= sale.get_pointsale.name %> </td>
                        <% end %>
                        <td> <%= sale.customer.nick_name %> </td>
                        <td> <%= sale.seller.name %> </td>
                        <td> <%= l(sale.date_sale, :format => '%d/%m/%Y') %> </td>
                        <td> <%= l(sale.expiration_date, :format => '%d/%B/%Y') %> </td>
                        <td> <%= sale.products.count %> </td>
                        <td>
                            <% if sale.reserve_is_expired? && !sale.paid? && !sale.cancelled? %>
                              <span class="label label-warning"> VENCIDO </span>
                            <% elsif sale.cancelled? %>
                              <span class="label label-danger"> CANCELADO </span>
                            <% elsif sale.parcial? %>
                              <span class="label label-success"> VIGENTE </span>
                            <% elsif sale.paid? %>
                              <span class="label label-success"> PAGADO </span>
                            <% end %>
                        </td>
                        <td><%= number_to_currency(sale.total, precision: 2) %></td>
                        <td><%= number_to_currency(sale.reserve_debt, precision: 2) %></td>
                        <td>
                          <!-- detalle del traspaso -->
                          <%= link_to sale, {:class=>"btn btn-icon-only default filtros", :title=>"Ver apartado"} do %>
                            <i class="fa fa-search"></i>
                          <% end %>
                          <!-- regresar productos a inventario cuando vencio el apartado -->
                          <% if sale.reserve_is_expired? && !sale.cancelled_by_expiration? && !sale.paid?&& !sale.cancelled? %>
                            <%= link_to sale_return_expired_path(sale), :class=>"btn btn-icon-only btn-info", :title=>"Regresar producto(s) a inventario", data: { confirm: '¿Esta seguro que desea reingresar los productos a inventario?', method: 'post'}  do %>
                                <i class="fa fa-reply"></i>
                            <% end %>
                          <% end %>
                          <!-- Cancelar apartado -->
                          <% if !sale.reserve_is_expired? && !sale.cancelled? && !sale.paid?%>
                            <%= link_to cancel_reserved_sale_path(sale), :remote => true, :class => "btn btn-icon-only btn-danger", :title=>"Cancelar apartado" do %>
                              <i class="fa fa-ban"></i>
                            <% end %>
                          <% end %>
                          <!-- liquidar traspaso -->
                          <% if !sale.cancelled_by_expiration? && sale.parcial? %>
                            <%= link_to sale_liquidate_reserve_path(sale), :remote => true, :class=>"btn btn-icon-only green-dark", :title=>"Liquidar apartado" do %>
                              <i class="fa fa-dollar"></i>
                            <% end %>
                          <% end %>
                        </td>
                      </tr>
                      <% end %>
                    </tbody>
                  </table>
                </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();
     $('#begin_date').datetimepicker({
      icons: {
        date: "fa fa-calendar"
      },
      format: "DD/MM/YYYY"
    });

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

    var startDate = moment($("#begin_date").data("date"), "DD-MM-YYYY").format('DD/MM/YYYY')
    var endDate = moment($("#end_date").data("date"), "DD-MM-YYYY").format('DD/MM/YYYY');
    $('#title_for_print').val('Ventas del ' + startDate + ' al ' + endDate);
  });

  function salesByDate() {
    var start = moment($("#begin_date").data("date"), "DD-MM-YYYY").format('YYYY-MM-DD HH:mm:ss');
    var end = moment($("#end_date").data("date"), "DD-MM-YYYY").format('YYYY-MM-DD HH:mm:ss');
    App.blockUI({
         target: $("#sales_table"),
         animate: true
    });
    $.ajax({
      type: "get",
      url:  '/find_reserved_sales_by_date/' + start + '/' + end,
      dataType: 'script',
      success: function(data) {
          window.setTimeout(function() {
            App.unblockUI($("#sales_table"));
            var startDate = moment($("#begin_date").data("date"), "DD-MM-YYYY").format('DD/MM/YYYY')
            var endDate = moment($("#end_date").data("date"), "DD-MM-YYYY").format('DD/MM/YYYY');
            $('#title_for_print').val('Apartados del ' + startDate + ' al ' + endDate);
          }, 100);
      }
    });
  }
</script>
