@extends('template.app')

@section('meta')
@endsection

@section('title')
    Update Pricelist
@endsection

@section('styles')
<link href="{{ asset('assets/css/select2.css') }}" rel="stylesheet">
<style>
     .col-sm-3{
        font-size: 15px;
    }
    .form-control{
        height: 32px;
        font-size: 14px;
    }
    .custom-form-spacing {
      margin-bottom: -5px !important;
    }
    .plus{
        margin-top: -67px;
        margin-right: -366px;
    }
</style>
@endsection

@section('content')
<div class="page-breadcrumb">
    <div class="row">
        <div class="col-7 align-self-center">
            <h4 class="page-title text-truncate text-dark font-weight-medium mb-1">Pricelist</h4>
            <div class="d-flex align-items-center">
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb m-0 p-0">
                        <li class="breadcrumb-item"><a href="{{ route('dashboard') }}" class="text-muted">Dashboard</a></li>
                        <li class="breadcrumb-item"><a href="{{ route('pricelist') }}" class="text-muted">Pricelist</a></li>
                        <li class="breadcrumb-item text-muted active" aria-current="page">Add</li>
                    </ol>
                </nav>
            </div>
        </div>
    </div>
</div>
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-body">
                    <form action="{{ route('pricelist.insert') }}" name="form" id="form" method="post" enctype="multipart/form-data">
                        @csrf
                        @method('POST')

                        <input type="hidden" name="rto_mas_indevidual" id="rto_mas_indevidual" value="">
                        <input type="hidden" name="rto_amount" id="rto_amount" value="">
                        <input type="hidden" name="rto_mas_company" id="rto_mas_company" value="">
                        <div class="row">
                            <div class="form-group col-sm-4 custom-form-spacing">
                                <label for="price_list_name">Price List Name</label>
                                <input type="text" name="price_list_name" id="price_list_name" class="form-control" placeholder="Price List Name">
                                <span class="kt-form__help error price_list_name"></span>
                            </div>
                            
                            <div class="form-group col-sm-4 custom-form-spacing">
                                <label for="is_csd">CSD<span class="required" style="color: red;">*</span></label>
                                <select class="form-control" name="is_csd" id="is_csd">
                                    <option value="">Select CSD</option>
                                <option value="Yes">Yes</option>
                                <option value="No">No</option>

                                </select>
                                <span class="kt-form__help error to_branch"></span>
                            </div>     
                             <div class="form-group col-sm-3 custom-form-spacing">
                                <label for="to_branch">RTO Type</label>
                                <select class="form-control" name="rto_type[]" id="rto_type">
                                <option value="regular_rto">Regular Rto</option>
                                <!--<option value="csd">CSD</option>-->
                                <option value="handicap">Handicap</option>
                                <option value="taxi">M-Drive(Taxi)</option>
                                <option value="electric">Electric</option>
                                <option value="crtem">Crtem</option>
                                <option value="ev_six">EV6</option>

                                </select>
                                <span class="kt-form__help error to_branch"></span>
                            </div>    
                           
                        

                           <div class="form-group col-sm-4 custom-form-spacing">
                               <label for="car_name">Car Model</label>
                               <select class="form-control" name="car_name" id="car_name">
                                   <option value="">Select Car Model</option>
                               @if(isset($Carmodel_arr)) 
                                   @foreach($Carmodel_arr as $car_model)
                                     <option value="{{ $car_model->id }}"> {{ $car_model->name }}</option>
                                   @endforeach
                               @endif
                               </select>
                                      
                               <span class="kt-form__help error car_name"></span>
                           </div>

                          
                           <div class="form-group col-sm-4 custom-form-spacing">
                               <label for="fule_type">Fuel Type</label>  

                                <select class="form-control" name="fule_type" id="fule_type">
                                    <option value="">Select Fuel Type</option>
                                     @if(isset($fuel_type)) 
                                                 @foreach($fuel_type as $val)
                                                   <option value="{{ $val->id }}" > {{ $val->title }}</option>
                                                 @endforeach
                                             @endif
                                </select>

                               <span class="kt-form__help error fule_type"></span>
                           </div>
                           
                            <div class="form-group col-sm-3 custom-form-spacing">
                               <label for="veriant">Car Variant</label>  
                               <select class="form-control" name="veriant" id="veriant">
                               </select>                                        
                               <span class="kt-form__help error veriant"></span>
                           </div>
                            
                             <div class="form-group col-sm-4 custom-form-spacing">
                               <label for="veriant">RTO Custom Handling Charges</label>  
                               <input type="number" name="rto_custome_handling_charges" id="rto_custome_handling_charges" class="form-control"   value="0">                                
                               <span class="kt-form__help error veriant"></span>
                           </div>

                           <div class="form-group col-sm-4 custom-form-spacing">
                              <label for="color_tone">Tone Selection <span class="required" style="color: red;">*</span></label>
                              <select name="color_tone" id="color_tone" class="form-control" >
                                  <option value="">Select Tone</option>
                                  @if(isset($tone) && $tone->isNotEmpty())
                                      @foreach($tone AS $s_tone)
                                          <option value="{{ $s_tone->id }}">{{ $s_tone->name }}</option>
                                      @endforeach
                                  @endif
                              </select>
                              <span class="kt-form__help error color_tone"></span>
                          </div>
                          <div class="form-group col-sm-3 custom-form-spacing">
                              <label for="branch_id">Branch Name <span class="required" style="color: red;">*</span></label>
                              <select name="branch_id[]" id="branch_id" class="form-control"multiple>
                                  <option value="">Select Branch Name</option>
                                  @if(isset($Branch_data) && $Branch_data->isNotEmpty())
                                      @foreach($Branch_data AS $row)
                                          <option value="{{ $row->id }}" data-start_value1="{{ $row->start_value1 }}" data-end_value1="{{ $row->end_value1 }}" data-tax_percentage1="{{ $row->tax_percentage1 }}" data-start_value2="{{ $row->start_value2 }}" data-end_value2="{{ $row->end_value2 }}" data-tax_percentage2="{{ $row->tax_percentage2 }}">{{ $row->name }}</option>
                                      @endforeach
                                  @endif
                              </select>
                              <span class="kt-form__help error branch_id"></span>
                          </div>
                           <div class="form-group col-sm-4 custom-form-spacing">
                                <label for="transmission">Transmission</label>
                                <select name="transmission" id="transmission" class="form-control" >
                                  <option value="">Select Transmission</option>
                                  @if(isset($transmission) && $transmission->isNotEmpty())
                                      @foreach($transmission AS $row)
                                          <option value="{{ $row->id }}">{{ $row->name }}</option>
                                      @endforeach
                                  @endif
                              </select>
                                <span class="kt-form__help error transmission"></span>
                            </div>                            

                            <div class="form-group col-sm-4 custom-form-spacing">
                                <label for="basic">BASIC</label>
                                <input type="number" name="basic" id="basic" class="form-control" placeholder="Enter BASIC"  value="0">
                                <span class="kt-form__help error basic"></span>
                            </div>
                            <div class="form-group col-sm-3 custom-form-spacing" id="csd_selling_price_div">
                                <label for="csd_selling_price">CSD Selling Price</label>
                                <input type="number" name="csd_selling_price" id="csd_selling_price" class="form-control" placeholder="Enter CSD Selling Price" value="0">
                                <span class="kt-form__help error csd_selling_price"></span>
                            </div>
                            <div class="form-group col-sm-4 custom-form-spacing" id="ls_order_amount_div">
                                <label for="ls_order_amount">LS Order Amount</label>
                                <input type="number" name="ls_order_amount" id="ls_order_amount" class="form-control" placeholder="Enter LSD Order Amount" value="0">
                                <span class="kt-form__help error ls_order_amount"></span>
                            </div>
                          
                            <!--<div class="form-group col-sm-4 custom-form-spacing" id="outstanding_div">-->
                            <!--    <label for="outstanding_amount">Outstanding Amount</label>-->
                            <!--    <input type="number" name="outstanding_amount" id="outstanding_amount" class="form-control"-->
                            <!--    disabled placeholder="Enter Outstanding Amount" value="0">-->
                            <!--    <span class="kt-form__help error outstanding_amount"></span>-->
                            <!--</div>-->
                            <div class="form-group col-sm-3 custom-form-spacing">
                                <label for="gst_percentage">GST (%)</label>
                                <input type="text" name="gst_percentage" id="gst_percentage" class="form-control" placeholder="Enter GST (%)"  value="0">
                                <span class="kt-form__help error gst_percentage"></span>
                            </div>

                            <div class="form-group col-sm-4 custom-form-spacing">
                                <label for="gst_amount">GST AMOUNT</label>
                                <input type="number" name="gst_amount" id="gst_amount" class="form-control" placeholder="Enter GST AMOUNT"  value="0" readonly>
                                <span class="kt-form__help error gst_amount"></span>
                            </div>

                            <div class="form-group col-sm-4 custom-form-spacing">
                                <label for="cess_percentage">CESS (%)</label>
                                <input type="text" name="cess_percentage" id="cess_percentage" class="form-control" placeholder="Enter CESS (%)"  value="0">
                                <span class="kt-form__help error cess_percentage"></span>
                            </div>

                            <div class="form-group col-sm-3 custom-form-spacing">
                                <label for="cess_amount">CESS AMOUNT</label>
                                <input type="number" name="cess_amount" id="cess_amount" class="form-control" placeholder="Enter CESS AMOUNT"  value="0" readonly>
                                <span class="kt-form__help error cess_amount"></span>
                            </div>

                            <div class="form-group col-sm-4 custom-form-spacing" id="ex_showroom_price_div">
                                <label for="ex_showroom_price">Ex Showroom Price</label>
                                <input type="number" name="ex_showroom_price" id="ex_showroom_price" class="form-control" placeholder="Enter Ex showroom price" value="0">
                                <span class="kt-form__help error ex_showroom_price"></span>
                            </div>

                            <div class="form-group col-sm-4 custom-form-spacing" id="tcs_div">
                                <label for="tcs">TCS</label>
                                <input type="number" name="tcs" id="tcs" class="form-control" placeholder="Enter TCS"  value="0" readonly>
                                <span class="kt-form__help error tcs"></span>
                            </div>
                            
                            <!--<div class="form-group col-sm-3 custom-form-spacing">-->
                            <!--    <label for="statutory_charges">Municipal Tax</label>-->
                            <!--    <input type="number" name="statutory_charges" id="statutory_charges" class="form-control" placeholder="Enter Municipal Tax"  value="0" readonly>-->
                            <!--    <span class="kt-form__help error statutory_charges"></span>-->
                            <!--</div>-->
                            <div class="form-group col-sm-4 custom-form-spacing">
                                <label for="registration_individual_charges">Registration Individual charges</label>
                                <input type="number" name="registration_individual_charges" id="registration_individual_charges" class="form-control" placeholder="Enter Registration Individual charges" readonly>
                                <span class="kt-form__help error registration_individual_charges"></span>
                            </div>
                            <div class="form-group col-sm-4 custom-form-spacing" id="registration_corporate_charges_div">
                                <label for="registration_corporate_charges">Registration corporate charges</label>
                                <input type="number" name="registration_corporate_charges" id="registration_corporate_charges" class="form-control" placeholder="Enter Registration corporate charges" readonly>
                                <span class="kt-form__help error registration_corporate_charges"></span>
                            </div>
                            <div class="form-group col-sm-3 custom-form-spacing">
                                <label for="insurance">Insurance</label>
                                <input type="number" name="insurance" id="insurance" class="form-control" placeholder="Enter Insurance" value="0">
                                <span class="kt-form__help error insurance"></span>
                            </div>
                            <div class="form-group col-sm-4 custom-form-spacing">
                                <label for="fasttag">Fasttag <span class="required" style="color: red;">*</span></label>
                                <select name="fasttag" id="fasttag" class="form-control" >
                                    <option value="">Select Fasttag</option>
                                    @if(isset($fasttag) && $fasttag->isNotEmpty())
                                        @foreach($fasttag AS $row)
                                            <option value="{{ $row->id }}" data-price="{{ $row->amount }}">{{ $row->brand_name ??'' }} - {{ $row->amount }}</option>
                                        @endforeach
                                    @endif
                                </select>
                                <span class="kt-form__help error fasttag"></span>
                            </div>
                            <div class="form-group col-sm-4 custom-form-spacing">
                                <label for="on_road_individual_charges">On Road individual charges</label>
                                <input type="number" name="on_road_individual_charges" id="on_road_individual_charges" class="form-control" placeholder="Enter On Road individual charges" readonly>
                                <span class="kt-form__help error on_road_individual_charges"></span>
                            </div>
                            <div class="form-group col-sm-3 custom-form-spacing" id="on_road_corporate_charges_div">
                                <label for="on_road_corporate_charges">On Road Corporate charges</label>
                                <input type="number" name="on_road_corporate_charges" id="on_road_corporate_charges" class="form-control" placeholder="Enter On Road Corporate charges" readonly>
                                <span class="kt-form__help error on_road_corporate_charges"></span>
                            </div>

                             <div class="form-group col-md-4 custom-form-spacing">
                                <label class="control-label">RSA MASTER</label>
                                <select name="rsa_master" id="rsa_master" class="form-control" >
                                    <option value="">Select RSA</option>
                                    @if(isset($rsa) && $rsa->isNotEmpty())
                                        @foreach($rsa AS $row)
                                            <option value="{{ $row->id }}" data-car-model="{{ $row->car_model }}" data-amount="{{ $row->amount ??'' }}">{{ $row->amount ??'' }} - {{ $row->name }}</option>
                                        @endforeach
                                    @endif
                                </select>
                                <span class="kt-form__help error rsa_master"></span>
                            </div>
                            
                            <div class="form-group col-md-4 custom-form-spacing">
                                <label class="control-label">Extended Warranty</label>
                                <select name="extended_warranty" id="extended_warranty" class="form-control" >
                                </select>
                                <span class="kt-form__help error extended_warranty"></span>
                            </div>
                            <div class="form-group col-md-3 convinience_program_div custom-form-spacing">
                                <label class="control-label">My Convinience Program</label>
                                
                                <select name="convinience_program" id="convinience_program" class="form-control">
                                </select>
                                <span class="kt-form__help error convinience_program"></span>
                            </div>

                            <div class="form-group col-md-4 custom-form-spacing">
                                  <label class="control-label">Other Details 1</label>
                                  <select name="other_details_1[]" id="other_details_1" class="form-control other_details_1" style="width:100%" multiple>
                                   <option value="">Please Select Data</option>
                                      @if(isset($OtherOne) && $OtherOne->isNotEmpty())
                                          @foreach($OtherOne AS $row)
                                              <option value="{{ $row->id }}">{{ $row->name ??'' }}</option>
                                          @endforeach
                                      @endif
                                  </select>
                                  <span class="kt-form__help error"></span>
                            </div>
                            <div class="form-group col-md-4 custom-form-spacing">
                              <label class="control-label">Other Details 2</label>
                              <select name="other_details_2[]" id="other_details_2" class="form-control other_details_2" style="width:100%" multiple>
                               <option value="">Please Select Data</option>
                                  @if(isset($OtherTwo) && $OtherTwo->isNotEmpty())
                                      @foreach($OtherTwo AS $row)
                                          <option value="{{ $row->id }}">{{ $row->name ??'' }}</option>
                                      @endforeach
                                  @endif
                              </select>
                              <span class="kt-form__help error"></span>
                            </div>
                            
                            <div class="form-group col-md-3 custom-form-spacing">
                                <label class="control-label">Final On Road Price Individual</label>
                                <input type="number" name="on_road_price_individual" id="on_road_price_individual" class="form-control" placeholder="Enter On Road Price Individual" readonly>
                                <span class="kt-form__help error on_road_price_individual"></span>
                            </div>

                            <div class="form-group col-md-4 custom-form-spacing" id="on_road_price_corporate_div">
                                <label class="control-label">Final On Road Price Corporate</label>
                                <input type="number" name="on_road_price_corporate" id="on_road_price_corporate" class="form-control" placeholder="Enter On Road Price Corporate" readonly>
                                <span class="kt-form__help error on_road_price_corporate"></span>
                            </div>
                            
                            <div class="form-group col-md-4">
                                <label>Detail</label>
                                <input type="text" name="detail_field[]" id="detail_field_0" class="form-control" placeholder="Enter Detail">
                              </div>  

                          <div class="form-group col-md-3">
                            <label>Value</label>
                            <input type="text" name="value_field[]" id="value_field_0" class="form-control" placeholder="Enter Value"><a class="btn waves-effect waves-light btn-rounded btn-outline-primary pull-right plus" href="javascript:void(0);" onclick="add_more_contact_person();"><i class="fa fa-plus" aria-hidden="true"></i></a>
                            <label>&nbsp;<span class="text-danger"></span></label>
                            
                          </div>  
                          <!--<div class="form-group col-md-1">-->
                            
                          <!--</div>  -->
                          <input type="hidden" name="count" id="count" value="0">
                        </div>

                        <div id="append_div">
                        </div>

                        <div class="row">
                          <!--<div class="form-group col-md-4">-->
                          <!--  <label>Detail</label>-->
                          <!--  <input type="text" name="detail_field[]" id="detail_field_0" class="form-control" placeholder="Enter Detail">-->
                          <!--</div>  -->

                          <!--<div class="form-group col-md-4">-->
                          <!--  <label>Value</label>-->
                          <!--  <input type="text" name="value_field[]" id="value_field_0" class="form-control" placeholder="Enter Value">-->
                          <!--</div>  -->
                          
                          
                          
                        </div>
 
                                        <input type="hidden" name="rto_mas_company" id="rto_mas_company" value="">
                                        <input type="hidden" name="rto_amount" id="rto_amount" value="">
                                         <input type="hidden" name="rto_amount_indevidual" id="rto_amount_indevidual" value="">
                                        <input type="hidden" name="rto_mas_indevidual" id="rto_mas_indevidual" value="">
                                        
                        <div class="form-group">
                            <button type="submit" class="btn waves-effect waves-light btn-sm btn-rounded btn-outline-primary">Submit</button>
                            <a href="{{ route('pricelist') }}" class="btn waves-effect waves-light btn-sm btn-rounded btn-outline-secondary">Back</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@section('scripts')

