@if(!session('business.enable_price_tax')) 
  @php
    $default = 0;
    $class = 'hide';
  @endphp
@else
  @php
    $default = null;
    $class = '';
  @endphp
@endif

<div class="col-sm-12"><br>
    <div class="table-responsive">
    <table class="table table-bordered add-product-price-table table-condensed {{$class}}">
        <tr>
          <th>@lang('product.default_purchase_price')</th>
          <th>@lang('product.profit_percent') @show_tooltip(__('tooltip.profit_percent'))</th>
          <th>@lang('product.default_selling_price') (MRP)</th>
          <th>Discount</th>
          <th>Final Sell Price</th>
          @if(empty($quick_add))
            <th hidden>@lang('lang_v1.product_image')</th>
          @endif
        </tr>
        @foreach($product_deatails->variations as $variation )
            @if($loop->first)
                <tr>
                    <td>
                        <input type="hidden" name="single_variation_id" value="{{$variation->id}}">

                        <div class="col-sm-6">
                          {!! Form::label('single_dpp', trans('product.exc_of_tax') . ':*') !!}

                          {!! Form::text('single_dpp', @num_format($variation->default_purchase_price), ['class' => 'form-control input-sm dpp input_number', 'placeholder' => __('product.exc_of_tax'), 'required']); !!}
                        </div>

                        <div class="col-sm-6">
                          {!! Form::label('single_dpp_inc_tax', trans('product.inc_of_tax') . ':*') !!}
                        
                          {!! Form::text('single_dpp_inc_tax', @num_format($variation->dpp_inc_tax), ['class' => 'form-control input-sm dpp_inc_tax input_number', 'placeholder' => __('product.inc_of_tax'), 'required']); !!}
                        </div>
                    </td>

                    <td>
                        <br/>
                        {!! Form::text('profit_percent', @num_format($variation->profit_percent), ['class' => 'form-control input-sm input_number', 'id' => 'profit_percent', 'required']); !!}
                    </td>

                    <td>
                        <label><span class="dsp_label"></span></label>
                        {!! Form::text('single_dsp', @num_format($variation->default_sell_price), ['class' => 'form-control input-sm dsp input_number', 'placeholder' => __('product.exc_of_tax'), 'id' => 'single_dsp', 'required']); !!}

                        {!! Form::text('single_dsp_inc_tax', @num_format($variation->sell_price_inc_tax), ['class' => 'form-control input-sm hide input_number', 'placeholder' => __('product.inc_of_tax'), 'id' => 'single_dsp_inc_tax', 'required']); !!}
                    </td>
                    
                    
                    
        <td>
            <select class="form-control input-sm" name="weight" id="weight">
                <?php 
                    $s=!empty($product->weight) ? $product->weight : '';
                ?>
                <option <?php if($s=="percentage"){ echo "selected";} ?> value="percentage">Percentage</option>
                <option value="fixed" <?php if($s=="fixed"){ echo "selected";} ?>>Fixed</option>
                
            </select>
          {!! Form::number('product_custom_field1', !empty($product->product_custom_field1) ? $product->product_custom_field1 : null, ['class' => 'form-control input-sm', 'id' => 'product_custom_field1','placeholder' => $product_custom_field1]); !!}
          </td>
          <td>
              <input class="form-control input-sm input_number valid" placeholder="Sell Net Rate" id="sell_net_rate" required="" name="sell_net_rate" type="text" value="0" >
        </td>
                    
                    
                    
                    
                    
                    <td>
                        @php 
                            $action = !empty($action) ? $action : '';
                        @endphp
                        @if($action !== 'duplicate')
                            @foreach($variation->media as $media)
                                <div class="img-thumbnail">
                                    <span class="badge bg-red delete-media" data-href="{{ action('ProductController@deleteMedia', ['media_id' => $media->id])}}"><i class="fas fa-times"></i></span>
                                    {!! $media->thumbnail() !!}
                                </div>
                            @endforeach
                        @endif
                        <div class="form-group" hidden>
                            {!! Form::label('variation_images', __('lang_v1.product_image') . ':') !!}
                            {!! Form::file('variation_images[]', ['class' => 'variation_images', 'accept' => 'image/*', 'multiple']); !!}
                            <small><p class="help-block">@lang('purchase.max_file_size', ['size' => (config('constants.document_size_limit') / 1000000)]) <br> @lang('lang_v1.aspect_ratio_should_be_1_1')</p></small>
                        </div>
                    </td>
                </tr>
            @endif
        @endforeach
    </table>
    </div>
</div>

<script type="text/javascript">
    $(document).ready( function(){
        var single_dsp_inc_tax = __read_number($('input#single_dsp_inc_tax'));
        var disc_type = $('select#weight').val();
        var disc_amount = __read_number($('#product_custom_field1'));

        if(disc_type=="fixed"){
            $('#sell_net_rate').val(single_dsp_inc_tax-disc_amount);
        }else{
            $('#sell_net_rate').val(single_dsp_inc_tax-(single_dsp_inc_tax/100)*disc_amount);
        }

      
$(document).on('change', 'input#single_dpp,input#single_dpp_inc_tax,input#profit_percent,input#single_dsp_inc_tax,input#product_custom_field1,select#weight,select#tax', function(e) {
        console.log('oooo');
        var single_dsp_inc_tax = __read_number($('input#single_dsp_inc_tax'));
        var disc_type = $('select#weight').val();
        var disc_amount = __read_number($('#product_custom_field1'));

        if(disc_type=="fixed"){
            $('#sell_net_rate').val(single_dsp_inc_tax-disc_amount);
        }else{
            $('#sell_net_rate').val(single_dsp_inc_tax-(single_dsp_inc_tax/100)*disc_amount);
        }
  
    });
    
    $(document).on('change', 'input#sell_net_rate', function(e) {
        console.log('oooo111');
        var single_dsp_inc_tax = __read_number($('input#single_dsp_inc_tax'));
        var sell_net_rate = __read_number($('input#sell_net_rate'));
        var disc_type = $('select#weight').val();
      
        if(disc_type=="fixed"){
            $('#product_custom_field1').val(single_dsp_inc_tax-sell_net_rate);
        }else{
            $('#product_custom_field1').val(((single_dsp_inc_tax-sell_net_rate)/single_dsp_inc_tax)*100);
        }
  
    });
    });
</script>