@extends('template.app')

@section('meta')
@endsection

@section('title')
    Update Inventory
@endsection

@section('styles')
@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">Inventories</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('inventory') }}" class="text-muted">Inventories</a></li>
                        <li class="breadcrumb-item text-muted active" aria-current="page">Update</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('inventory.update') }}" name="form" id="form" method="post" enctype="multipart/form-data">
                        @csrf
                        @method('PATCH')

                        <input type="hidden" name="id" value="{{ $data->id }}">
                        <div class="row">
                            <div class="form-group col-sm-4">
                                <label for="branch_id">Branch</label>
                                <select class="form-control" name="branch_id" id="branch_id">
                                    <option value="">Select Branch</option>
                                    @if(isset($branches) && $branches->isNotEmpty())
                                        @foreach($branches as $row)
                                            <option value="{{ $row->id }}" @if(!empty($data) && $data->branch_id == $row->id) selected @endif> {{ $row->name }} </option>
                                        @endforeach
                                    @endif
                                </select>
                                <span class="kt-form__help error branch_id"></span>
                            </div>

                            <?php 
                            $data_car_model = '';
                            if(isset($Product_data->car_model) && $Product_data->car_model){
                                $data_car_model = $Product_data->car_model;
                            }

                            $data_veriant_get_new_id = '';
                            if(isset($Product_data->veriant) && $Product_data->veriant){
                                $data_veriant_get_new_id = $Product_data->veriant;
                            }
                            
                            $data_fuel_id = '';
                            if(isset($Product_data->id) && $Product_data->id){
                                $data_fuel_id = $Product_data->id;
                            }

                             ?>

                            <div class="form-group col-sm-4">
                                <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 }}" @if(!empty($data_car_model) && $data_car_model == $car_model->id) selected @endif> {{ $car_model->name }}</option>
                                    @endforeach
                                @endif
                                </select>
                                       
                                <span class="kt-form__help error car_name"></span>
                            </div>
                            
                             <div class="form-group col-sm-3" id="fule_type">
                                <label for="fule_type">Fuel Type</label>  
                                <select class="form-control" name="fuel_type" id="fuel_type">
                                   <!--<option value="{{ $data->id }}"> {{ $data->title }}</option>-->
                                 @if(isset($fuel_type)) 
                                     @foreach($fuel_type as $fuel)
                                       <!--<option value="{{ $fuel->id }}"> {{ $fuel->title }}</option>-->
                                       <option value="{{ $fuel->id }}" @if(!empty($data) && $data->fuel_type == $fuel->id) selected @endif> {{ $fuel->title }} </option>
                                     @endforeach
                                 @endif
                                 </select>
                                <span class="kt-form__help error fule_type"></span>
                            </div>

                            <div class="form-group col-sm-4">
                                <label for="veriant">Car Variant</label>
                                <select class="form-control variant_class" name="veriant" id="veriant">
                                    @if(isset($Carvarient)) 
                                        @foreach($Carvarient as $model)
                                             <option value="{{ $model->id }}" @if($Product_data->veriant == $model->id) selected @endif> {{ $model->name }} - {{ $model->gst }} %</option>
                                        @endforeach
                                     @endif
                                </select>                                       
                                <span class="kt-form__help error veriant"></span>
                            </div>

                            <input type="hidden" id="selected_car_color" name="selected_car_color" value="{{$data->name ?? ''}}">
                            <div class="form-group col-sm-4">
                                <label for="name">Select Car Color</label>
                                <select class="form-control" name="name" id="name" onchange="car_mveriant(this);">
                                </select>
                                <span class="kt-form__help error branch_id"></span>
                            </div>

                            <div class="form-group col-sm-3">
                                <label for="exterior_color">Exterior Color</label>
                                <input type="text" class="form-control" name="exterior_color" id="exterior_color" value="" readonly>
                                <span class="kt-form__help error exterior_color"></span>
                            </div>

                            <div class="form-group col-sm-4">
                                <label for="interior_color">Interior Color</label>
                                <input type="text" class="form-control" name="interior_color" id="interior_color" value="" readonly>
                                <span class="kt-form__help error interior_color"></span>
                            </div>   
                            
                            <div class="form-group col-sm-4 custom-form-spacing">
                                <label for="kin_invoice_no">KIN Invoice Number</label>
                                <input type="text" name="kin_invoice_no" id="kin_invoice_no" class="form-control" value="{{ $data->kin_invoice_no ?? '' }}">
                                <span class="kt-form__help error kin_invoice_no"></span>
                            </div>

                            <div class="form-group col-md-3">
                                <label class="control-label">KIN Invoice Date</label>
                                <!-- <span class="required" style="color: red;">*</span> -->
                                <input type="date" name="kin_invoice_date" id="kin_invoice_date" class="form-control" value="{{ $data->kin_invoice_date ?? '' }}">
                                <span class="kt-form__help error kin_invoice_date"></span>
                            </div>                   

                            <div class="form-group col-sm-4">
                                <label for="manufacturing_year">Manufacturing Year</label>
                                <select class="form-control" name="manufacturing_year" id="manufacturing_year"> 
                                    <option value="">Select Manufacturing Year</option>
                                    <option value="2024" {{ $data->manufacturing_year == '2024' ? 'selected' : '' }} selected>2024</option>
                                    <option value="2025" {{ $data->manufacturing_year == '2025' ? 'selected' : '' }}>2025</option>
                                    <option value="2026" {{ $data->manufacturing_year == '2026' ? 'selected' : '' }}>2026</option>
                                    <option value="2027" {{ $data->manufacturing_year == '2027' ? 'selected' : '' }}>2027</option>
                                    <option value="2028" {{ $data->manufacturing_year == '2028' ? 'selected' : '' }}>2028</option>
                                </select>
                                <span class="kt-form__help manufacturing_year"></span>
                            </div>
                            <div class="form-group col-sm-4">
                                <label for="key_number">Key number</label>
                                <input type="text" name="key_number" id="key_number" class="form-control" placeholder="Enter key number" value="{{ $data->key_number ?? '' }}">
                                <span class="kt-form__help error key_number"></span>
                            </div>
                            <div class="form-group col-sm-3">
                                <label for="engine_number">Engine number</label>
                                <input type="text" name="engine_number" id="engine_number" class="form-control" placeholder="Enter engine number" value="{{ $data->engine_number ?? '' }}">
                                <span class="kt-form__help error engine_number"></span>
                            </div>
                           <!-- <div class="form-group col-sm-3">
                                <label for="chassis_number">Chassis number</label>
                                <input type="text" name="chassis_number" id="chassis_number" class="form-control" placeholder="Enter chassis number" value="{{ $data->chassis_number ?? '' }}">
                                <span class="kt-form__help error chassis_number"></span>
                            </div>-->
                            <div class="form-group col-sm-4">
                                <label for="vin_number">Vin number</label>
                                <input type="text" name="vin_number" id="vin_number" class="form-control" placeholder="Enter vin number" value="{{ $data->vin_number ?? '' }}">
                                <span class="kt-form__help error vin_number"></span>
                            </div>
                            <!-- <div class="form-group col-sm-3">
                                <label for="basic_price">Basic Price [A]</label>
                                <input type="text" name="basic_price" id="basic_price" class="form-control" placeholder="Basic Price" value="{{ $data->basic_price ?? '' }}">
                                <span class="kt-form__help error basic_price"></span>
                            </div> -->
                            <div class="form-group col-sm-4">
                                <label for="ex_showroom_price">EX Showroom price [Without GST - A]</label>
                                <input type="text" name="ex_showroom_price" id="ex_showroom_price" class="form-control" placeholder="Enter ex showroom price" value="{{ $data->ex_showroom_price ?? '' }}">
                                <span class="kt-form__help error ex_showroom_price"></span>
                            </div>
                            <div class="form-group col-sm-3">
                                <label for="purchase_price">Purchase Price [Without GST -B]</label>
                                <input type="text" name="purchase_price" id="purchase_price" class="form-control" placeholder="Enter Purchase Price" value="{{ $data->purchase_price ?? '' }}">
                                <span class="kt-form__help error vin_number"></span>
                            </div>
                            
                            <div class="form-group col-sm-4">
                                <label for="kin_margin">KIN Margin [C = A - B]</label>
                                <input type="text" name="kin_margin" id="kin_margin" class="form-control" placeholder="Enter kin margin price" value="{{ $data->kin_margin ?? '' }}">
                                <span class="kt-form__help error kin_margin"></span>
                            </div>
                            
                            <div class="form-group col-sm-4">
                                 <label for="car_name">Inventory Type</label>  
                                 <select class="form-control" name="inventory_type" id="inventory_type">
                                    <option value="">Select Inventory Type</option>
                                    <option value="Type A" {{ $data->inventory_type == 'Type A' ? 'selected' : '' }}>Type A</option>
                                    <option value="Type B" {{ $data->inventory_type == 'Type B' ? 'selected' : '' }}>Type B</option>
                                    <option value="Type C" {{ $data->inventory_type == 'Type C' ? 'selected' : '' }}>Type C</option>
                                    <option value="Type D" {{ $data->inventory_type == 'Type D' ? 'selected' : '' }}>Type D</option>
                                    <option value="Demo" {{ $data->inventory_type == 'Demo' ? 'selected' : '' }}>Demo</option>
                                 </select>                                        
                                 <span class="kt-form__help error car_name"></span>
                             </div>

                            <div class="form-group col-sm-3">
                                <label for="kin_margin">Remark</label>
                                <textarea name="remark" id="remark" class="form-control" placeholder="Enter Remark">{{ $data->remark }}</textarea>
                                <span class="kt-form__help error kin_margin"></span>
                            </div>
                            
                            <div class="form-group col-sm-4 custom-form-spacing">
                                <label for="offer_applicable">Offer Applicable ?</label>  
                                <select class="form-control" name="offer_applicable" id="offer_applicable">
                                    <option value="">Select Offer Applicable</option>
                                    <option value="Yes" {{ $data->offer_applicable == 'Yes' ? 'selected' : '' }}>Yes</option>
                                    <option value="No" {{ $data->offer_applicable == 'No' ? 'selected' : '' }}>No</option>
                                </select>                                        
                                <span class="kt-form__help error offer_applicable"></span>
                            </div>
                                <div class="form-group col-sm-4 custom-form-spacing">
                                <label for="fsc_code">FSC CODE</label>
                                <input type="text" name="fsc_code" id="fsc_code" class="form-control" placeholder="Enter Fsc Code">{{ $data->fsc_code }}
                                <span class="kt-form__help error fsc_code"></span><br>
                            </div>
                        </div>
                        <div class="form-group">
                            <button type="submit" class="btn waves-effect waves-light btn-rounded btn-outline-primary">Submit</button>
                            <a href="{{ route('inventory') }}" class="btn waves-effect waves-light btn-rounded btn-outline-secondary">Back</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection


