@extends('template.app')

@section('meta')
@endsection

@section('title')
Offers & Discounts
@endsection

@section('styles')
<link href="{{ asset('assets/css/dropify.min.css') }}" rel="stylesheet">
<link href="{{ asset('assets/css/sweetalert2.bundle.css') }}" rel="stylesheet">
@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">Offers & Discounts Master</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('offer') }}" class="text-muted">Offers & Discounts Master</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('offer.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="title"> Title</label>
                                <input type="text" name="title" id="title" class="form-control" placeholder="Enter Title" value="{{ $data->title ??'' }}">
                                <span class="kt-form__help error title"></span>
                            </div>
                            
                            <div class="form-group col-sm-4">
                                <label for="start_date">Start date</label>
                                <input type="date" name="start_date" id="start_date" class="form-control" placeholder="Enter Start date" value="{{ $data->start_date ??'' }}">
                                <span class="kt-form__help error start_date"></span>
                            </div>
                            <div class="form-group col-sm-4">
                                <label for="end_date">End date</label>
                                <input type="date" name="end_date" id="end_date" class="form-control" placeholder="Enter End date" value="{{ $data->end_date ??'' }}">
                                <span class="kt-form__help error end_date"></span>
                            </div>
                            <div class="form-group col-sm-4">
                                <label for="offer_type">Offer type</label>
                               <!--  <input type="text" name="offer_type" id="offer_type" class="form-control" placeholder="Enter Offer type"> -->
                               <select class="form-control" name="offer_type" id="offer_type">
                                     <option value="{{ $data->offer_type ??'' }}">{{ $data->offer_type }}</option>
                                       <option value="KIN"> KIN Offers</option>
                                        <option value="ShivamIB"> Shivam IB Offers</option>
                                 </select>
                                <span class="kt-form__help error offer_type"></span>
                            </div>
                            <div class="form-group col-sm-4">
                                <label for="offer_type">Amount</label>
                                <input type="text" name="amount" id="offer_type" class="form-control" placeholder="Enter Amount" value="{{ $data->amount ??'' }}">
                                <span class="kt-form__help error offer_type"></span>
                            </div>
                            <div class="form-group col-sm-4 custom-form-spacing">
                                <label for="offer_type">KIA Share / Company Contribution </label>
                                <input type="text" name="company_contribution" id="company_contribution" class="form-control" placeholder="Enter Amount" value="{{ $data->company_contribution ?? '' }}">
                                <span class="kt-form__help error company_contribution"></span>
                            </div>
                            <div class="form-group col-sm-4">
                                <label for="car_name">Branch</label>
                                    <select class="form-control" name="branch_id[]" id="branch_id" multiple>
                                        @php
                                            $selectedBranches = [];
                                        
                                            if(!empty($data->fk_branch_id)){
                                                $selectedBranches = array_filter(
                                                    array_map('intval', explode(',', trim($data->fk_branch_id, ',')))
                                                );
                                            }
                                        @endphp
                                        @if(isset($branch)) 
                                            @foreach($branch as $branches)
                                                <option value="{{ $branches->id }}"
                                                    {{ in_array((int)$branches->id, $selectedBranches) ? 'selected' : '' }}>
                                                    {{ $branches->name }}
                                                </option>
                                            @endforeach
                                        @endif
                                    </select> 
                                <span class="kt-form__help error car_name"></span>
                            </div>
                            
                            <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="{{ $data->mid }}"> {{ $data->name }}</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" id="fule_type">
                                <label for="fule_type">Fuel Type</label>  
                                <select class="form-control" name="fuel_type" id="fuel_type">
                                     <option value="{{ $data->fid }}"> {{ $data->ftitle }}</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">
                                 <label for="car_name">Car Variant</label>  
                                 <select class="form-control" name="veriant[]" id="veriant" multiple>
                                    <option value="">Select Car Variant</option>
                                    @if(isset($veriant) && isset($data->fk_car_varient_id)) 
                                        @php
                                            $selectedVarients = explode(',', $data->fk_car_varient_id); // Convert stored variant IDs into an array
                                        @endphp
                                        @foreach($veriant as $que)
                                            @if(in_array($que->id, $selectedVarients)) <!-- Only show selected variants -->
                                                <option value="{{ $que->id }}" selected>
                                                    {{ $que->name }}
                                                </option>
                                            @endif
                                        @endforeach
                                    @endif
                                 </select>                                        
                                 <span class="kt-form__help error car_name"></span>
                             </div>

                             <div class="form-group col-sm-4">
                                 <label for="vin">Car VIN Number</label>  
                                 <select class="form-control" name="vin[]" id="vin" multiple> 
                                    <option value="{{ $data->vin_number }}"> {{ $data->vin_number }}</option>
                                 </select>                                        
                                 <span class="kt-form__help error vin"></span>
                             </div>

                            <!--<div class="form-group col-sm-4" id="price">
                                <label for="price">Ex Showroom Price</label>
                                <input type="text" name="ex_showroom_price"  readonly="readonly" class="form-control" value="{{$data->ex_showroom_price}}">
                            </div>

                            <div class="form-group col-sm-4" id="aging_days">
                            <label for="price">Aging Days</label> <input type="text" name="aging_days"  readonly="readonly" class="form-control"> <span class="kt-form__help error aging_days"></span>
                            </div>-->

                            <!-- <div class="form-group col-sm-4" id="exteriorcolor">
                            <label for="price">Exterior Color</label> <input type="text" name="exteriorcolor"  readonly="readonly" class="form-control" value="{{$data->ename}}"> <span class="kt-form__help error exteriorcolor"></span>
                            </div>

                            <div class="form-group col-sm-4" id="interiorcolor">
                            <label for="price">Interior Color</label> <input type="text" name="interiorcolor" readonly="readonly" class="form-control" value="{{$data->iname}}"> <span class="kt-form__help error interiorcolor"></span>
                            </div> -->
                            <div class="form-group col-sm-9">
                                <label for="description">Description</label>
                                <textarea name="description" row="3" id="description"  class="form-control"> {{ $data->description ??'' }} </textarea>
                                <span class="kt-form__help error description"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <button type="submit" name="submit" id="submit" class="btn waves-effect waves-light btn-rounded btn-outline-primary">Submit</button>
                            <a href="{{ route('offer') }}" class="btn waves-effect waves-light btn-rounded btn-outline-secondary">Back</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script src="{{ asset('assets/js/promise.min.js') }}"></script>
<script src="{{ asset('assets/js/sweetalert2.bundle.js') }}"></script>

<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_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_vin(varient, option_check) {
            $.ajax({
                url: "{{ url('offer/car_vin_asper_variant') }}",
                data: {
                    'varient': varient,                   
                    'option_check': option_check
                },
                type: 'get',
                dataType: 'json',
                success: function(resp) {
                    var html = '';
                    var e_row = resp.data.vin_num;
                    //console.log(e_row);
                    html = '';
                    html += '<option value="">Select VIN Number</option>';
                    jQuery.each(e_row, function(key, val) {
                        html += '<option value="' + val.vin_num + '">' + val.vin_num + '</option>';
                    });
                    console.log(html);
                    jQuery("#vin").empty();
                    jQuery("#vin").html(html);
                }
            });
        }
        jQuery(document).on('change', '#veriant', function(e) {
            //var id = $('#car_name').val();
            var option_check = 'vin_number';           
            var varient = $('#veriant').val();
            // alert('1');
            car_vin(varient, option_check);
        });


         function car_price(vin_num, option_check) {
            $.ajax({
                url: "{{ url('offer/ex_price_asper_vin') }}",
                data: {
                    'vin_num': vin_num,                   
                    'option_check': option_check
                },
                type: 'get',
                dataType: 'json',
                success: function(resp) {
                    var html = '';
                    var html_s = '';
                    var html_ss = '';
                    var e_row = resp.data;
                    console.log(e_row);
                     html = '<div class="form-group"><label for="price">Ex Showroom Price</label> <input type="text" name="ex_showroom_price" value="'+ resp.data.price +'" readonly="readonly" class="form-control"> <span class="kt-form__help error price"></span></div>';

                     html_s += '<div class="form-group" id="aging_days"><label for="price">Aging Days</label> <input type="text" name="aging_days" value="'+ resp.data.aging_days +'" readonly="readonly" class="form-control"> <span class="kt-form__help error price"></span></div>';

                    /*html_s += '<div class="form-group" id="exteriorcolor"><label for="price">Exterior Color</label> <input type="text" name="exteriorcolor" value="'+ resp.data.exteriorcolor +'" readonly="readonly" class="form-control"> <span class="kt-form__help error price"></span></div>';
                    html_ss += '<div class="form-group" id="interiorcolor"><label for="price">Interior Color</label> <input type="text" name="interiorcolor" value="'+ resp.data.interiorcolor +'" readonly="readonly" class="form-control"> <span class="kt-form__help error price"></span></div>';*/
                    console.log(html);
                    jQuery("#price").empty();
                    jQuery("#price").html(html);
                    jQuery("#aging_days").html(html_s);
                   /* jQuery("#exteriorcolor").html(html_s);
                    jQuery("#interiorcolor").html(html_ss);*/
                }
            });
        }
        jQuery(document).on('change', '#vin', function(e) {
            //var id = $('#car_name').val();
            var option_check = 'ex_showroom_price';           
            var vin_num = $('#vin').val();
            
            car_price(vin_num, option_check);
        });


        jQuery(document).on('click','#submit',function(e){
            var vin_num = $('#vin').val();
            alert(vin_num);
        });
    });
</script>
@endsection