@extends('admin.layouts.container')

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

    <div class="border-item">
        <form class="form-row" method="post" action="{{ $action }}" enctype="multipart/form-data">

            @if($seller ?? ''  && $seller->id)
                @method('PUT')
            @endif
            @csrf
            <div class="form-group col-lg-6">
                <label>{{ meta('seller_id', getCurrentLocale()) ?? __('sellers.seller_id')}}</label>
                <select class="form-control {{ $errors->has('seller_id') ? 'is-invalid' : '' }}" name="seller_id">
                    <option value="">{{__('dashboard.select')}}</option>
                    @foreach($sellers as $row)
                        <option {{ (isset($seller) ? $seller->seller_id : '') == $row->id ? 'selected' : '' }} value="{{$row->id}}">{{$row->name}}</option>
                    @endforeach
                </select>
            </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>{{ meta('count', getCurrentLocale()) ?? __('sellers.count')}}</label>
                <input type="number" min="0" class="form-control  {{ $errors->has('count') ? 'is-invalid' : '' }}" name="count" placeholder="{{ meta('count', getCurrentLocale()) ?? __('sellers.count') }}" value='{{ old("count", $seller->count ?? "") }}'/>
            </div>

            <div class="form-group col-lg-6">
                <label>{{ meta('status_user', getCurrentLocale()) ?? __('sellers.status_user')}}</label>
                <select onchange="statusSelect(this)" class="form-control {{ $errors->has('status_user') ? 'is-invalid' : '' }}" name="status_user">
                    <option @if($seller ?? ''  && $seller->id) @if($seller->status_user == 'all') selected @endif @endif value="all">{{__('sellers.all')}}</option>
                    <option @if($seller ?? ''  && $seller->id) @if($seller->status_user == 'specific') selected @endif @endif value="specific">{{__('sellers.specific')}}</option>
                </select>
            </div>
            <div class="form-group col-lg-6">
                <label>{{ meta('discount_type', getCurrentLocale()) ?? __('sellers.discount_type')}}</label>
                <select onchange="typeSelect(this)" class="form-control {{ $errors->has('discount_type') ? 'is-invalid' : '' }}" name="discount_type">
                    <option @if($seller ?? ''  && $seller->id) @if($seller->discount_type == '1') selected @endif @endif value="1">{{__('sellers.value')}}</option>
                    <option @if($seller ?? ''  && $seller->id) @if($seller->discount_type == '2') selected @endif @endif  value="2">{{__('sellers.percentage')}}</option>
                </select>
            </div>
            <div class="form-group col-lg-6">
                <label>{{ meta('discount_amount', getCurrentLocale()) ?? __('sellers.discount_amount')}}</label>
                <input type="number"  class="form-control  {{ $errors->has('discount_amount') ? 'is-invalid' : '' }}" name="discount_amount" placeholder="{{ meta('discount_amount', getCurrentLocale()) ?? __('sellers.discount_amount') }}" value='{{ old("discount_amount", $seller->discount_amount ?? "") }}'/>
            </div>

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

            <div class="form-group col-lg-6">
                <label>{{ meta('start_date', getCurrentLocale()) ?? __('sellers.start_date')}}</label>
                <input type="date"  class="form-control  {{ $errors->has('start_date') ? 'is-invalid' : '' }}" name="start_date" placeholder="{{ meta('start_date', getCurrentLocale()) ?? __('sellers.start_date') }}" value='{{ old("start_date", $seller->start_date ?? "") }}'/>
            </div>
            <div class="form-group col-lg-6">
                <label>{{ meta('end_date', getCurrentLocale()) ?? __('sellers.end_date')}}</label>
                <input type="date"  class="form-control  {{ $errors->has('end_date') ? 'is-invalid' : '' }}" name="end_date" placeholder="{{ meta('end_date', getCurrentLocale()) ?? __('sellers.end_date') }}" value='{{ old("end_date", $seller->end_date ?? "") }}'/>
            </div>
                <div class="col-md-12 mb-4" id="users" @if ($seller ?? '' && $seller->id) @if($seller->status_user == 'all') style="display: none" @endif  @else style="display: none" @endif>
                    <label for="validationServer">{{__('sellers.user')}}</label>
                    <input type="text" name="search_user" value="" placeholder="{{__('sellers.product')}}" id="input-product" class="form-control usersBox" />
                    <div id="branche-user" class="well well-sm usersBox form-control" style="height: 150px; overflow: auto;">
                        @if ($seller ?? '' && $seller->id)
                            @foreach($users as $row)
                                <div id="branche-user{{$row->user_id}}" class="branches"><i class="fa fa-minus-circle"></i> {{$row->name}}<input type="hidden" name="user_ids[]" value="{{$row->id}}"></div>
                            @endforeach
                        @endif
                    </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 files -->
    {{--    <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 src="//cdn.tinymce.com/4/tinymce.min.js"></script>

    <script>
        let url_search_user = "{{url('')}}/admin/cobons/user"
    </script>
    <script>
        function statusSelect(elem)
        {
            let data = $(elem).val()
            if(data == 'all')
            {
                $('#users').css('display','none');
            }
            else
            {
                $('#users').css('display','block');
            }
        }
    </script>
    <script>

        $('input[name=\'search_user\']').autocomplete({

            'source': function (request, response) {
                let target = request['term'];
                $.ajax({
                    url: url_search_user,
                    data: 'name='+target,
                    dataType: 'json',
                    type: 'get',

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

        $('#branche-user').delegate('.fa-minus-circle', 'click', function () {
            $(this).parent().remove();
        });
    </script>

    <script>
        let route_prefix = "{{ url(config('lfm.prefix')) }}",
            url = "{{url('/')}}/",
            url_type = {!! json_encode(url(getCurrentLocale().'/admin/type/')) !!},
            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>
    <script src="{{ asset('assets/admin/js/sellers.js') }}"></script>
@endsection



