@extends('admin.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($offer ?? ''  && $offer->id)
                @method('PUT')
            @endif
            @csrf

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

            <div class="form-group col-lg-6" >
                <label id="start_date">{{ meta('start_date', getCurrentLocale()) ?? __('offers.start_date') }} </label>
                <input type="date" class="form-control {{ $errors->has('start_date') ? 'is-invalid' : '' }}" name="start_date" placeholder="{{ meta('start_date', getCurrentLocale()) ?? __('offers.start_date') }}" value='{{ old("start_date", $offer->start_date ?? "") }}'/>
            </div>

            <div class="form-group col-lg-6" id="end_date">
                <label>{{ meta('end_date', getCurrentLocale()) ?? __('offers.end_date') }} </label>
                <input type="date" class="form-control {{ $errors->has('end_date') ? 'is-invalid' : '' }}" name="end_date" placeholder="{{ meta('end_date', getCurrentLocale()) ?? __('offers.end_date') }}" value='{{ old("end_date", $offer->end_date ?? "") }}'/>
            </div>
            <div class="form-group col-lg-6">
                <label>{{ __('products.category_id') }} </label>
                <select onchange="categorySelect(this)" id="category_id" class="form-control {{ $errors->has('category_id') ? 'is-invalid' : '' }}" name="category_id">
                    <option value="">{{__('dashboard.select')}}</option>
                    @foreach($categories as $row)
                        <option {{ (isset($offer) ? $offer->category_id : null) == $row->id ? 'selected' : '' }} value="{{$row->id}}">{{$row->title}}</option>
                    @endforeach
                </select>
            </div>
            <div class="form-group col-lg-6">
                <label>{{ __('offers.sub_category_id') }} </label>
                <select id="sub_category_id" class="form-control {{ $errors->has('sub_category_id') ? 'is-invalid' : '' }}" name="sub_category_id">
                    <option value="">{{__('dashboard.select')}}</option>
                        @foreach($sub_categories as $row)
                            <option {{ (isset($offer) ? $offer->sub_category_id : null) == $row->id ? 'selected' : '' }} value="{{$row->id}}">{{$row->title}}</option>
                        @endforeach
                </select>
            </div>

            @foreach($languages as $language)

                <div class="form-group col-lg-6">
                    <label>{{ meta('title', $language->local) ?? __('offers.title') }} {{ count($languages) > 1 ? "({$language->name})" : '' }} </label>
                    <input type="text" class="form-control {{ $errors->has('title_'.$language->local) ? 'is-invalid' : '' }}" name="title_{{ $language->local }}" placeholder="{{ meta('title', $language->local) ?? __('offers.title') }}" value='{{ old("title_{$language->local}", $offer[$language->local]->title ?? "") }}'/>
                </div>

            @endforeach

                <div class="col-md-6 mb-4" id="products">
                    <label for="validationServer">{{__('offers.product')}}</label>
                    <input type="text" name="search_product" value="" placeholder="{{__('offers.product')}}" id="input-product" class="form-control usersBox" />
                    <div id="branche-product" class="well well-sm usersBox form-control" style="height: 150px; overflow: auto;">
                        @foreach($products as $row)
                            <div id="branche-product{{$row->id}}" class="branches"><i class="fa fa-minus-circle"></i> {{$row->title}}<input type="hidden" name="product_ids[]" value="{{$row->id}}"></div>
                        @endforeach
                    </div>
                </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 src="{{ asset('/assets/admin/js/lib/tinymce.min.js') }}"></script>
    <script>

        let sub_category_id = $('#sub_category_id').val()
        let category_id = $('#category_id').val()

            $('input[name=\'search_product\']').autocomplete({
            'source': function (request, response) {
                let target = request['term'];
                    $.ajax({
                        url: url_search_product,
                        data: 'title=' + target + "&category_id=" + category_id + '&sub_category_id=' + sub_category_id,
                        dataType: 'json',
                        type: 'get',

                        success: function (json) {
                            response($.map(json, function (item) {
                                return {
                                    label: `${item['title']}`,
                                    value: item['title'],
                                    id: item['id']
                                };
                            }));
                        }
                    });

            }, 'select': function (e, selected) {
                var item = selected['item'];
                $('#branche-product' + item['id']).remove();
                $('#input-product').removeAttr('value');
                $('#branche-product').append('<div id="branche-product' + item['id'] + '" class="branches"><i class="fa fa-minus-circle"></i> ' + item['label'] + '<input type="hidden" name="product_ids[]" value="' + item['id'] + '" /></div>');
            }
        });

        $('#branche-product').delegate('.fa-minus-circle', 'click', function () {
            $(this).parent().remove();
        });

        function categorySelect(elem)
        {
            let category_id = $(elem).val()
            $.ajax({
                url : url_category,
                type : 'GET',
                data : 'category_id='+category_id,
                success:function(data)
                {
                    $('#sub_category_id').html(data);
                }
            })
        }
        function selectStatus()
        {
            let status = $('#status').val()
            if (status == "{{$status_day}}")
            {
                $('#start_date').html("{{__('offers.date')}}")
                $('#end_date').hide()
            }
            else
            {
                $('#start_date').html("{{__('offers.start_date')}}")
                $('#end_date').show()
            }
        }
        $('#status').trigger('change')
    </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')}}",
            imagePath = "{{asset('')}}";
    </script>
    <script src="{{ asset('assets/admin/js/editor.js') }}"></script>
    <script src="{{ asset('assets/admin/js/lfm.js') }}"></script>
    <script src="{{ asset('assets/admin/js/articles.js') }}"></script>
@endsection



