@extends('template.app')

@section('meta')
@endsection

@section('title')
Update Sales
@endsection

@section('styles')
<link rel="stylesheet" href="//code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">

@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">Manage Sales</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('sales') }}" class="text-muted">Manage sales</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('sales.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">
                          

                            <?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-3">
                                <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>
                                    @endforeach
                                    @endif
                                </select>
                                <span class="kt-form__help error fule_type"></span>
                            </div>

                            <div class="form-group col-sm-3">
                                <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 }}</option>
                                    @endforeach
                                    @endif
                                </select>
                                <span class="kt-form__help error veriant"></span>
                            </div>


                            <div class="form-group col-sm-3">
                                <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><br>
                                <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-3">
                                <label for="interior_color">Interior Color</label><br>
                                <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-3">
                                <label for="team">Team</label>
                                <select class="form-control" name="team" id="team">
                                    <option value="">Select Team</option>
                                    @if(isset($team_master))
                                    @foreach($team_master as $team)
                                    <option value="{{ $team->id }}" @if(!empty($data->team) && $data->team==$team->id) selected @endif> {{ $team->team_name }}</option>
                                    @endforeach
                                    @endif
                                </select>

                                <span class="kt-form__help error team"></span>
                            </div>


                            <div class="form-group col-md-3">
                                            <label class="control-label">Date</label>
                                          

                                            <?php
                                                $created_date = '';
                                                if(!empty($data->created_date) && $data->created_date != null){
                                                    $created_date = date("d-m-Y",strtotime($data->created_date));
                                                }
                                            ?>

                                            <input type="text"  name="created_date" id="created_date" class="form-control" placeholder="dd-mm-yyyy" value="<?php echo $created_date; ?>">

                                            <span class="kt-form__help error created_date"></span>
                                        </div>

                            
                            <div class="form-group col-sm-3">
                                <label for="monthly_target">Monthly Target</label>
                                <input type="text" name="monthly_target" id="monthly_target" class="form-control" placeholder="Enter key number" value="{{ $data->monthly_target ?? '' }}">
                                <span class="kt-form__help error monthly_target"></span>
                            </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('sales') }}" 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="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>

<?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);
                        });
                    }
                }
            });
        });
    });


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

                jQuery.each(resp.data, 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);*/
            }
        });

    }

    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 basic_price = $('#basic_price').val();
        var purchase_price = $('#purchase_price').val();
        var kin_margin = basic_price - purchase_price;
        $('#kin_margin').val(kin_margin);
    });

    $(function() {
        $("#created_date").datepicker({
            dateFormat: 'dd-mm-yy'
        });

    });
</script>
@endsection