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


{{--
            <div class="form-group col-lg-6">
                <label>{{ meta('status', getCurrentLocale()) ?? __('attributes.status')}}</label>
                <select onchange="statusSelect(this)" id="status" class="form-control {{ $errors->has('status') ? 'is-invalid' : '' }}" name="status" >
                    <option {{ (isset($attribute) ? $attribute->status : '')  == 'multi' ? 'selected' : ''}} value="multi">{{__('attributes.multi')}}</option>
                    <option {{ (isset($attribute) ? $attribute->status : '')  == 'one' ? 'selected' : ''}} value="one">{{__('attributes.one')}}</option>
                </select>
            </div>

            <div class="form-group col-lg-6" id="types">
                <label>{{ meta('type', getCurrentLocale()) ?? __('attributes.type')}}</label>
                <select id="type" onchange="typeSelect(this)" class="form-control {{ $errors->has('type') ? 'is-invalid' : '' }}" name="type" >
                    <option {{ (isset($attribute) ? $attribute->type : '')  == 'main' ? 'selected' : ''}} value="main">{{__('attributes.main')}}</option>
                    <option {{ (isset($attribute) ? $attribute->type : '')  == 'sub' ? 'selected' : ''}} value="sub">{{__('attributes.sub')}}</option>
                </select>
            </div>
            <div class="form-group col-lg-6" id="attribute_id">
                <label>{{ meta('attribute_id', getCurrentLocale()) ?? __('attributes.attribute_id')}}</label>
                <select id="attribute_id" onchange="attributeSelect(this)" class="form-control {{ $errors->has('attribute_id') ? 'is-invalid' : '' }}" name="attribute_id" >
                    <option  value="">{{__('attributes.select')}}</option>
                    @foreach($attributes as $row)
                        <option {{ (isset($main_attr) ? $main_attr->id : '')  == $row->id ? 'selected' : ''}} value="{{$row->id}}">{{ $row->name }}</option>
                    @endforeach
                </select>
            </div>

            <div class="form-group col-lg-6" id="choices_id">
                <label>{{ meta('choice_id', getCurrentLocale()) ?? __('attributes.choice_id')}}</label>
                <select id="choice_id" class="form-control {{ $errors->has('choice_id') ? 'is-invalid' : '' }}" name="choice_id" >
                    <option  value="">{{__('attributes.select')}}</option>

                </select>
            </div>

--}}
            @foreach($languages as $language)

                <div class="form-group col-lg-6">
                    <label>{{ meta('name', $language->local) ?? __('attributes.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) ?? __('attributes.name') }}" value='{{ old("name_{$language->local}", $attribute[$language->local]->name ?? "") }}'/>
                </div>

            @endforeach

            <div class="col-12">
                    <div  class="row pricing-list-item border-item mb-4" style="background-color: white;height:auto">
                        <div class="col-lg-12">
                            <h3> {{__('attributes.choices')}} </h3>
                            <div class="col-lg-12" style="height:100%;" id="choices">
                                @if($attribute ?? ''  && $attribute->id)
                                    @foreach($attr_choices as $key=>$choice)
                                        <div class="row pricing-list-item border-item mb-4"  style="background-color: rgba(21,97,203,0.1);height:auto">
                                            <div class="form-group col-lg-6">
                                                <label>{{__('attributes.choice') }}</label>
                                                <select name="choices[{{$key}}][id]" class="form-control">
                                                    @foreach($choices as $row)
                                                        <option {{$choice->choice_id == $row->id ? 'selected' : ''}} value="{{$row->id}}">{{$row->currentLangauge->name}}</option>
                                                    @endforeach
                                                </select>
                                            </div>
                                            <div class="form-group col-lg-4"></div>
                                            <div class="form-group col-lg-2" style="font-size: 30px;margin-top:35px;">
                                                <i class="fas fa-times-circle" onclick="remove(this)"></i>
                                            </div>

                                        </div>
                                    @endforeach
                                @endif
                            </div>
                        </div>
                        <div class="col-lg-12">
                            <a style="cursor: pointer;" @if($attribute ?? ''  && $attribute->id) data-id="{{count($choices)}}" @else data-id="0" @endif onclick="addchoice(this)" class="btn_1 gray "><i class="fa fa-fw fa-plus-circle"></i>{{__("attributes.addchoice")}}</a>
                        </div>
                    </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>
    <script>
        function addchoice(elem)
        {
            var num = $(elem).attr('data-id');
            var newrow = parseInt(num)+1;
            $(elem).attr('data-id', newrow);

            $.ajax({
                url : url_addchoice,
                type : 'GET',
                data : 'key='+num,

                success:function(data)
                {
                    $('#choices').append(data);
                }
            })
        }
        function remove(elem){
            $(elem).closest('.row').remove();
        }
        function typeSelect(elem)
        {
            let data = $(elem).val()
            if(data == 'main')
            {
                $('#attribute_id').hide();
                $('#choices_id').hide();
            }
            else
            {
                $('#attribute_id').show();
                $('#choices_id').show();
            }
        }

        function statusSelect(elem)
        {
            let data = $(elem).val()
            if(data == 'one')
            {
                $('#types').hide();
                $('#attribute_id').hide();
                $('#choices_id').hide();
            }
            else
            {
                $('#types').show();
                $('#attribute_id').show();
                $('#choices_id').show();
            }
        }

        function attributeSelect(elem)
        {
            let data = $(elem).val()
            $.ajax({
                url : url_choices_attribute,
                type : 'GET',
                data : 'attribute_id='+data,
                success:function(data)
                {
                    $('#choice_id').html(data);
                }
            })
        }

        $('#status').trigger('change');
        $('#type').trigger('change');
    </script>
    <script>
        let route_prefix = "{{ url(config('lfm.prefix')) }}",
            url = "{{url('/')}}/",
            url_addchoice = {!! json_encode(url(getCurrentLocale().'/admin/attributes/addChoice')) !!},
            url_choices_attribute = {!! json_encode(url(getCurrentLocale().'/admin/attributes/choices')) !!},
            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



