@extends('admin.layouts.container')
@section('css')
    <link rel="stylesheet" href="{{ asset('assets/web/css/lib/toastr.css') }}" />
@endsection
@section('content')
    <div class="side-title">
        <h3>{{ $head ?? '' }}</h3>
    </div>

    <div class="border-item">
        <form class="form-row add_member_form" method="post" action="{{ $action }}">
            @if($member ?? ''  && $member->id)
                @method('PUT')
            @endif
            @csrf

            <div class="form-group col-lg-6">
                <label>{{ meta('name', getCurrentLocale()) ?? __('members.name') }} </label>
                <input type="text" class="form-control {{ $errors->has('name') ? 'is-invalid' : '' }}" name="name" placeholder="{{ meta('name', getCurrentLocale()) ??__('members.name') }}" value='{{ old("name", $member->name ?? "") }}'/>
            </div>
                <div class="form-group col-lg-6">
                    <label>{{ meta('email', getCurrentLocale()) ?? __('members.email') }} </label>
                    <input type="text" class="form-control {{ $errors->has('email') ? 'is-invalid' : '' }}" name="email" placeholder="{{ meta('email', getCurrentLocale()) ?? __('members.email') }}" value='{{ old("email", $member->email ?? "") }}' {{ ($member->email ?? null)}}/>
                </div>
                <div class="form-group col-lg-6">
                    <label>{{ meta('phone', getCurrentLocale()) ?? __('members.phone') }} </label>
                    <input type="text" class="form-control {{ $errors->has('phone') ? 'is-invalid' : '' }}" name="phone" placeholder="{{ meta('phone', getCurrentLocale()) ?? __('members.phone') }}" value='{{ old("phone", $member->phone ?? "") }}'/>
                </div>
            <div class="form-group col-lg-6">
                <label>{{ meta('gender', getCurrentLocale()) ?? __('members.gender') }} </label>
                <select name="gender" class="form-control {{ $errors->has('gender') ? 'is-invalid' : '' }}">
                    <option value="">{{__('members.gender')}}</option>
                    <option {{old('gender', $member->gender ?? null) == 'male' ? 'selected' : '' }} value="male">{{__('members.male')}}</option>
                    <option {{old('gender', $member->gender ?? null) == 'female' ? 'selected' : '' }} value="female">{{__('members.female')}}</option>
                </select>
            </div>

            <div class="form-group col-lg-6">
                <label>{{ meta('country_id', getCurrentLocale()) ?? __('states.country_id') }} </label>
                <select name="country_id" class="form-control {{ $errors->has('country_id') ? 'is-invalid' : '' }}">
                    <option value="">{{ meta('country_id', getCurrentLocale()) ?? __('members.country_id') }}</option>
                    @if(count($countries))
                        @foreach($countries as $country)
                            <option {{old('country_id', $state->country_id ?? null) == $country->id ? 'selected' : ''}} value="{{$country->id}}">{{$country->name}}</option>
                        @endforeach
                    @endif
                </select>
            </div>

            <div class="form-group col-lg-6">
                <label>{{ meta('state_id', getCurrentLocale()) ?? __('states.state_id') }} </label>
                <select name="state_id" class="form-control {{ $errors->has('state_id') ? 'is-invalid' : '' }}">
                    <option value="">{{ meta('state_id', getCurrentLocale()) ?? __('members.state_id') }}</option>
                </select>
            </div>
            <div class="form-group col-lg-6">
                <label>{{ meta('city', getCurrentLocale()) ?? __('members.city') }} </label>
                <input type="text" class="form-control {{ $errors->has('city') ? 'is-invalid' : '' }}" name="city" placeholder="{{ meta('city', getCurrentLocale()) ?? __('members.city') }}" value='{{ old("city", $member->city ?? "") }}'/>
            </div>

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

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

                <div class="form-group col-lg-6">
                    <label>{{ meta('status', getCurrentLocale()) ?? __('members.status') }} </label>
                    <select name="status" class="form-control {{ $errors->has('status') ? 'is-invalid' : '' }}">
                        <option selected value="">{{__('members.status')}}</option>
                        <option {{old('status', $member->status ?? null) == 1 ? 'selected' : '' }} value="1">{{__('members.active')}}</option>
                        <option {{old('status', $member->status ?? null) == '0' ? 'selected' : '' }} value="0">{{__('members.non_active')}}</option>
                    </select>
                </div>

            {{--Start image container--}}
            <div class="form-group col-lg-12">
                <label>{{ meta('photo', getCurrentLocale()) ?? __('members.photo') }} </label>
                <input id="image_thumbnail" class="image_thumbnail" type="hidden" name="photo" value="{{old("photo", $member->photo ?? "")}}"/>

                <div class="photos main_photo {{ old('photo', $member->photo ?? '') ? ' wow fadeInUp' : 'hide' }}">
                    <div class="col-lg-4">
                        <div class="items">
                            <div class="img">
                                <a href="#" id="lfm" data-input=".image_thumbnail" data-preview=".image_holder" class="lfm" data-hide=".pick_button">
                                    <img src="{{asset(old('photo', $member->photo ?? ''))}} "  id="image_holder" class="image_holder"/>
                                </a>
                            </div>
                            <a href="#" class="showHide resetImage close" data-empty=".image_thumbnail" data-hide=".main_photo" data-show=".pick_button"><i class="fas fa-times"></i></a>
                        </div>
                    </div>
                </div>

                <div class="clearfix"></div>
                <span class="input-group-btn pick_button {{ old('photo', $member->photo ?? '') ? 'hide' : '' }} ">
                <a id="lfm" data-input=".image_thumbnail" data-preview=".image_holder" data-show=".main_photo" data-hide=".pick_button" class="lfm btn btn-primary text-light">
                    <i class="fas fa-camera-retro"></i> Choose
                </a>
            </span>
            </div>
            {{--End image container--}}


            <div class="text-center mt-5 col-12">
                <button class="butn add_member_button"><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 src="{{ asset('/assets/admin/js/lib/tinymce.min.js') }}"></script>
    {{--    <script src="//cdn.tinymce.com/4/tinymce.min.js"></script>--}}
    {{--    <script src="https://cdn.tiny.cloud/1/cvo4dn512c81ken3vhruwrooatptt4zew0lyb0ey4pas8mp1/tinymce/5/tinymce.min.js" referrerpolicy="origin"></script>--}}
    <script>
        let route_prefix = "{{ url(config('lfm.prefix')) }}",
            url = "{{url('/')}}/",
            imagePath = "{{asset('')}}",
            getStateRoute ="{{route('members.getState')}}";
    </script>
    <script src="{{ asset('assets/admin/js/editor.js') }}"></script>
    <script src="{{ asset('assets/admin/js/lfm.js') }}"></script>
    <script src="{{ asset('assets/web/js/lib/toastr.min.js') }}"></script>


    <script>
        let selectOption = $('select[name="role"] option:selected').val();
        if (selectOption == 'user'){
            $('.instructor_container').addClass('d-none');
            $('.instructor_container input').val('');
            $('.city_container').removeClass('d-none');
        } else {
            $('.instructor_container').removeClass('d-none');
            $('.city_container').addClass('d-none');
            $('.city_container input').val('');
        }
        $('select[name="role"]').change(function () {
            if($(this).val() === 'user'){
                $('.instructor_container').addClass('d-none');
                $('.instructor_container input').val('');
                $('.city_container').removeClass('d-none');
            } else {
                $('.instructor_container').removeClass('d-none');
                $('.city_container').addClass('d-none');
                $('.city_container input').val('');
            }
        });

        $('.add_member_button').click(async function (e) {
            e.preventDefault();

            let form = $('.add_member_form'),
                data = new FormData(form[0]),
                url = form.attr('action'),
                method = form.attr('method');

            const response=await ajax({
                url,
                data,
                dataType: 'json',
                method
            });

            if (response.url){
                window.location.href = response.url;
            }

            if (response.errors){
                toasterError(response.errors);
            }


        });

        $('select[name="country_id"]').change(async function (e) {
            e.preventDefault();

            let data = {country_id: $(this).val()},
                url = getStateRoute,
                method = 'get';

            const response=await ajax({
                url,
                data,
                dataType: 'json',
                method
            });

            if (response.states){
                $('select[name="state_id"]').html(`<option value="">{{ meta('state_id', getCurrentLocale()) ?? __('members.state_id') }}</option>`);
                for(let i = 0; i < response.states.length; i++){
                    $('select[name="state_id"]').append(`<option value="`+response.states[i].id+`">`+response.states[i].name+`</option>`)
                }
            }
        });

    </script>

@endsection
