@extends('store.layouts.container')

@section('content')
    <div class="side-title">
        <h3>{{ $head ?? ''}}</h3>
    </div>

    <div class="border-item">
        <form class="form-row" method="post" onsubmit="saveForm(this)" action="{{ $action }}" enctype="multipart/form-data">
            @if($productWarehouse ?? ''  && $productWarehouse->id)
                @method('PUT')
            @endif
            @csrf


            <div class="form-group col-lg-6">
                <label>{{ __('products.warehouse_id') }} </label>
                <select id="warehouse_id" class="form-control {{ $errors->has('warehouse_id') ? 'is-invalid' : '' }}" name="warehouse_id">
                    <option value="">{{__('dashboard.select')}}</option>
                    @foreach($warehouses as $row)
                        <option {{ (isset($productWarehouse) ? $productWarehouse->warehouse_id : null) == $row->id ? 'selected' : '' }} value="{{$row->id}}">{{$row->title}}</option>
                    @endforeach
                </select>
            </div>

                <div class="col-md-6 mb-4 product_status" id="products">
                    <label for="validationServer">{{__('product_warehouses.product')}}</label>
                    <input type="text" name="search_product" value="" placeholder="{{__('product_warehouses.product')}}" id="input-product" class="form-control usersBox" />
                    <div id="branche-product" class="well well-sm usersBox form-control" style="height: 150px; overflow: auto;">
                        @if($product)
                            <div id="branche-product{{$product->id}}" class="branches"><i class="fa fa-minus-circle"></i> {{$product->currentDescription->title}}<input type="hidden" discount="{{$product->discount_percentage}}" name="product_id" value="{{$product->id}}"></div>
                        @endif
                    </div>
                </div>



                <div class="form-group col-lg-6 none_attr" >
                    <label>{{ __('products.count') }} </label>
                    <input type="number" class="form-control {{ $errors->has('count') ? 'is-invalid' : '' }}" name="count" placeholder="{{ __('products.count') }}" value='{{ old("count", $productWarehouse->count ?? "") }}'/>
                </div>
                <div class="form-group col-lg-6" id="options">
                    @if($productWarehouse ?? ''  && $productWarehouse->id)
                        <label>{{ __('product_warehouses.option_id') }} </label>
                        <select id="option_id"  class="form-control {{ $errors->has('option_id') ? 'is-invalid' : '' }}" name="option_id">
                            <option value="">{{__('dashboard.select')}}</option>
                            @foreach($options as $row)
                                <option {{ (isset($productWarehouse) ? $productWarehouse->option_id : null) == $row->id ? 'selected' : '' }} value="{{$row->id}}">{{$row->title}}</option>
                            @endforeach
                        </select>
                    @endif
                </div>

                <div class="form-group col-lg-6 none_attr" >
                    <label>{{ __('products.cost') }} </label>
                    <input type="number" onkeyup="AddDicount()" id="cost" class="form-control {{ $errors->has('cost') ? 'is-invalid' : '' }}" name="cost" placeholder="{{ __('products.cost') }}" value='{{ old("cost", $productWarehouse->cost ?? "") }}'/>
                </div>

                <div class="form-group col-lg-6 none_attr" >
                    <label>{{ __('products.cost_discount') }} </label>
                    <p class="form-control" id="cost_discount" ></p>
                </div>

            <div class="text-center mt-5 col-12">
                <button class="butn"><span>{{ __('dashboard.save') }}</span></button>
            </div>
        </form>
    </div>

@endsection

@section('inner_js')
    <!-- Editor JS orders -->

    <script type="text/javascript">

        $('input[name=\'search_product\']').autocomplete({
            'source': function (request, response) {
                let target = request['term'];
                $.ajax({
                    url: url_search_product,
                    data: 'title='+target,
                    dataType: 'json',
                    type: 'get',
                    success: function (json) {
                        response($.map(json, function (item) {
                            return {
                                label:`${item['title']}` ,
                                value: item['title'],
                                id: item['id'],
                                discount: item['discount_percentage']
                            };
                        }));
                        AddDicount()
                    }
                });
            }, 'select': function (e, selected) {
                var item = selected['item'];
                $.ajax({
                    url: url_options,
                    data: 'product_id='+item['id'],
                    type: 'get',
                    success: function (data) {
                        $('#options').html(data)
                    }
                });
                $('#branche-product' + item['id']).remove();
                $('#input-product').removeAttr('value');
                $('#branche-product').html('<div id="branche-product' + item['id'] + '" class="branches"><i class="fa fa-minus-circle"></i> ' + item['label'] + '<input type="hidden" discount="'+item['discount']+'" name="product_id" value="' + item['id'] + '" /></div>');
            }
        });
        $('#branche-product').delegate('.fa-minus-circle', 'click', function () {
            $(this).parent().remove();
        });
    </script>
    <script src="{{ asset('/assets/admin/js/lib/tinymce.min.js') }}"></script>
    <script>
        function AddDicount()
        {
            let cost = $('#cost').val()
            let percetnage = $('input[name="product_id"]').attr('discount')
            if(percetnage && cost)
            {
                let discount = cost * (percetnage / 100)
                let cost_discount = cost - discount
                $('#cost_discount').html(cost_discount)
            }
        }
        $('#cost').trigger('keyup')
    </script>
    <script>
        let route_prefix = "{{ url(config('lfm.prefix')) }}",
            url = "{{url('/')}}/",
            currentLanguage = "{{getCurrentLocale()}}",
            url_category = "{{route('admin.subCategories')}}",
            url_search_product = "{{route('admin.product.search')}}",
            url_options = "{{route('admin.product.options')}}",
            imagePath = "{{asset('')}}";
    </script>
    <script src="{{ asset('assets/admin/js/editor.js') }}"></script>
    <script src="{{ asset('assets/admin/js/lfm.js') }}"></script>
@endsection



