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


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

            <div class="form-group col-lg-6">
                <label>{{ __('categories.category_id') }} </label>
                <select onchange="CategorySelect()" id="parent_id" class="form-control {{ $errors->has('parent_id') ? 'is-invalid' : '' }}" name="parent_id">
                    <option {{ (isset($category) ? $category->parent_id : null) == 'true' ? 'selected' : '' }} value="">{{__('categories.main')}}</option>
                    @foreach($categories as $row)
                        <option {{ (isset($category) ? $category->parent_id : null) == $row->id ? 'selected' : '' }} value="{{$row->id}}">{{$row->currentDescription->title}}</option>
                    @endforeach
                </select>
            </div>

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

            <div class="form-group col-lg-6" id="show_home">
                <label>{{ __('categories.show_home') }} </label>
                <select id="show_home" class="form-control {{ $errors->has('show_home') ? 'is-invalid' : '' }}" name="show_home">
                    @foreach($show_home_statuses as $row)
                        <option {{ (isset($category) ? $category->show_home : null) == $row ? 'selected' : '' }} value="{{$row}}">{{__('categories.show_home_statuses.'.$row)}}</option>
                    @endforeach
                </select>
            </div>
            
            <div class="form-group col-lg-6" id="max_count_order">
                <label>{{ meta('max_count_order', getCurrentLocale()) ?? __('categories.max_count_order') }} </label>
                <input type="number" step=".01" class="form-control {{ $errors->has('max_count_order') ? 'is-invalid' : '' }}" name="max_count_order" placeholder="{{ meta('max_count_order', getCurrentLocale()) ?? __('categories.max_count_order') }}" value='{{ old("max_count_order", $category->max_count_order ?? "") }}'/>
            </div>

            @foreach($languages as $language)

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

            @endforeach

            <div class="form-group col-lg-6">
                <label>{{ __('categories.image') }} </label>
                <input id="old_image" class="old_image" type="hidden" name="old_image" value="{{ $category->image ?? ""}}"/>
                <input type="file" class="form-control {{ $errors->has('image') ? 'is-invalid' : '' }}" name="image" placeholder="{{ __('categories.image') }}" value='{{ old('image', '') }}' accept="image/*"/>
                @if (($category->image ?? null) && ($category->image_url ?? null))
                    <a href="{{ $category->image_url }}" class="btn btn-primary" target="_new"><i class="fas fa-download"></i></a>
                @endif
            </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>

        function addAttribute(elem){
            var num = $(elem).attr('data-id');

            var newrow = parseInt(num)+1;
            $(elem).attr('data-id', newrow);

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

              success:function(data)
              {
                $('#attributes').append(data);
              }
            })
        }
        function remove(elem){
            $(elem).closest('.row').remove();
        }
        function CategorySelect()
        {
            let category_id = $('#category_id').val();
            if(category_id == '')
            {
                $('#max_count_order').show()
                $('#show_footer').show()
                $('#show_home').hide()
            }
            else
            {
                $('#max_count_order').hide()
                $('#show_footer').hide()
                $('#show_home').show()
            }
        }
        $('#category_id').trigger('change');
        function TypeSelect()
        {
            let type_id = $('#type_id').val();
             $.ajax({
              url : url_type_change,
              type : 'GET',
              data : 'type_id='+type_id,

              success:function(data)
              {
                $('#category_id').html(data);
              }
            })
            
        }
        $('#category_id').trigger('change');
    </script>
@endsection



