@extends('template.app')

@section('meta')
@endsection

@section('title')
    Product Create
@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-md-3 {
        font-size: 15px;
    }
    .form-control {
        height: 38px !important;
        width: 186px !important;
        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">Car 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('products') }}" class="text-muted">Car Master</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('products.insert') }}" name="form" id="form" method="post" enctype="multipart/form-data">
                        @csrf
                        @method('POST')

                        <div class="row">
                            <div class="form-group col-md-3">
                                <label for="car_model">Car Model</label>
                                <select class="form-control" name="car_model" id="car_model">
                                    <option value="">Select Car Model</option>
                                    @if(isset($Carmodel) && $Carmodel->isNotEmpty())
                                        @foreach($Carmodel as $car_model)
                                            <option value="{{ $car_model->id }}">{{ ucfirst(str_replace('_', ' ', $car_model->name)) }}</option>
                                        @endforeach
                                    @endif
                                </select>
                                <span class="kt-form__help error car_model"></span>
                            </div>

                            <div class="form-group col-md-3">
                                <label for="fuel_type">Fuel Type <span class="required" style="color: red;">*</span></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 $val)
                                            <option value="{{ $val->id }}">{{ $val->title }}</option>
                                        @endforeach
                                    @endif
                                </select>
                                <span class="kt-form__help error fuel_type"></span>
                            </div>

                            <div class="form-group col-md-3">
                                <label for="veriant">Car Variant</label>
                                <select class="form-control" name="veriant" id="veriant" onchange="car_mveriant(this);"></select>
                                <span class="kt-form__help error veriant"></span>
                            </div>

                            <div class="form-group col-md-3">
                                <label for="exterior_color">Exterior Color</label>
                                <select class="form-control" name="exterior_color" id="exterior_color" disabled="disabled"></select>
                                <span class="kt-form__help error exterior_color"></span>
                            </div>
                        </div>

                        <div class="row">
                            <div class="form-group col-md-3">
                                <label for="interior_color">Interior Color</label>
                                <select class="form-control" name="interior_color" id="interior_color" disabled="disabled"></select>
                                <span class="kt-form__help error interior_color"></span>
                            </div>
                        </div>
                        <!--  <div class="form-group col-sm-3 d-none">
                            <label for="category_id">Car Type</label>
                                <select class="form-control" name="category_id" id="category_id">
                                    <option value="">Select Car Type</option>
                                        @if(isset($data) && $data->isNotEmpty())
                                            @foreach($data AS $row)
                                    <option value="{{ $row->id }}">{{ $row->name ??'' }}</option>
                                            @endforeach
                                        @endif
                                </select>
                            <span class="kt-form__help error category_id"></span>
                        </div> -->
                        <!-- <div class="form-group col-sm-3">
                            <label for="ex_showroom_price">Ex-Showroom Price</label>
                            <input type="number" name="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 d-none">
                            <label for="is_applicable_for_mcp">Is Applicable For My Convinience Program</label>
                                <select class="form-control" name="is_applicable_for_mcp" id="is_applicable_for_mcp">
                                    <option value="">Select Is Applicable For My Convinience Program</option>
                                    <option value="yes">Yes</option>
                                    <option value="no">No</option>
                                </select>
                            <span class="kt-form__help error is_applicable_for_mcp"></span>
                        </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('products') }}" 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_mveriant(e) {
        var id = e.value;
        $.ajax({
            url: "{{ url('products/carvarient/') }}",
            data: { 'id': id },
            type: 'get',
            dataType: 'json',
            success: function(resp) {
                var html = '', ihtml = '';
                var e_row = resp.data.Exterior_color;
                var row = resp.data.Interior_color;

                jQuery.each(e_row, function(key, val) { 
                    $('#exterior_color').removeAttr('disabled');
                    html += '<option value="'+val.e_id+'">'+val.e_name+'</option>';
                });

                jQuery.each(row, function(key, val) { 
                    $('#interior_color').removeAttr('disabled');
                    ihtml += '<option value="'+val.id+'">'+val.name+'</option>';
                });

                jQuery("#exterior_color").empty().html(html);
                jQuery("#interior_color").empty().html(ihtml);
            }
        });
    }

    function car_name_new(id, option_check, fuel, new_mcp) {
        $.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 e_row = resp.data.Carvarient_arr;
                var html = '<option value="">Select Variant</option>';
                jQuery.each(e_row, function(key, val) {
                    html += '<option value="'+val.id+'">'+val.name+'</option>';
                });
                jQuery("#veriant").empty().html(html);
            }
        });
    }

    jQuery(document).on('change', '#fuel_type', function(e) {
        var id = $('#car_model').val();
        var option_check = 'Carvarient';
        var fuel = $('#fuel_type').val();
        var new_mcp = 'Mcp';
        car_name_new(id, option_check, fuel, new_mcp);
    });
</script>
@endsection
