
	<div class="row">

		<div class="col-md-12">
			<p> Producto <strong><%= @product.name %></strong> con precio de venta (base) de <strong>$<%= @product.price_sale %></strong></p>
            <% if @product.pointsales.count > 0 %>
			<table class="table table-striped table-bordered table-advance table-hover">
                <thead>
                    <tr>
                        <th>
                            <i class="fa fa-briefcase"></i> Punto de venta </th>
                        <th class="hidden-xs">
                            <i class="fa fa-money"></i> Precio </th>
                        <th> </th>
                    </tr>
                </thead>
                <tbody>
                	<% @product.pointsales.each do |pv| %>

                    <tr>
                        <td> <%= pv.name %> </td>
                        <td> $ <%= @product.get_price_sale(pv.id) %> </td>
                        <td>
                            <%= link_to available_product_edit_price_path( @product.get_available_in_pointsale(pv.id) ), :remote=> true, :class=>"btn btn-icon-only green-dark", :title=>"Cambiar precio" do %>
								<i class="fa fa-dollar"></i>
							<% end %>
                        </td>
                    </tr>
                    <% end %>
                </tbody>
            </table>
            <% elsif @product.presentation %>
            <div class="well">
                <div class="form-horizontal">
                    <div class="form-group">
                        <label class="col-md-3 control-label">Punto de venta</label>
                        <div class="col-md-9">
                            <%= select_tag :pointsale_id, options_from_collection_for_select(@pointsales, :pointsale_id, :name), :include_blank => "Seleccione punto de venta",  class: "form-control select2" %>

                        </div>
                        <br>
                    </div>
                    <div class="form-group hidden" id="price_sale_div">
                        <label class="col-md-3 control-label">Precio de venta</label>
                        <div class="col-md-5">
                            <div class="input-group">
                                <span class="input-group-addon"> $ </span>
                                <%= number_field_tag "price_sale", "", class: 'form-control mask_decimal' %>
                            </div>
                        </div>
                        <div class="col-md-4" style="padding-left: 0px">
                            <button type="button" class="btn green-dark" onclick="updateVariantPrices()">Aplicar a todo el estilo</button>
                        </div>
                    </div>
                </div>
            </div>
            <table id="variantes" class="table table-striped table-bordered table-advance table-hover hidden">
                <thead>
                    <tr>
                        <th>
                            <i class="fa fa-tag"></i> Producto variante </th>
                        <th class="hidden-xs">
                            <i class="fa fa-money"></i> Precio </th>
                        <th> </th>
                    </tr>
                </thead>
                <tbody>

                </tbody>
            </table>
            <% else %>
            <div class="alert alert-danger">
               <strong>NOTA:</strong> No hay ningún punto de venta manejando este producto. </div>
            <% end %>
		</div>
	</div>

<script type="text/javascript">

    $(document).ready(function(){
        App.init();

    });
    $('#pointsale_id').on('change', function() {
        $('#variantes').find('tbody').empty();
        if($('#pointsale_id').val() != "") {
            $.ajax({
                type: "get",
                url:  '/products/' + <%= @product.id %> +'/variantsprices/' + $(this).val(),
                dataType: 'json',
                success: function(data) {
                    $('#variantes').removeClass("hidden");
                    for (var i = 0; i < data.length; i++) {
                        $('#variantes > tbody:last').append(
                            '<tr><td>'+ data[i].name +'</td><td>$ '+ data[i].price +'</td><td><a class="btn btn-icon-only green-dark" title="Cambiar precio" data-remote="true" href="/available_products/'+ data[i].available_product_id +'/edit_price"><i class="fa fa-dollar"></i></a></td></tr>'
                            );
                    }
                    $('#price_sale_div').removeClass('hidden');
                }
            });
        } else{
            $('#variantes').addClass("hidden");
            $('#price_sale_div').addClass('hidden');
            $('#price_sale').val('');
        }
    });

    function updateVariantPrices() {
        $.ajax({
            type: "POST",
            url: "/update_all_variants_prices",
            dataType: "json",
            data: {
                pointsale_id: $('#pointsale_id').val(),
                new_price: $('#price_sale').val(),
                product_id: <%= @product.id %>
            },
            success: function(xhr, status, error) {
                $('#dialog').modal('toggle');
            }
        });
    }

</script>

