<!-- 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>Compras</h1>
				</div>
			</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 compras</span>
									</div>
									<div class="actions">
										<% if can? :create, Purchase %>
										<%= link_to new_purchase_path, {:class=>"btn bold green pull-right filtros"} do %> Nueva Compra <i class="fa fa-plus"></i>
										<% end %>
										<% end %>
									</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="purchasesByDate()">
				                              <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="purchases_table">
										<thead>
											<tr>
												<th>#</th>
												<th>Folio</th>
												<th>Usuario</th>
												<% if current_user.usertype == "A" %>
													<th>Pto. de venta</th>
													<th>Almacén</th>
												<% end %>
												<th>Proveedor</th>
												<th>Total </th>
												<th>Fecha</th>
												<th>Status</th>
												<% if can? :manage, Purchase %>
													<th>Acciones</th>
												<% end %>
											</tr>
										</thead>
										<tbody>
											<% @purchases.each_with_index do |purchase, key| %>
												<tr <% if purchase.status == "cancelled" %>class="danger"<% end %>>
													<td><%= key + 1 %></td>
													<td><%= purchase.purchase_code %></td>
													<td><%= purchase.user.first_name%></td>
													<% if current_user.usertype == "A" %>
														<td><%= purchase.pointsale.present? ? purchase.pointsale.name : '' %></td>
														<td><%= purchase.warehouse.present? ? purchase.warehouse.name : '' %></td>
													<% end %>
													<td><%= purchase.supplier.nick_name %></td>
													<td><%= number_to_currency(purchase.total, precision: 2) %> <%= purchase.is_in_dollars ? 'USD' : 'MXN' %></td>

													<td><%= l(purchase.purchase_date, :format => '%d/%B/%Y') %>
													</td>
							                        <td class="text-center">
							                          <% case purchase.status %>
								                          <% when "paid"%>
								                            <span class="label label-sm label-success"> <i class="fa fa-check"></i> Pagada </span>
								                          <% when "cancelled"%>
								                             <span class="label label-sm label-danger"> <i class="fa fa-close"></i> Cancelada </span>
								                          <% when "parcial"%>
								                            <span class="label label-sm label-warning"> <i class="fa fa-clock-o"></i> Abonada </span>
								                          <% when "notpaid"%>
								                            <span class="label label-sm label-default"> <i class="fa fa-clock-o"></i> Pte. de pago</span>
							                          <% end %>
							                        </td>
													<td class="text-center">
							                          	<%= link_to purchase, {:class=>"btn btn-icon-only default filtros", :title=>"Ver compra"} do %> <i class="fa fa-search"></i> <% end %>
							                          	<% daysToCancel = @pos_config.days_cancel_purchase %>
							                          	<% if (can? :manage, Purchase) && (purchase.purchase_date + daysToCancel.days >= Date.today) && !purchase.cancelled? %>
															<%= link_to purchase_path(purchase), method: :delete, :class => "btn btn-icon-only btn-danger", :title=>"Cancelar compra", data: { confirm: '¿Está seguro de cancelar la compra?'}   do %> <i class="fa fa-times"></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() {
     $('#begin_date').datetimepicker({
      icons: {
        date: "fa fa-calendar"
      },
      format: "DD/MM/YYYY",
      defaultDate: moment().startOf('month').toDate()
     });

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

    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 purchasesByDate() {
	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: $("#purchases_table"),
         animate: true
    });
    $.ajax({
      type: "get",
      url:  '/find_purchases_by_date/' + start + '/' + end,
      dataType: 'script',
      success: function(data) {
          window.setTimeout(function() {
            App.unblockUI($("#purchases_table"));
          }, 100);
      }
    });
  }
</script>
