@extends('canteen.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($product ?? ''  && $product->id)
                @method('PUT')
            @endif
            @csrf
                @if($product ?? ''  && $product->id)
                    <div class="form-group col-lg-6">
                        <label>{{  __('products.code') }} </label>
                        <p class="form-control" style="height: auto">#{{$product->code}}</p>
                    </div>
                @endif

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



            @foreach($languages as $language)

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

                <div class="form-group col-lg-6">
                    <label>{{ meta('image', getCurrentLocale()) ?? __('products.image') }} </label>
                    <input type="file"  class="form-control {{ $errors->has('image') ? 'is-invalid' : '' }}" name="image" placeholder="{{ meta('image', getCurrentLocale()) ?? __('products.image') }}" value='{{ old("image", $product->image ?? "") }}'/>
                </div>
                @if($product ?? ''  && $product->id)
                    <div class="form-group col-lg-6">
                        <img src="{{asset($product->image)}}">
                    </div>
                @endif
            <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>
        function selectCanteen(elem)
        {
            let id = $(elem).val()
            $.ajax({
                url : url_categories,
                type : 'GET',
                data : 'canteen_id='+id,
                success:function(data)
                {
                    $('#category_id').html(data);
                }
            })
        }

    </script>
    <script>
        let route_prefix = "{{ url(config('lfm.prefix')) }}",
            url = "{{url('/')}}/",
            currentLanguage = "{{getCurrentLocale()}}",
            url_categories = "{{route('admin.fetch.canteen.categories')}}",
            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



