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

                <div class="form-group col-lg-6">
                    <label>{{ __('sellers.user_id') }} </label>
                    <select id="user_id" class="form-control {{ $errors->has('user_id') ? 'is-invalid' : '' }}" name="user_id">
                        <option value="">{{__('sellers.select')}}</option>
                        @foreach($users as $row)
                            <option {{isset($seller) ? ($seller->user_id == $row->id ? 'selected' : '') : ''}} value="{{$row->id}}">{{$row->name}}</option>
                        @endforeach
                    </select>
                </div>

            <div class="form-group col-lg-6">
                <label>{{ meta('title', getCurrentLocale()) ?? __('sellers.title')}}</label>
                <input type="text"  class="form-control  {{ $errors->has('title') ? 'is-invalid' : '' }}" name="title" placeholder="{{ meta('title', getCurrentLocale()) ?? __('sellers.title') }}" value='{{ old("title", $seller->title ?? "") }}'/>
            </div>
            <div class="form-group col-lg-6">
                <label>{{ meta('code', getCurrentLocale()) ?? __('sellers.code')}}</label>
                <input type="text"  class="form-control  {{ $errors->has('code') ? 'is-invalid' : '' }}" name="code" placeholder="{{ meta('code', getCurrentLocale()) ?? __('sellers.code') }}" value='{{ old("code", $seller->code ?? "") }}'/>
            </div>

            <div class="form-group col-lg-6">
                <label>{{ __('sellers.discount_status') }} </label>
                <select id="discount_status" class="form-control {{ $errors->has('discount_status') ? 'is-invalid' : '' }}" name="discount_status">
                    @foreach($discount_statuses as $row)
                        <option {{isset($seller) ? ($seller->discount_status == $row ? 'selected' : '') : ''}} value="{{$row}}">{{__('sellers.discount_statuses.'.$row)}}</option>
                    @endforeach
                </select>
            </div>

            <div class="form-group col-lg-6" >
                <label>{{ __('sellers.discount') }} </label>
                <input type="number" class="form-control {{ $errors->has('discount') ? 'is-invalid' : '' }}" name="discount" placeholder="{{ __('sellers.discount') }}" value='{{ old("discount", $seller->discount ?? "") }}'/>
            </div>

            <div class="form-group col-lg-6">
                <label>{{ __('sellers.status') }} </label>
                <select id="status" onchange="statusSelect()" class="form-control {{ $errors->has('status') ? 'is-invalid' : '' }}" name="status">
                    @foreach($statuses as $row)
                        <option {{isset($seller) ? ($seller->status == $row ? 'selected' : '') : ''}} value="{{$row}}">{{__('sellers.statuses.'.$row)}}</option>
                    @endforeach
                </select>
            </div>
            <div class="col-md-6 mb-4" id="product_status">
                <label for="validationServer">{{__('sellers.product')}}</label>
                <input type="text" name="search_product" value="" placeholder="{{__('sellers.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 ($seller ?? '' && $seller->id)
                        @foreach($products as $row)
                            <div id="branche-product{{$row->product_id}}" class="branches"><i class="fa fa-minus-circle"></i> {{$row->product->currentDescription->title}}<input type="hidden" name="product_ids[]" value="{{$row->product_id}}"></div>
                        @endforeach
                    @endif
                </div>
            </div>

            <div class="form-group col-lg-6" >
                <label>{{ __('sellers.user_count') }} </label>
                <input type="number" class="form-control {{ $errors->has('user_count') ? 'is-invalid' : '' }}" name="user_count" placeholder="{{ __('sellers.user_count') }}" value='{{ old("user_count", $seller->user_count ?? "") }}'/>
            </div>

            <div class="form-group col-lg-6" >
                <label>{{ __('sellers.count') }} </label>
                <input type="number" class="form-control {{ $errors->has('count') ? 'is-invalid' : '' }}" name="count" placeholder="{{ __('sellers.count') }}" value='{{ old("count", $seller->count ?? "") }}'/>
            </div>

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

            <div class="form-group col-lg-6" >
                <label>{{ __('sellers.end_date') }} </label>
                <input type="date" class="form-control {{ $errors->has('end_date') ? 'is-invalid' : '' }}" name="end_date" placeholder="{{ __('sellers.end_date') }}" value='{{ old("end_date", $seller->end_date ?? "") }}' />
            </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>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <script>

        $('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']
                            };
                        }));
                    }
                });
            }, '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 statusSelect()
        {
            let data = $('#status').val()
            if(data == '0')
            {
                $('#product_status').show();
            }
            else
            {
                $('#product_status').hide();
            }
        }
        $('#status').trigger('change');
    </script>
    <script>
        let route_prefix = "{{ url(config('lfm.prefix')) }}",
            url = "{{url('/')}}/",
            url_search_product = "{{route('admin.search_products')}}",
            currentLanguage = "{{getCurrentLocale()}}",
            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



