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

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

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

            @foreach($languages as $language)

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

            @endforeach

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

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

            <div class="form-group col-lg-6">
                <label>{{ __('warehouses.country_id') }} </label>
                <select onchange="selectCountry(this)" class="form-control {{ $errors->has('country_id') ? 'is-invalid' : '' }}" name="country_id">
                    <option value="">{{__('dashboard.select')}}</option>
                    @foreach($countries as $row)
                        <option {{ (isset($city) ? $city->region->country_id : null) == $row->id ? 'selected' : '' }} value="{{$row->id}}">{{$row->title}}</option>
                    @endforeach
                </select>
            </div>

            <div class="form-group col-lg-12">
                <h4>{{ __('warehouses.region_id') }} </h4>
            </div>

            <div class="row col-lg-12" id="regions">
                @include('admin.warehouses.regions',['regions'=>$regions,'region_ids'=>$region_ids])
            </div>
            <div class="row col-lg-12" id="cities">
                @include('admin.warehouses.cities',['cities'=>$cities,'city_ids'=>$city_ids])
            </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>

    <script>
        function selectCountry(elem)
        {
            let country_id = $(elem).val()
            $.ajax({
                url : url_regions,
                type : 'GET',
                data : 'country_id='+country_id,
                success:function(data)
                {
                    $('#regions').html(data);
                }
            })
        }
        function regionSelect()
        {
            let region_id = [];
            $("input:checkbox[name='region_id[]']:checked").each(function (index, obj) {
                region_id.push(obj.value);
            });
            let city_id = [];
            $("input:checkbox[name='city_id[]']:checked").each(function (index, obj) {
                city_id.push(obj.value);
            });
            $.ajax({
                url : url_cities,
                type : 'GET',
                data : 'region_id='+region_id+'&city_id='+city_id,
                success:function(data)
                {
                    $('#cities').html(data);
                }
            })
        }
    </script>

    <script>
        let route_prefix = "{{ url(config('lfm.prefix')) }}",
            url = "{{url('/')}}/",
            url_regions = "{{route('admin.fetch.regions.warehouses')}}",
            url_cities = "{{route('admin.fetch.cities.warehouses')}}",
            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