<script src="{{ asset('assets/js/select2.js') }}"></script>

<script>
  window.tex_master_gst = 0;

      //select2
      $(document).ready(function(){
        $('.other_details_1').select2({
            placeholder:"Select Other Details 1",
        })
        $('.other_details_2').select2({
            placeholder:"Select Other Details 2",
        })
    }); 
    $(document).ready(function(){
        $('#is_csd').on('change',function(){
            console.log($('#is_csd').val());
            if($('#is_csd').val() == 'Yes'){
                $('#ex_showroom_price_div').hide();
                $('#tcs_div').hide();
                $('#registration_corporate_charges_div').hide();
                $('#on_road_corporate_charges_div').hide();
                $('#on_road_price_corporate_div').hide();
                $('#basic').attr('disabled',false);
                $("#csd_selling_price_div").show();
                $("#ls_order_amount_div").show();
                $("#outstanding_div").show();
            } else {
              $('#ex_showroom_price_div').show();
                $('#tcs_div').show();
                $("#csd_selling_price_div").hide();
                $('#registration_corporate_charges_div').show();
                $('#on_road_corporate_charges_div').show();
                $('#on_road_price_corporate_div').show();
                $('#basic').attr('disabled',false);
                $("#ls_order_amount_div").hide();
                $("#outstanding_div").hide();
            }
        });
        
        $('#model').on('change' ,function(){
            var is_applicable_for_mcp = $('#model :selected').data("is_applicable_for_mcp");
            if(is_applicable_for_mcp == 'yes'){
                $('.convinience_program_div').show();
            }else{
                $('.convinience_program_div').hide();
            }
        });
        
        $('#car_name').change(function() {
            var selectedCarModel = $(this).val();
    
            $('#rsa_master option').each(function() {
                var rsaCarModel = $(this).data('car-model');
                
                // Show all options if no car model is selected
                if (selectedCarModel === "" || rsaCarModel == selectedCarModel) {
                    $(this).show();
                } else {
                    $(this).hide();
                }
            });
    
            // Reset RSA Master dropdown when car model changes
            $('#rsa_master').val('');
        });
        
        $('#convinience_program').on('change' ,function(){
            var convinience_program = $('#convinience_program :selected').val();
            if(convinience_program == 'mcp_plus'){
                $('#extended_warranty').attr('disabled',true);
                $("#extended_warranty").val([]);
            }else{
                $('#extended_warranty').removeAttr('disabled',true);
            }
        });
      });

    $(document).ready(function() {
        var form = $('#form');
        $('.kt-form__help').html('');
        form.submit(function(e) {
            $('.help-block').html('');
            $('.m-form__help').html('');
            $.ajax({
                url: form.attr('action'),
                type: form.attr('method'),
                data: new FormData($(this)[0]),
                dataType: 'json',
                async: false,
                processData: false,
                contentType: false,
                success: function(json) {
                    return true;
                },
                error: function(json) {
                    if (json.status === 422) {
                        e.preventDefault();
                        var errors_ = json.responseJSON;
                        $('.kt-form__help').html('');
                        $.each(errors_.errors, function(key, value) {
                            $('.' + key).html(value);
                        });
                    }
                }
            });
        });
    });

     
    $("#ex_showroom_price,#tcs,#statutory_charges,#registration_individual_charges,#insurance,#fasttag,#on_road_individual_charges,#branch_id,#on_road_corporate_charges,#veriant,#extended_warranty,#convinience_program,#basic,#gst_percentage,#cess_percentage,#registration_corporate_charges,#on_road_price_individual,#on_road_price_corporate,#ls_order_amount,#csd_selling_price,#rsa_master").change(function(){

        if($('#is_csd').val() == 'No'){
            
            var basic = parseInt($("#basic").val());
            if(isNaN(basic)) {
                basic = 0;
            }
            
            var gst_percentage = parseInt($("#gst_percentage").val());
            if(isNaN(gst_percentage)) {
                gst_percentage = 1;
            }
            
            var cess_percentage = parseInt($("#cess_percentage").val());
            if(isNaN(cess_percentage)) {
                cess_percentage = 1;
            }
            
            var gst_amount= ((basic * gst_percentage) / 100);    
            var cess_amount= ((basic * cess_percentage) / 100);   
            var ex_showroom_price = basic + gst_amount + cess_amount;
            var lsd_amount = 0;
            
            if($('#is_csd').val() == 'Yes'){
                lsd_amount = basic + gst_amount + cess_amount;
                if(isNaN(lsd_amount)) {
                    lsd_amount = 0;
                }
                $("#ls_order_amount").val(Math.round(lsd_amount));
                lsd_amount = Math.round(lsd_amount);
            }

            if(isNaN(ex_showroom_price)) {
                ex_showroom_price = 0;
            }
            
            $("#ex_showroom_price").val(Math.round(ex_showroom_price));
    
            ex_showroom_price = Math.round(ex_showroom_price);

            // tcs 1%
            if(ex_showroom_price > 1000000){
                var tcs= ((ex_showroom_price * 1) / 100);    
                $("#tcs").val(Math.round(tcs));
            }else{
                $("#tcs").val(0);
            }

            var tcs = parseInt($("#tcs").val());
            if(isNaN(tcs)) {
                tcs = 0;
            }

            $("#gst_amount").val(Math.round(gst_amount));
            $("#cess_amount").val(Math.round(cess_amount));



            if(ex_showroom_price > 1000000){
                var tcs= ((ex_showroom_price * 1) / 100);    
                $("#tcs").val(Math.round(tcs));
            }else{
                $("#tcs").val(0);
            }
    
            var tcs = parseInt($("#tcs").val());
            if(isNaN(tcs)) {
                tcs = 0;
            }
    
            // var branch_persentage = parseInt($('option:selected', '#branch_id').attr('data-branchper'));        
            var start_value1 = parseInt($('option:selected', '#branch_id').attr('data-start_value1'));
            var end_value1 = parseInt($('option:selected', '#branch_id').attr('data-end_value1'));
            var tax_percentage1 = parseFloat($('option:selected', '#branch_id').attr('data-tax_percentage1'));
            var start_value2 = parseInt($('option:selected', '#branch_id').attr('data-start_value2'));
            var end_value2 = parseInt($('option:selected', '#branch_id').attr('data-end_value2'));
            var tax_percentage2 = parseFloat($('option:selected', '#branch_id').attr('data-tax_percentage2'));
    
            
            var branch_persentage = 0; 
            if(ex_showroom_price >= start_value1 && ex_showroom_price <= end_value1){
                branch_persentage = tax_percentage1; 
            }else if(ex_showroom_price >= start_value1 && ex_showroom_price <= end_value2){
                branch_persentage = tax_percentage2; 
            }   

            var statutory_charges = ((basic + gst_amount)/100)*branch_persentage;

            if(isNaN(statutory_charges)) {
                statutory_charges = 0;
            }

            $('#statutory_charges').val(Math.round(statutory_charges));
        
            var customer_type = $("#rto_type").val();
            var rtoURL = '{{route('pricelist.getpricelist')}}'
            $.ajax({
                url: rtoURL,
                type: 'post',
                data: {customer_type:customer_type,_token: "{{ csrf_token() }}"},
                cache: false,
                success: function(data) {
                   $('#rto_mas_indevidual').val(data.rto_tax_individual);
                    $('#rto_mas_company').val(data.rto_tax_company);
                    $('#rto_amount').val(data.totalcharges);
                    $('#rto_amount_indevidual').val(data.totali_individual_charges);
                   
                }
            });
         
            var rto_amount2 = $('#rto_amount').val();
            var rto_amount_indevidual2 = $('#rto_amount_indevidual').val();
            var rto_mas_indevidual2 = $('#rto_mas_indevidual').val();
            var rto_mas_company2 = $('#rto_mas_company').val();
            var customer_type_new = $('#customer_type_new').val();
            
            registration_individual_charges_2 = ((basic)/100*rto_mas_indevidual2)+parseInt(rto_amount2);
            if(isNaN(registration_individual_charges_2)) {
                registration_individual_charges_2 = 0;
            }
            $('#registration_individual_charges').val(Math.round(registration_individual_charges_2));
        
            registration_corporate_charges = ((basic)/100*rto_mas_company2)+parseInt(rto_amount2);
            if(isNaN(registration_corporate_charges)) {
                registration_corporate_charges = 0;
            }
            $('#registration_corporate_charges').val(Math.round(registration_corporate_charges));
        
            var statutory_charges = parseInt($("#statutory_charges").val());
            if(isNaN(statutory_charges)) {
                statutory_charges = 0;
            }
    
            var registration_individual_charges=parseInt($("#registration_individual_charges").val());
            if(isNaN(registration_individual_charges)) {
                registration_individual_charges = 0;
            }
    
            var registration_corporate_charges_1_new=parseInt($("#registration_corporate_charges").val());
            if(isNaN(registration_corporate_charges_1_new)) {
                registration_corporate_charges_1_new = 0;
            }
    
            var insurance=parseInt($("#insurance").val());
            if(isNaN(insurance)) {
                insurance = 0;
            }
    
            var fasttag = parseInt($('option:selected', '#fasttag').attr('data-price'));
            if(isNaN(fasttag)) {
                fasttag = 0;
            }
           
            var on_road_individual_charges_1=ex_showroom_price+tcs+statutory_charges+registration_individual_charges+insurance+fasttag;
    
            if(isNaN(on_road_individual_charges_1)) {
                on_road_individual_charges_1 = 0;
            }
    
            on_road_individual_charges_1 = Math.round(on_road_individual_charges_1);
            $('#on_road_individual_charges').val(Math.round(on_road_individual_charges_1));    
    
            var extended_warranty = parseInt($('option:selected', '#extended_warranty').attr('data-amount'));
            if (isNaN(extended_warranty)) {
                extended_warranty = 0;
            }
            
            var rsa_master = parseInt($('option:selected', '#rsa_master').attr('data-amount'));
            if (isNaN(rsa_master)) {
                rsa_master = 0;
            }
            
            var convinience_program_amount = parseInt($('option:selected', '#convinience_program').attr('data-convinience_amount'));
            if (isNaN(convinience_program_amount)) {
               convinience_program_amount = 0;
            }
            
            var on_road_price_individual = parseInt(on_road_individual_charges_1) + extended_warranty + convinience_program_amount + rsa_master;
            if (isNaN(on_road_price_individual)) {
               on_road_price_individual = 0;
            }
            
            $('#on_road_price_individual').val(parseInt(on_road_price_individual));           
            if($('#is_csd').val() == 'Yes'){
                tcs = 0;
            }

            var on_road_corporate_charges_1 = ex_showroom_price + tcs + statutory_charges+ registration_corporate_charges_1_new +insurance + fasttag;
            if(isNaN(on_road_corporate_charges_1)) {
                on_road_corporate_charges_1 = 0;
            }
            on_road_corporate_charges_1 = Math.round(on_road_corporate_charges_1);
            $('#on_road_corporate_charges').val(Math.round(on_road_corporate_charges_1));

            var on_road_price_corporate = on_road_corporate_charges_1 + extended_warranty + convinience_program_amount;
            if (isNaN(on_road_price_corporate)) {
                on_road_price_corporate = 0;
            }
            $('#on_road_price_corporate').val(parseInt(on_road_price_corporate));   
        } else if ($('#is_csd').val() == 'Yes') {
            
            var basic = parseInt($("#basic").val());
            if(isNaN(basic)) {
                basic = 0;
            }
                
            var csd_selling_amount = parseInt($("#csd_selling_price").val());
            if(isNaN(csd_selling_amount)) {
                csd_selling_amount = 0;
            }
            
            var lsd_amount = parseInt($("#ls_order_amount").val());
            if(isNaN(lsd_amount)) {
                lsd_amount = 0;
            }
                
            var gst_percentage = parseInt($("#gst_percentage").val());
            if(isNaN(gst_percentage)) {
                gst_percentage = 1;
            }
            
            var cess_percentage = parseInt($("#cess_percentage").val());
            if(isNaN(cess_percentage)) {
                cess_percentage = 1;
            }
            var gst_amount= ((basic * gst_percentage) / 100);    
            var cess_amount= ((basic * cess_percentage) / 100);   
        
            var ex_showroom_price = 0;
            $("#csd_selling_price").val(Math.round(csd_selling_amount));
            csd_selling_amount = Math.round(csd_selling_amount);
                
            $("#ls_order_amount").val(Math.round(lsd_amount));
            lsd_amount = Math.round(lsd_amount);
        
            // tcs 1%
            var tcs =0;
            $("#gst_amount").val(Math.round(gst_amount));
            $("#cess_amount").val(Math.round(cess_amount));
        
            // var branch_persentage = parseInt($('option:selected', '#branch_id').attr('data-branchper'));        
            var start_value1 = parseInt($('option:selected', '#branch_id').attr('data-start_value1'));
            var end_value1 = parseInt($('option:selected', '#branch_id').attr('data-end_value1'));
            var tax_percentage1 = parseFloat($('option:selected', '#branch_id').attr('data-tax_percentage1'));
            var start_value2 = parseInt($('option:selected', '#branch_id').attr('data-start_value2'));
            var end_value2 = parseInt($('option:selected', '#branch_id').attr('data-end_value2'));
            var tax_percentage2 = parseFloat($('option:selected', '#branch_id').attr('data-tax_percentage2'));
        
            var branch_persentage = 0; 
            if(basic >= start_value1 && basic <= end_value1){
                branch_persentage = tax_percentage1; 
            }else if(basic >= start_value1 && basic <= end_value2){
                branch_persentage = tax_percentage2; 
            }   
        
            var statutory_charges = ((basic + gst_amount)/100)*branch_persentage;
            if(isNaN(statutory_charges)) {
                statutory_charges = 0;
            }

            $('#statutory_charges').val(Math.round(statutory_charges));
            
            var customer_type = $("#rto_type").val();
            var rtoURL = '{{route('pricelist.getpricelist')}}'
            
            $.ajax({
                url: rtoURL,
                type: 'post',
                data: {customer_type:customer_type,_token: "{{ csrf_token() }}"},
                cache: false,
                success: function(data) {
                   $('#rto_mas_indevidual').val(data.rto_tax_individual);
                    $('#rto_mas_company').val(data.rto_tax_company);
                    $('#rto_amount').val(data.totalcharges);
                    $('#rto_amount_indevidual').val(data.totali_individual_charges);
                   console.log(data);
                }
            });
            var rto_amount2 = $('#rto_amount').val();
            var rto_amount_indevidual2 = $('#rto_amount_indevidual').val();
            var rto_mas_indevidual2 = $('#rto_mas_indevidual').val();
            var rto_mas_company2 = $('#rto_mas_company').val();
            var customer_type_new = $('#customer_type_new').val();

            // registration_individual_charges_2 = (basic*rto_mas_indevidual2);
            registration_individual_charges_2 = (basic * (rto_mas_indevidual2 / 100))+parseInt(rto_amount2);
            if(isNaN(registration_individual_charges_2)) {
                registration_individual_charges_2 = 0;
            }
            $('#registration_individual_charges').val(Math.round(registration_individual_charges_2));
            registration_corporate_charges = 0;
            if(isNaN(registration_corporate_charges)) {
                registration_corporate_charges = 0;
            }
            $('#registration_corporate_charges').val(Math.round(registration_corporate_charges));
               
            var statutory_charges = parseInt($("#statutory_charges").val());
            if(isNaN(statutory_charges)) {
                statutory_charges = 0;
            }
    
            var registration_individual_charges=parseInt($("#registration_individual_charges").val());
            if(isNaN(registration_individual_charges)) {
                registration_individual_charges = 0;
            }
    
            var registration_corporate_charges_1_new=0;
    
            var insurance=parseInt($("#insurance").val());
            if(isNaN(insurance)) {
                insurance = 0;
            }
    
            var fasttag = parseInt($('option:selected', '#fasttag').attr('data-price'));
            if(isNaN(fasttag)) {
                fasttag = 0;
            }
           
            var on_road_individual_charges_1=basic+gst_amount+cess_amount+registration_individual_charges+insurance+fasttag;
    
            if(isNaN(on_road_individual_charges_1)) {
                on_road_individual_charges_1 = 0;
            }
    
            on_road_individual_charges_1 = Math.round(on_road_individual_charges_1);
            $('#on_road_individual_charges').val(Math.round(on_road_individual_charges_1));    
    
            var extended_warranty = parseInt($('option:selected', '#extended_warranty').attr('data-amount'));
            if (isNaN(extended_warranty)) {
                extended_warranty = 0;
            }
            
            var convinience_program_amount = parseInt($('option:selected', '#convinience_program').attr('data-convinience_amount'));
            if (isNaN(convinience_program_amount)) {
               convinience_program_amount = 0;
            }
            
            var rsa_master = parseInt($('option:selected', '#rsa_master').attr('data-amount'));
            if (isNaN(rsa_master)) {
                rsa_master = 0;
            }
            
            var on_road_price_individual = parseInt(on_road_individual_charges_1) + extended_warranty + convinience_program_amount + rsa_master;
            if (isNaN(on_road_price_individual)) {
               on_road_price_individual = 0;
            }
            
            $('#on_road_price_individual').val(parseInt(on_road_price_individual));           
            if($('#is_csd').val() == 'Yes'){
                tcs = 0;
            }
    
    
            var on_road_corporate_charges_1 = 0
            if(isNaN(on_road_corporate_charges_1)) {
                on_road_corporate_charges_1 = 0;
            }
            on_road_corporate_charges_1 = Math.round(on_road_corporate_charges_1);
            $('#on_road_corporate_charges').val(Math.round(on_road_corporate_charges_1));
        
            var on_road_price_corporate = 0;
            if (isNaN(on_road_price_corporate)) {
                on_road_price_corporate = 0;
            }
            
            $('#on_road_price_corporate').val(parseInt(on_road_price_corporate));   
             var outstanding_price =   parseInt($('#on_road_price_individual').val()) - parseInt($('#ls_order_amount').val());
            $('#outstanding_amount').val(Math.round(outstanding_price));
        }
    })
    
    

    function car_mveriant(e){
       var id = e.value;
         $.ajax({
             url : "{{ url('obf/carvarient/') }}",
             data : {'id' : id},
             type : 'get',
             dataType : 'json',

             success : function(resp){
                 
                
                 var html = ihtml = ehtml = vhtml = '';
                     
                 jQuery.each(resp.cardeails, function(key, val){ 
                   /*  html = val.car_name;
                     vhtml = val.varient_name;*/
                     ehtml = val.exterior_color;
                     ihtml = val.interior_color;
                 });
                 jQuery("#exterior_color").val(ehtml);
                 jQuery("#interior_color").val(ihtml);
                 /*jQuery("#veriant").val(vhtml);
                 jQuery("#car_name").val(html);*/

                 var e_row = resp.data.pricelist_arr;

                 // console.log(e_row);

                 html = '';
                 html +='<option value="">Select Pricelist</option>';
                 jQuery.each(e_row, function(key, val){ 
                     $('#pricelist_manager_id').removeAttr('disabled');
                     html += '<option value="'+val.id+'">'+val.price_list_name+' | '+val.transmission+'</option>';
                 });
                 // console.log(html);
                 jQuery("#pricelist_manager_id").empty();
                 jQuery("#pricelist_manager_id").html(html);

             }
         });
         
     }

    
    function car_name_new(id,option_check,new_mcp,fuel){
          $.ajax({
              url : "{{ url('obf/car_drop_down_get/') }}",
              data : {'id' : id,'option_check': option_check,'new_mcp':new_mcp,'fuel':fuel},
              type : 'get',
              dataType : 'json',

              success : function(resp){  
                  var html = '';
                  var e_row = resp.data.Carvarient_arr;
                  html = '';
                  html +='<option value="">Select Veriant</option>';
                  jQuery.each(e_row, function(key, val){ 
                      html += '<option value="'+val.id+'">'+val.name+'</option>';
                  });
                  jQuery("#veriant").empty();
                  jQuery("#veriant").html(html);
                  
                  //alert(html);

                  /*var e_row = resp.data.ExtandWarranty_arr;
                  var html2 = '';
                  html2 = '';
                  html2 +='<option value="">Select Extanded Warranty</option>';
                  jQuery.each(e_row, function(key, val){ 
                      html2 += '<option value="'+val.id+'" data-amount="'+val.amount+'">'+val.years+' - '+val.amount+'</option>';                      
                  });
                  console.log(html2);
                  jQuery("#extended_warranty").empty();
                  jQuery("#extended_warranty").html(html2);*/

                  var html3 = '';
                  var e_row = resp.data.Mcp_arr_new;
                  html3 = '';
                  html3 +='<option value="">Select convinience program</option>';
                  jQuery.each(e_row, function(key, val){ 
                      html3 += '<option value="'+val.id+'" data-convinience_amount="'+val.amount+'">'+val.name+' - '+val.amount+'</option>';
                  });
                    
                  jQuery("#convinience_program").empty();
                  jQuery("#convinience_program").html(html3);
              }
          });
    }

    function car_fuel_type(id,model_id){
        $.ajax({
            url : "{{ url('pricelist/car_fuel_type/') }}",
           data : {'id' : id,model_id:model_id},
            type : 'get',
            dataType : 'json',
            success : function(resp){
                var fule_html = '<option value="" disabled>Select Fuel Type</option>';
                $.each(resp.data, function (id, value){
                    var id = value.id;
                    var title = value.title;

                    fule_html += '<option value="'+id+'"';

                    if(resp.id.fule_type == id){
                        fule_html += 'selected';
                    }
                    
                    fule_html +='>'+title+'</option>';
                });
                $("#fule_type").html(fule_html);

                var e_row = resp.ExtandWarranty_arr;

                if(e_row != null && e_row.length > 0){
                  var html2 = '';
                  html2 = '';
                  html2 +='<option value="">Select Extanded Warranty</option>';
                  jQuery.each(e_row, function(key, val){

                      html2 += '<option value="'+val.id+'" data-amount="'+val.amount+'" >'+val.years+' - '+val.amount+'</option>';                      
                  });
                  console.log(html2);
                  jQuery("#extended_warranty").empty();
                  jQuery("#extended_warranty").html(html2);
                }


            }
        });
    }
       

      jQuery(document).on('change', '#fule_type', function(e){
          var id = $('#car_name').val();
          var option_check = 'Carvarient';
          var new_mcp = 'Mcp';
          // alert('1');
          var fuel = $('#fule_type').val();
         car_name_new(id,option_check,new_mcp,fuel);
      });

    jQuery(document).on('change', '#veriant', function(e){
        var id = $('#veriant').val();
        var model_id = $('#car_name').val();
        car_fuel_type(id,model_id);
    });

      function car_list_new(id,option_check){
          $.ajax({
              url : "{{ url('obf/car_drop_down_get/') }}",
              data : {'id' : id,'option_check': option_check},
              type : 'get',
              dataType : 'json',

              success : function(resp){  
                  var html = '';
                  // console.log(resp.data.tex_master_gst);

                  window.tex_master_gst = parseInt(resp.data.tex_master_gst);

                  /*var e_row = resp.data.carname_arr;
                   console.log(e_row);
                  html = '';
                  html +='<option value="">Select Car</option>';
                  jQuery.each(e_row, function(key, val){ 
                      html += '<option value="'+val.id+'">'+val.exterior_color+' | '+val.interior_color+'</option>';
                  });
                   console.log(html);
                  jQuery("#product_id").empty();
                  jQuery("#product_id").html(html);*/
              }
          });
      }
      
       function getRTOTypeValues(id){
          $.ajax({
              url : "{{ url('rto_master/getdetails/') }}",
              data : {'id' : id},
              type : 'get',
              dataType : 'json',

              success : function(resp){  
                  console.log(resp);
              }
          });
      }
    
        // jQuery(document).on('change', '#rto_type', function(e){
        //   var id = $('#rto_type').val();
        //   alert(id);
        //   getRTOTypeValues(id);
          
        // });
      jQuery(document).on('change', '#veriant', function(e){
          var id = $('#veriant').val();
          var option_check = 'carname';
          // alert('1');
          car_list_new(id,option_check);
          
      });



    var i = $('#count').val();
    function add_more_contact_person(){
        i++;
        $('#count').val(i);
        var html = '';

        html +='<div class="row">';
        html +='<div class="form-group col-md-4">';
        html +='<label>Detail</label>';
        html +='<input type="text" name="detail_field[]" id="detail_field_'+i+'" class="form-control" placeholder="Enter Detail">';
        html +='</div>';
        html +='<div class="form-group col-md-4">';
        html +='<label>Value</label>';
        html +='<input type="text" name="value_field[]" id="value_field_'+i+'" class="form-control" placeholder="Enter Value">';
        html +='</div>';
        html +='<div class="form-group col-md-1">';
        html +='<label>&nbsp;<span class="text-danger"></span></label><br>';
        html +='<a class="btn waves-effect waves-light btn-rounded btn-outline-danger pull-right" href="javascript:void(0);" onclick="remove_contact_person(this);"><i class="fa-solid fa-trash-can"></i></a>';
        html +='</div>';
        html +='<input type="hidden" name="count" id="count" value="0">';
        html +='</div>';

        $("#append_div").append(html);
    }

    function remove_contact_person(that){
        $(that).parent().parent().remove();
    }

    
    
</script>

@endsection