@section('scripts')

<?php
    $data_veriant = '';
    if($data_veriant != ''){
        $data_veriant = $data_veriant;
    }

 ?>

<script>

    $(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);
                        });
                    }
                }
            });
        });
        
        var selectedCarColorId = $("#selected_car_color").val();
        var purchase_price = $('#purchase_price').val();
        $.ajax({
            url : "{{ url('inventory/carvarient/') }}",
            data : {'id' : selectedCarColorId},
            type : 'get',
            dataType : 'json',

            success : function(resp){
                if(resp.data && resp.data.length > 0){
                    // sort by created_at descending
                    resp.data.sort(function(a, b){
                        return new Date(b.created_at) - new Date(a.created_at);
                    });
            
                    var latest = resp.data[0]; // first after sort
                    var html = ihtml = '';
                       
                    jQuery.each(resp.data, function(key, val){ 
                       html = val.ex_showroom_price;
                       ihtml = (val.ex_showroom_price - purchase_price).toFixed(2);
                    });
                    jQuery("#ex_showroom_price").val(html);
                    jQuery('#kin_margin').val(ihtml);
                }    
            }
        });
    });
    
    function car_mveriant(e){
        var id = e.value;
        $.ajax({
            url : "{{ url('inventory/carvarient/') }}",
            data : {'id' : id},
            type : 'get',
            dataType : 'json',

            success : function(resp){
                
               
                // var html = ihtml = ehtml = vhtml = bhtml = '';
                   
                // jQuery.each(resp.data, function(key, val){ 
                //   /* html = val.car_name;
                //     vhtml = val.varient_name;*/
                //     ehtml = val.exterior_color;
                //     ihtml = val.interior_color;
                //     bhtml = val.ex_showroom_price;
                // });
                // jQuery("#exterior_color").val(ehtml);
                // jQuery("#interior_color").val(ihtml);
                // jQuery("#ex_showroom_price").val(bhtml);
                // /*jQuery("#veriant").val(vhtml);
                // jQuery("#car_name").val(html);*/
                
                if(resp.data && resp.data.length > 0){
                    // sort by created_at descending
                    resp.data.sort(function(a, b){
                        return new Date(b.created_at) - new Date(a.created_at);
                    });
            
                    var latest = resp.data[0]; // first after sort
            
                    $("#exterior_color").val(latest.exterior_color);
                    $("#interior_color").val(latest.interior_color);
                    $("#ex_showroom_price").val(latest.ex_showroom_price);
                }
            }
        });
        
    }

       function car_mveriant_edit(value){

       var id = 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 data_veriant = "<?php echo $data->name ?>"; 
       console.log("---");
       console.log(data_veriant);
       console.log("---");
       if(data_veriant != ''){
           car_mveriant_edit(data_veriant);
       }


       //new start

       function car_name_new(car_model,fuel_type, option_check) {
           $.ajax({
               url: "{{ url('inventory/car_varient_asper_fueltype') }}",
               data: {
                   'car_model': car_model,
                   'fuel_type': fuel_type,
                   'option_check': option_check
               },
               type: 'get',
               dataType: 'json',
               success: function(resp) {
                   var e_row = resp.data.Carvarient_arr;
                   // console.log(e_row);
                   var html = '';
                   html += '<option value="">Select Veriant</option>';
                   jQuery.each(e_row, function(key, val) {
                       var data_veriant_get_new_id = "<?php echo $data_veriant_get_new_id; ?>";
                       var selected = '';
                       if (data_veriant_get_new_id == val.id) {
                           selected = 'selected';
                       }
                       html += '<option value="' + val.id + '" ' + selected + '>' + val.name + '</option>';
                   });
                   /* console.log('=========');
                    console.log(html);*/
                   // jQuery("#veriant").empty();
                   // jQuery("#veriant").html(html);
                   jQuery("#veriant").empty();
                   jQuery("#veriant").html(html);
                   // console.log('=========');
               }
           });
       }
       var data_car_model_id = "<?php echo $data_car_model; ?>";
        //alert(data_car_model_id);
       if (data_car_model_id != '') {
          // car_name_new(data_car_model_id, 'Carvarient');
       }
       var data_car_fuel_id = "<?php echo $data_fuel_id; ?>";
        //alert(data_car_fuel_id);
       if (data_car_fuel_id != '') {
         //  car_name_new(data_car_fuel_id, 'Carvarient');
       }
       jQuery(document).on('change', '#fuel_type', function(e) {
           var fuel_type = $('#fuel_type').val();
           var car_model = $('#car_name').val();
           var option_check = 'Carvarient';
           // alert('1');
           car_name_new(car_model,fuel_type, option_check);
       });

       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 = '';
                   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) {
                       var data_product_id = "<?php echo $data->name; ?>";
                       var selected = '';
                       if (data_product_id == val.id) {
                           selected = 'selected';
                       }
                       //+ val.car_name + ' | ' + val.varient_name + ' | '
                       html += '<option value="' + val.id + '" ' + selected + '>'  + val.exterior_color + ' | ' + val.interior_color + '</option>';
                   });
                   // console.log(html);
                   jQuery("#name").empty();
                   jQuery("#name").html(html);
               }
           });
       }
       var data_veriant_id = "<?php echo $data_veriant_get_new_id; ?>";
       if (car_list_new != '') {
           car_list_new(data_veriant_id, 'carname');
       }
       jQuery(document).on('change', '#veriant', function(e) {
           var id = $('#veriant').val();
           var option_check = 'carname';
           // alert('1');
           car_list_new(id, option_check);
       });
       
        jQuery(document).on('keyup', '#purchase_price', function(e){
            var ex_showroom_price = $('#ex_showroom_price').val();
            var purchase_price = $('#purchase_price').val();
            var kin_margin = ex_showroom_price - purchase_price;
            $('#kin_margin').val(kin_margin);
        });
</script>
@endsection