<div class="form-horizontal">
	<div class="col-md-12">
		<div class="note note-success">
			<h4>NOTA:</h4>
	        <p>Aun hay existencia de los siguientes productos, para eliminarlos, es necesario seleccionar un punto de venta ó almacen a donde enviarlos. </p>
	    </div>
		<div class="form-group">
			<%= hidden_field_tag 'ids_to_transfer', @ids_array %>
			<%= label :destiny, "", {:class=>"col-md-2 control-label"} do %>Destino
				<span class="required">*</span>
			<% end %> 		
			<div class="col-md-6 select2-bootstrap-prepend">
				<%= select_tag "destiny", grouped_options_for_select([
				  ['Puntos de venta',  Pointsale.activos.collect {|p| [ p.name, ('P-' + p.id.to_s) ] }],
				  ['Almacenes', Warehouse.activos.collect {|w| [ w.name, ('W-' + w.id.to_s)] }]]),  class: "pointsale_id form-control select2" %>
			</div> 	
		</div>	
	</div>
	
	<table class="table table-striped table-bordered table-hover" id="products">
	    <thead>
	        <tr>
	        	<th>#</th>
	            <th>Producto</th>           
	            <th>Existencia</th>
	        </tr>
	    </thead>
	    <tbody>
	        <% @to_delete_with_stock.each_with_index do |to_delete, key| %>
	        	<tr id="available_<%= to_delete.id %>">
	        		<td> <%= key +1 %> </td>
	        		<td> 
		                <label> 
		                  <strong> <%= to_delete.product.name %> </strong><br>
		                  <%= to_delete.product.display_attributes %>
		                </label> <br>
		                SKU: <%= to_delete.product.sku %><br>
		                <% if to_delete.product.barcode.present? %>
		                  <i class="fa fa-barcode"></i>: <%= to_delete.product.barcode %><br>
		                <% end %>
		                <% if to_delete.product.description.present? %>
		                  Descripción: <%= to_delete.product.description %>
		                <% end %>           		
	    			</td>
	        		<td> <%= to_delete.stock %> </td>
	        	</tr>
	        <% end %>
	    </tbody>
	</table>	
</div>
<div class="form-actions">
	<div class="row">
		<div class="col-md-9">
			<button class="btn green" onclick="transferProducts()">Traspasar productos</button>
			<button class="btn default" onclick="closeModal()">Cancelar</button>
		</div>
	</div>
</div>
<script>
	function closeModal() {
		$('#dialog').modal('toggle');
	}

	function transferProducts() {
		var destiny = $('#destiny').val();
		var ids = []; 
		var all_ids = JSON.parse($('#ids_to_transfer').val());
		if(destiny) {
			$('#products tbody tr').each(function() {
				var idText = $(this).attr('id');
	    		var availableId = idText.substring(idText.lastIndexOf('_') + 1, idText.length);			
		      	ids.push(availableId);  
		    });	

		    $.ajax({
		      type: "POST",
		      url: "/transfer_stock/",
		      dataType: "json",
		      data: {ids_to_transfer: ids, destiny: destiny, all_ids: all_ids},
		      success: function(xhr, status, error) {
		      	$(class_2).DataTable().draw();
		      	$('#dialog').modal('toggle'); 
		      	toastr["success"]("Se desasignaron "  + all_ids.length + " producto(s) del punto de venta.");
		      }
		    });	 
		} else {
			toastr["error"]("Seleccione destino de los productos.");
		}
	}
</script>
