@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.warehouse_id') }} </label>
                <select  id="warehouse_id" class="form-control {{ $errors->has('warehouse_id') ? 'is-invalid' : '' }}" name="warehouse_id">
                    <option value="">{{__('warehouses.main')}}</option>
                    @foreach($warehouses as $row)
                        <option {{ (isset($warehouse) ? $warehouse->warehouse_id : null) == $row->id ? 'selected' : '' }} value="{{$row->id}}">{{$row->currentDescription->title}}</option>
                    @endforeach
                </select>
            </div>

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

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


            <div class="form-group col-lg-6">
                <label>{{ __('warehouses.country_id') }} </label>
                <select onchange="fetchRegions(this)" id="country_id" class="form-control {{ $errors->has('country_id') ? 'is-invalid' : '' }}" name="country_id">
                    <option  value="">{{__('dashboard.select')}}</option>
                    @foreach($countries as $row)
                        <option {{ (isset($warehouse) ? $warehouse->country_id : null) == $row->id ? 'selected' : '' }} value="{{$row->id}}">{{$row->currentDescription->title}}</option>
                    @endforeach
                </select>
            </div>
            <div class="form-group col-lg-6">
                <label>{{ __('warehouses.region_id') }} </label>
                <select onchange="fetchCities(this)" id="region_id" id="region_id" class="form-control {{ $errors->has('region_id') ? 'is-invalid' : '' }}" name="region_id">
                    <option  value="">{{__('dashboard.select')}}</option>
                    @foreach($regions as $row)
                        <option {{ (isset($warehouse) ? $warehouse->region_id : null) == $row->id ? 'selected' : '' }} value="{{$row->id}}">{{$row->currentDescription->title}}</option>
                    @endforeach
                </select>
            </div>
            <div class="form-group col-lg-6">
                <label>{{ __('warehouses.city_id') }} </label>
                <select onchange="fetchAreas(this)"  id="city_id" class="form-control {{ $errors->has('city_id') ? 'is-invalid' : '' }}" name="city_id">
                    <option  value="">{{__('dashboard.select')}}</option>
                    @foreach($cities as $row)
                        <option {{ (isset($warehouse) ? $warehouse->city_id : null) == $row->id ? 'selected' : '' }} value="{{$row->id}}">{{$row->currentDescription->title}}</option>
                    @endforeach
                </select>
            </div>
            <div class="form-group col-lg-6">
                <label>{{ __('warehouses.area_id') }} </label>
                <select  id="area_id" class="form-control {{ $errors->has('area_id') ? 'is-invalid' : '' }}" name="area_id">
                    <option  value="">{{__('dashboard.select')}}</option>
                    @foreach($areas as $row)
                        <option {{ (isset($warehouse) ? $warehouse->area_id : null) == $row->id ? 'selected' : '' }} value="{{$row->id}}">{{$row->currentDescription->title}}</option>
                    @endforeach
                </select>
            </div>

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

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

            @foreach($languages as $language)

                <div class="form-group col-lg-6">
                    <label>{{ meta('title', $language->local) ?? __('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>
                <div class="form-group col-lg-6">
                    <label>{{ meta('address', $language->local) ?? __('warehouses.address') }} {{ count($languages) > 1 ? "({$language->name})" : '' }} </label>
                    <input type="text" class="form-control {{ $errors->has('address_'.$language->local) ? 'is-invalid' : '' }}" name="address_{{ $language->local }}" placeholder="{{ meta('address', $language->local) ?? __('warehouses.address') }}" value='{{ old("address_{$language->local}", $warehouse[$language->local]->address ?? "") }}'/>
                </div>
                <div class="form-group col-lg-12">
                    <label>{{ meta('work_hours', $language->local) ?? __('warehouses.work_hours') }} {{ count($languages) > 1 ? "({$language->name})" : '' }} </label>
                    <textarea  class="form-control {{ $errors->has('work_hours_'.$language->local) ? 'is-invalid' : '' }}" name="work_hours_{{ $language->local }}" placeholder="{{ meta('work_hours', $language->local) ?? __('warehouses.work_hours') }}">{{ old("work_hours_{$language->local}", $warehouse[$language->local]->work_hours ?? "") }}</textarea>
                </div>
                <div class="form-group col-lg-12">
                    <label>{{ meta('description', $language->local) ?? __('warehouses.description') }} {{ count($languages) > 1 ? "({$language->name})" : '' }} </label>
                    <textarea  class="form-control {{ $errors->has('description_'.$language->local) ? 'is-invalid' : '' }}" name="description_{{ $language->local }}" placeholder="{{ meta('description', $language->local) ?? __('warehouses.description') }}">{{ old("description_{$language->local}", $warehouse[$language->local]->description ?? "") }}</textarea>
                </div>

            @endforeach

                <div class="col-md-12">
                    <input type="hidden" name="latitude" id="latitude" value="@if($warehouse ?? ''  && $warehouse->id) {{$warehouse->latitude}} @endif">
                    <input type="hidden" name="longitude" id="longitude" value="@if($warehouse ?? ''  && $warehouse->id) {{$warehouse->longitude}} @endif">
                    <div id="address-canvas">
                        <div id="map" style=" height: 400px;border: 1px solid #000;"></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 orders -->
    <script src="{{ asset('/assets/admin/js/lib/tinymce.min.js') }}"></script>
    <script>

        function fetchRegions(elem){
            let country_id = $(elem).val();
            $.ajax({
                url : url_regions,
                type : 'GET',
                data : 'country_id ='+country_id,
                success:function(data)
                {
                    $('#region_id').html(data);
                }
            })
        }
        function fetchCities(elem){
            let region_id = $(elem).val();
            $.ajax({
                url : url_cities,
                type : 'GET',
                data : 'region_id ='+region_id,
                success:function(data)
                {
                    $('#city_id').html(data);
                }
            })
        }
        function fetchAreas(elem){
            let city_id = $(elem).val();
            $.ajax({
                url : url_areas,
                type : 'GET',
                data : 'city_id ='+city_id,
                success:function(data)
                {
                    $('#area_id').html(data);
                }
            })
        }
    </script>
    <script type="text/javascript">

        window.onload = function() {
            var latlng = new google.maps.LatLng({!! $warehouse ?? ''  && $warehouse->id ? $warehouse->latitude.', '.$warehouse->longitude : '21.4989, 39.2509' !!});
            var map = new google.maps.Map(document.getElementById('map'), {
                center: latlng,
                zoom: 11,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            });
            var marker = new google.maps.Marker({
                position: latlng,
                map: map,
                title: 'Set lat/lon values for this property',
                draggable: true
            });
            google.maps.event.addListener(marker, 'dragend', function(a) {
                console.log(a);
                var div = document.createElement('div');
                let lat = a.latLng.lat().toFixed(4);
                let lan =  a.latLng.lng().toFixed(4);
                document.getElementsByTagName('body')[0].appendChild(div);
                $("#latitude").val(function() {
                    return  lat;
                });
                $("#longitude").val(function() {
                    return  lan;
                });

            });

        };

        function getLocation() {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(showPosition);
            } else {
                x.innerHTML = "Geolocation is not supported by this browser.";
            }
        }

        function showPosition(position) {


            var latlng = new google.maps.LatLng(position.coords.latitude , position.coords.longitude);
            $("#mylatlng").val(function() {
                return  latlng;


            });
            var map = new google.maps.Map(document.getElementById('map'), {
                center: latlng,
                zoom: 11,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            });
            var marker = new google.maps.Marker({
                position: latlng,
                map: map,
                title: 'Set lat/lon values for this property',
                draggable: true
            });
            google.maps.event.addListener(marker, 'dragend', function(a) {
                console.log(a);
                var div = document.createElement('div');alert(div);
                div.innerHTML = a.latLng.lat().toFixed(4) + ', ' + a.latLng.lng().toFixed(4);



            });
        }

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



