@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">
<style>
    .col-sm-3{
        font-size:15px;
    }
    .form-control{
        height:30px;
        font-size:14px;
    }
</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">Discount 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('discount') }}" class="text-muted">Discount</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('discount.insert') }}" name="form" id="form" method="post" enctype="multipart/form-data">
                        @csrf
                        @method('POST')

                        <div class="row">
                            <div class="form-group col-sm-4">
                                <label for="tittle"> Tittle</label>
                                <input type="text" name="title" id="tittle" class="form-control" placeholder="Enter Tittle">
                                <span class="kt-form__help error tittle"></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">
                                <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">
                                <span class="kt-form__help error end_date"></span>
                            </div>
                            
                            <div class="form-group col-sm-4">
                                <label for="discount_year">Discount Year</label>
                                <select name="discount_year" id="discount_year" class="form-control">
                                    <option value="">Select Year</option>
                                    @php
                                        $currentYear = date('Y');
                                    @endphp
                            
                                    @for ($i = $currentYear; $i >= $currentYear - 50; $i--)
                                        <option value="{{ $i }}">{{ $i }}</option>
                                    @endfor
                                </select>
                                <span class="kt-form__help error discount_year"></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>
                                    <option value="">Select Branch</option>
                                    @if(isset($branch)) 
                                        @foreach($branch as $branches)
                                           <option value="{{ $branches->id }}"> {{ $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="">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" 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">
                                <label for="car_name">Car Variant</label>  
                                <select class="form-control" name="veriant[]" id="veriant" multiple>
                                    <option value="">Select Car Varient</option>
                                    @if(isset($veriant)) 
                                        @foreach($veriant as $var)
                                           <option value="{{ $var->id }}"> {{ $var->name }}</option>
                                        @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>   
                                </select>                                        
                                <span class="kt-form__help error vin"></span>
                            </div>
                            
                            <div class="form-group col-sm-4">
                                <label for="description">Remarks</label>
                                <textarea name="description" row="3" id="description"  class="form-control"style="height: 94px;"></textarea>
                                <span class="kt-form__help error description"></span>
                            </div>
                        </div>
                            
                        <div class="row mt-5">   
                            <div class="form-group col-sm-2" style="margin-top:30px;">
                                <label for="kec_desgn">KEC</label>  
                            </div>
                            
                            <div class="form-group col-sm-4 ">
                                <label for="kec_discount">Amount</label>
                                <input type="text" name="kec_discount" id="kec_discount" class="form-control" placeholder="Enter Amount">
                                <span class="kt-form__help error kec_discount"></span>
                            </div>
                        </div>
                        
                        <div class="row">   
                            <div class="form-group col-sm-2" style="margin-top:30px;">
                                <label for="tl_desgn">Team Leader</label>  
                            </div>
                            
                            <div class="form-group col-sm-4 ">
                                <label for="tl_discount">Amount</label>
                                <input type="text" name="tl_discount" id="tl_discount" class="form-control" placeholder="Enter Amount">
                                <span class="kt-form__help error tl_discount"></span>
                            </div>
                        </div>

                        <div class="row">   
                            <div class="form-group col-sm-2" style="margin-top:30px;">
                                <label for="sm_desgn">Sales Manager</label>  
                            </div>
                            
                            <div class="form-group col-sm-4 ">
                                <label for="sm_discount">Amount</label>
                                <input type="text" name="sm_discount" id="sm_discount" class="form-control" placeholder="Enter Amount">
                                <span class="kt-form__help error sm_discount"></span>
                            </div>
                        </div>

                        <div class="row">   
                            <div class="form-group col-sm-2" style="margin-top:30px;">
                                <label for="gm_desgn">General Manager</label>  
                            </div>
                            
                            <div class="form-group col-sm-4 ">
                                <label for="gm_discount">Amount</label>
                                <input type="text" name="gm_discount" id="gm_discount" class="form-control" placeholder="Enter Amount">
                                <span class="kt-form__help error gm_discount"></span>
                            </div>
                        </div>

                        <div class="row">   
                            <div class="form-group col-sm-2" style="margin-top:30px;">
                                <label for="bh_desgn">Business Head</label>  
                            </div>
                            
                            <div class="form-group col-sm-4 ">
                                <label for="bh_discount">Amount</label>
                                <input type="text" name="bh_discount" id="bh_discount" class="form-control" placeholder="Enter Amount">
                                <span class="kt-form__help error bh_discount"></span>
                            </div>
                        </div>
                        
                        <div class="row">   
                            <div class="form-group col-sm-2" style="margin-top:30px;">
                                <label for="ceo_desgn">CEO</label>  
                            </div>
                            
                            <div class="form-group col-sm-4 ">
                                <label for="ceo_discount">Amount</label>
                                <input type="text" name="ceo_discount" id="ceo_discount" class="form-control" placeholder="Enter Amount">
                                <span class="kt-form__help error ceo_discount"></span>
                            </div>
                        </div>
                        
                        <div class="row">   
                            <div class="form-group col-sm-2" style="margin-top:30px;">
                                <label for="md_desgn">Managing Director</label>  
                            </div>
                            
                            <div class="form-group col-sm-4 ">
                                <label for="md_discount">Amount</label>
                                <input type="text" name="md_discount" id="md_discount" class="form-control" placeholder="Enter Amount">
                                <span class="kt-form__help error md_discount"></span>
                            </div>
                        </div>
                            
                        <div class="form-group mt-5">
                            <button type="submit" id="submit" class="btn waves-effect waves-light btn-sm btn-rounded btn-outline-primary ">Submit</button>
                            <a href="{{ route('discount') }}" 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/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 + ' - ' + val.branch + '</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);
        });
        
        $(document).ready(function() {
            $('#vin').select2({
                placeholder: "Search & Select VIN Number",
                allowClear: true,
                width: '100%'
            });
        });


         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>';

                    jQuery("#price").empty();
                    jQuery("#price").html(html);
                    jQuery("#aging_days").html(html_s);
                }
            });
        }
        jQuery(document).on('change', '#vin', function(e) {
            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);
            if(vin_num == null || vin_num == ''){
                if (confirm('Is it applicable to all cars of selected variants?')) {
                    $.ajax({                        
                        "dataType": "json",
                        "type": "POST",
                        "data": {                           
                            _token: "{{ csrf_token() }}"
                        },
                        success: function(response) {
                            if (response.code == 200) {
                                datatable.ajax.reload();
                                toastr.success('Status changed successfully', 'Success');
                            } else {
                                toastr.error('Failed to change status', 'Error');
                            }
                        }
                    });
                }
            }
        });
    });
</script>
@endsection