@extends('template.app')

@section('meta')
@endsection

@section('title')
    Create Transit Inventory
@endsection

@section('styles')
<style>
    .col-sm-3{
        font-size: 15px;
    }
    .form-control{
        height: 32px;
        font-size: 14px;
    }
    .custom-form-spacing {
      margin-bottom: -5px !important;
    }
</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">Transit 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('transit-inventory') }}" class="text-muted">Transit Inventories</a></li>
                        <li class="breadcrumb-item text-muted active" aria-current="page">Create</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('transit-inventory.insert') }}" name="form" id="form" method="post" enctype="multipart/form-data">
                        @csrf
                        @method('POST')

                        <div class="row">
                            <div class="form-group col-sm-4 custom-form-spacing">
                                <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 }}"> {{ $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="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-3 custom-form-spacing" id="fule_type">
                                <label for="fule_type">Fuel Type</label>  
                                <select class="form-control" name="fuel_type" id="fuel_type">
                                     <option value="">Select Fuel Type</option>
                                 @if(isset($fuel_type)) 
                                     @foreach($fuel_type as $fuel)
                                       <option value="{{ $fuel->id }}"> {{ $fuel->title }}</option>
                                     @endforeach
                                 @endif
                                 </select>
                                <span class="kt-form__help error fule_type"></span>
                            </div>
                            
                             <div class="form-group col-sm-4 custom-form-spacing">
                                 <label for="car_name">Car Variant</label>  
                                 <select class="form-control" name="veriant" id="veriant">
                                 </select>                                        
                                 <span class="kt-form__help error car_name"></span>
                             </div>

                            

                             <div class="form-group col-sm-4 custom-form-spacing">
                                 <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 custom-form-spacing">
                                <label for="exterior_color">Exterior Color</label>
                                <input type="text" class="form-control" name="exterior_color" id="exterior_color" readonly>
                                <span class="kt-form__help error exterior_color"></span>
                            </div>
                            
                            <div class="form-group col-sm-4 custom-form-spacing">
                                <label for="interior_color">Interior Color</label>
                                <input type="text" class="form-control" name="interior_color" id="interior_color" 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" placeholder="Enter KIN invoice number">
                                <span class="kt-form__help error kin_invoice_no"></span>
                            </div>
                            <div class="form-group col-md-3 custom-form-spacing">
                                <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">
                                <span class="kt-form__help error kin_invoice_date"></span>
                            </div>
                            <div class="form-group col-sm-4 custom-form-spacing">
                                <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" selected>2024</option>
                                    <option value="2025">2025</option>
                                    <option value="2026">2026</option>
                                    <option value="2027">2027</option>
                                    <option value="2028">2028</option>
                                </select>
                                <span class="kt-form__help manufacturing_year"></span>
                            </div>
                            <div class="form-group col-sm-4 custom-form-spacing">
                                <label for="key_number">Key number</label>
                                <input type="text" name="key_number" id="key_number" class="form-control" placeholder="Enter key number">
                                <span class="kt-form__help error key_number"></span>
                            </div>
                            <div class="form-group col-sm-3 custom-form-spacing">
                                <label for="engine_number">Engine number</label>
                                <input type="text" name="engine_number" id="engine_number" class="form-control" placeholder="Enter 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">
                                <span class="kt-form__help error chassis_number"></span>
                            </div>-->
                            <div class="form-group col-sm-4 custom-form-spacing">
                                <label for="vin_number">Vin number</label>
                                <input type="text" name="vin_number" id="vin_number" class="form-control" placeholder="Enter vin number">
                                <span class="kt-form__help error vin_number"></span>
                                @if ($errors->has('vin_number'))
                                    <div class="alert alert-danger">
                                        {{ $errors->first('vin_number') }}
                                    </div>
                                @endif
                            </div>
                            
                            <div class="form-group col-sm-4 custom-form-spacing">
                                <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">
                                <span class="kt-form__help error ex_showroom_price"></span>
                            </div>
                            <div class="form-group col-sm-3 custom-form-spacing">
                                <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">
                                <span class="kt-form__help error vin_number"></span>
                            </div>
                            <div class="form-group col-sm-4 custom-form-spacing">
                                <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">
                                <span class="kt-form__help error ex_showroom_price"></span>
                            </div>
                            <div class="form-group col-sm-4 custom-form-spacing">
                                 <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">Type A</option>
                                    <option value="Type B">Type B</option>
                                    <option value="Type C">Type C</option>
                                    <option value="Type D">Type D</option>
                                    <option value="Demo">Demo</option>
                                 </select>                                        
                                 <span class="kt-form__help error car_name"></span>
                             </div>

                             <div class="form-group col-sm-3">
                                <label for="remark">Remark</label>
                                <textarea name="remark" id="remark" class="form-control" placeholder="Enter Remark"></textarea>
                                <span class="kt-form__help error remark"></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">Yes</option>
                                    <option value="No">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">
                                <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-sm btn-rounded btn-outline-primary">Submit</button>
                            <a href="{{ route('transit-inventory') }}" 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>
    $(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);
                        });
                    }
                }
            });
        });
    });

     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 = phtml = vhtml = '';
                       
                    jQuery.each(resp.data, function(key, val){ 
                       /* html = val.car_name;
                        vhtml = val.varient_name;*/
                        ehtml = val.exterior_color;
                        ihtml = val.interior_color;
                        phtml = val.ex_showroom_price;
                    });
                    jQuery("#exterior_color").val(ehtml);
                    jQuery("#interior_color").val(ihtml);
                    jQuery("#ex_showroom_price").val(phtml);
                    /*jQuery("#veriant").val(vhtml);
                    jQuery("#car_name").val(html);*/
                }
            });
            
        }

        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 html = '';
                    var e_row = resp.data.Carvarient_arr;
                    //console.log(e_row);
                    html = '';
                    html += '<option value="">Select Veriant</option>';
                    jQuery.each(e_row, function(key, val) {
                        html += '<option value="' + val.id + '">' + val.name + '</option>';
                    });
                    console.log(html);
                    jQuery("#veriant").empty();
                    jQuery("#veriant").html(html);
                }
            });
        }
        jQuery(document).on('change', '#fuel_type', function(e) {
            //var id = $('#car_name').val();
            var option_check = 'Carvarient';
            var fuel_type = $('#fuel_type').val();
            var car_model = $('#car_name').val();
            // 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) {
                        //+ val.car_name + ' | ' + val.varient_name + ' | ' 
                        html += '<option value="' + val.id + '">' + val.exterior_color + ' | ' + val.interior_color + '</option>';
                    });
                    console.log(html);
                    jQuery("#name").empty();
                    jQuery("#name").html(html);
                }
            });
        }
        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