@extends('template.app')

@section('meta')
@endsection

@section('title')
    MCP Update
@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-6 align-self-center">
            <h4 class="page-title text-truncate text-dark font-weight-medium mb-1">MCP 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('mcp') }}" class="text-muted">MCP 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('mcp.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-3 custom-form-spacing">
                                <label for="name">Name</label>
                                <input type="text" name="name" id="name" class="form-control" placeholder="Enter Name" value="{{ $data->name ??'' }}">
                                <span class="kt-form__help error name"></span>
                            </div>
                        <div class="form-group col-sm-3 custom-form-spacing">
                                <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 }}" @if($data->car_model == $car_model->id) selected @endif >{{ 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-sm-3 custom-form-spacing">
                                <label for="car_model">Fual Type</label>
                                <select class="form-control" name="fual_type" id="car_model">
                                    <option value="">Select Fual Type</option>
                                    @if(isset($fualtype) && $fualtype->isNotEmpty())
                                        @foreach($fualtype as $f)
                                            <option value="{{ $f->id }}" @if($data->fual_type_id == $f->id) selected @endif>{{ $f->title }}</option>
                                        @endforeach
                                    @endif
                                </select>
                                <span class="kt-form__help error car_model"></span>
                            </div>
                            <div class="form-group col-sm-3 custom-form-spacing">
                                <label for="amount">Amount</label>
                                <input type="text" name="amount" id="amount" class="form-control" placeholder="Enter Amount" value="{{ $data->amount ??'' }}">
                                <span class="kt-form__help error amount"></span>
                            </div>
                            <div class="form-group col-sm-3">
                                <label for="profit">Profit</label>
                                <input type="number" name="profit" id="profit" class="form-control" placeholder="Enter Profit Amount" value="{{ $data->profit ??'' }}">
                                <span class="kt-form__help error profit"></span>
                            </div>
                            <div class="form-group col-sm-3">
                                <label for="gst_percentage">GST %</label>
                                <input type="number" name="gst_percentage" id="gst_percentage" class="form-control" placeholder="Enter GST Percentage" value="{{ $data->gst_percentage ??'' }}">
                                <span class="kt-form__help error gst_percentage"></span>
                            </div>
                            <div class="form-group col-sm-3">
                                <label for="gst">GST</label>
                                <input type="number" name="gst" id="gst" class="form-control" placeholder="Enter GST" value="{{ $data->gst ??'' }}" readonly>
                                <span class="kt-form__help error gst"></span>
                            </div>
                            <div class="form-group col-sm-3">
                                <label for="total_profit">Total Profit</label>
                                <input type="number" name="total_profit" id="total_profit" class="form-control" placeholder="Enter Amount" value="{{ $data->total_profit ??'' }}">
                                <span class="kt-form__help error total_profit"></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('mcp') }}" class="btn waves-effect waves-light btn-rounded btn-outline-secondary">Back</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script>
    $("#percentage").keyup(function(e){
        $('.percentage').html('');
        if ($(this).val() > 99){
            $('.percentage').html('');
            $('.percentage').html('Enter Value Between 0 to 100!');
            $(this).val();
            $(this).val(100);
        }else if($(this).val() <= 99){
            $('.percentage').html('');
        }
    });

</script>

<script>
    $(document).ready(function() {
        $('#gst_percentage').blur(function() {
            var profit = parseFloat($('#profit').val());
            var gstPercentage = parseFloat($('#gst_percentage').val());
            var gst = (profit * gstPercentage) / 100;
            var total_profit = profit+gst;
            $('#gst').val(gst.toFixed(0));
            $('#total_profit').val(total_profit.toFixed(0))
            console.log('Total Profit:', total_profit);
    });
        
        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);
                        });
                    }
                }
            });
        });
    });
</script>
@endsection