<!-- BEGIN FORM-->
<%= form_for(@product, url: product_update_from_purchase_path(@product), :remote => true, :html => {:class=>"form-horizontal"}) do |f| %>
<div class="portlet-body form">
	<% if @product.errors.any? %>
		<div class="alert alert-danger">
			<strong>Tiene <%= pluralize(@product.errors.count, "error") %> no se puede guardar el producto</strong><br>
		</div>
	<% end %>
	<div class="form-body">
		<%= hidden_field_tag :gain_margin, @pos_config.gain_margin %>
		<%= hidden_field_tag :product_id, @product.id %>
		<%= f.hidden_field :price_base, :value => @product.price_base %>
		<div class="row">
			
			<div class="col-md-12">
				<div class="note note-success">
			        <p> ¿Desea actualizar el precio de venta? el sugerido es en base al margen de ganancia establecido en la configuración.</p>
			        <p>Al omitir, el precio de venta se mantendrá en: <strong><%= number_to_currency(@product.price_sale, precision: 2)%> MXN</strong></p>
			    </div>
				<div class="form-group">
					<%= f.label :price_sale, "Precio de venta base", {:class=>"col-md-3 control-label"} %>
					<div class="col-md-9">
						<div class="col-md-9">
							<div class="input-group">
								<span class="input-group-addon"> $ </span>
								<%= f.text_field :price_sale, :class=>"form-control mask_decimal", :value => @suggested_price_sale %>
								<span class="input-group-addon"> MXN </span>
							</div>	
							<span class="help-block">El precio es sugerido. <br> Precio de compra 
								<strong> 
									<% if @product.is_in_dollars %>
										<%= number_to_currency(@product.price_base_dollars, precision: 2) %> USD
									<% else %>
										<%= number_to_currency(@product.price_base, precision: 2) %> MXN
									<% end %>
								</strong> 
							</span>													
						</div>
					</div>
				</div>
				<% if @product.pointsales_prices.count > 0 %>
				<h4>Precios de venta por punto de venta </h4>
				<div class="note note-warning">
			        <p> Los siguientes puntos de venta cambiaron el precio. Puede modificarlo deacuerdo al precio base.</p>
			        
			    </div>

				<table class="table table-striped table-bordered table-advance table-hover">
	                <thead>
	                    <tr>
	                        <th width="35%">
	                            <i class="fa fa-briefcase"></i> Punto de venta </th>
	                        <th class="hidden-xs">
	                            <i class="fa fa-user"></i> Precio </th>
	                        
	                    </tr>
	                </thead>
	                <tbody>
	                	
		                	<%= f.fields_for :available_products, @product.available_products do |ff| %>
		                    <tr class="<%= ff.object.price_sale.nil? ? 'hidden' : '' %>">
		                        <td> <%= Pointsale.find(ff.object.pointsale_id).name %></td>
		                        <td><%= ff.hidden_field :id %>
		                        	<div class="input-group">
										<span class="input-group-addon"> $ </span>
										<%= ff.text_field :price_sale, :class=>"form-control mask_decimal" %>
										<span class="input-group-addon"> MXN </span>
									</div>
		                        </td>
		                        
		                    </tr>
		                    <% end %>
	                   	
	                </tbody>
	            </table>
	            <% end %>
			</div>
		</div>
	</div>
	<div>
	    <h4 class="form-section"></h4>
		<div class="row">
			<div class="col-md-9">
				<button type="submit" class="btn green" >Actualizar precio</button>
				<button type="button" class="btn default" onclick="cerrarDialog()">Omitir</button>
			</div>
		</div>
	</div>
</div>
<% end %>
<script type="text/javascript">
	function submitForm() {
		var priceSale = $('#product_price_sale').val();
		var productId = $('#product_id').val();
        $.ajax({
            type: "POST",
          	url: "/products/" + productId + "/update_from_purchase",
          	dataType: "json",
          	data: {product: {price_sale: priceSale }},
            success: function(xhr, status, error) {
        		$('#dialog').modal('toggle');
            }
        });  
	}

	function cerrarDialog() {
		$('#dialog').modal('toggle');
	}

</script>
										
