@extends('admin.layouts.container')

@section('content')
<div class="side-title">
    <h3>{{ $head ?? '' }}</h3>
</div>

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

<div class="form-group col-lg-12">
            <label>{{ meta('role', getCurrentLocale()) ??  __('members.role') }} </label>
            <div class="clearfix"></div>
            <div class="row">
               
                <div class="form-group col-sm-6">
                   
                    <div class="custom-control custom-checkbox">
                        <select  class="form-control {{ $errors->has('role') ? 'is-invalid' : '' }}" onchange="changetype(this)" name="role">
                         <option @if($member ?? ''  && $member->id) @if($member->role == 'member') selected @endif @endif  value="member">{{__('members.member')}}</option>
                         <option @if($member ?? ''  && $member->id) @if($member->role == 'consult') selected @endif @endif  value="consult">{{__('members.consult')}}</option>
                          </select>
                        </div>
                </div>
               
            </div>
        </div>
        <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 ?? "") }}'/>
        </div>

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

        {{--Start image container--}}
        <div class="form-group col-lg-6">
            <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="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 id="member" class="col-12"  @if($member ?? ''  && $member->id)  @if( $member->role == 'member') style="display:block" @else style="display:none" @endif @else style="display:block" @endif>
        <div class="row">
        <div class="form-group col-lg-6">
            <label>{{ meta('relative', getCurrentLocale()) ?? __('members.relative') }} </label>
            <input type="text" class="form-control {{ $errors->has('relative') ? 'is-invalid' : '' }}" name="relative" placeholder="{{ meta('relative', getCurrentLocale()) ?? __('members.relative') }}" value='{{ old("relative", $member->relative ?? "") }}'/>
        </div>
        <div class="form-group col-lg-6">
            <label>{{ meta('age', getCurrentLocale()) ?? __('members.age') }} </label>
            <input type="number" class="form-control {{ $errors->has('age') ? 'is-invalid' : '' }}" name="age" placeholder="{{ meta('age', getCurrentLocale()) ?? __('members.age') }}" value='{{ old("age", $member->age ?? "") }}'/>
        </div>
        <div class="form-group col-sm-6">
                    <div class="custom-control custom-checkbox">
                        <label>{{__('members.type')}}</label>
                        <select  class="form-control {{ $errors->has('role') ? 'is-invalid' : '' }}" name="type_id">
                            <option selected disabled value="">{{__('members.select')}}</option>
                            @foreach($types as $row)
                         <option @if($member ?? ''  && $member->id) @if($member->type_id == $row->id) selected @endif @endif  value="{{$row->id}}">{{typename($row->id)}}</option>
                        @endforeach
                          </select>
                        </div>
                </div>
          
        </div>
         </div>
         <div id="consult" class="col-12"  @if($member ?? ''  && $member->id)  @if( $member->role == 'consult') style="display:block" @else style="display:none" @endif @else style="display:none" @endif>
        <div class="row">
            
        <div class="form-group col-lg-6">
            <label>{{ meta('bank_acc', getCurrentLocale()) ?? __('members.bank_acc') }} </label>
            <input type="text" class="form-control {{ $errors->has('bank_acc') ? 'is-invalid' : '' }}" name="bank_acc" placeholder="{{ meta('bank_acc', getCurrentLocale()) ?? __('members.bank_acc') }}" value='{{ old("bank_acc", $member->bank_acc ?? "") }}'/>
        </div>
        <div class="form-group col-lg-6">
            <label>{{ meta('iban', getCurrentLocale()) ?? __('members.iban') }} </label>
            <input type="text" class="form-control {{ $errors->has('iban') ? 'is-invalid' : '' }}" name="iban" placeholder="{{ meta('iban', getCurrentLocale()) ?? __('members.iban') }}" value='{{ old("iban", $member->iban ?? "") }}'/>
        </div>
        <div class="form-group col-lg-6">
            <label>{{ meta('bank', getCurrentLocale()) ?? __('members.bank') }} </label>
            <input type="text" class="form-control {{ $errors->has('bank') ? 'is-invalid' : '' }}" name="bank" placeholder="{{ meta('bank', getCurrentLocale()) ?? __('members.bank') }}" value='{{ old("bank", $member->bank ?? "") }}'/>
        </div>
        <div class="form-group col-lg-6">
            <label>{{ meta('cost', getCurrentLocale()) ?? __('members.cost') }} </label>
            <input type="number" class="form-control {{ $errors->has('cost') ? 'is-invalid' : '' }}" name="cost" placeholder="{{ meta('cost', getCurrentLocale()) ?? __('members.cost') }}" value='{{ old("cost", $member->cost ?? "") }}'/>
        </div>
        @if($member ?? ''  && $member->id)
        <div class="form-group col-lg-6">
            <label>{{ meta('wallet', getCurrentLocale()) ?? __('members.wallet') }} </label>
            <input disabled class="form-control" placeholder="{{ meta('wallet', getCurrentLocale()) ?? __('members.wallet') }}" value='{{ old("wallet", $member->wallet ?? "") }}'/>
        </div>
        @endif
        
        <div class="form-group col-lg-6">
            <label>{{ meta('country', getCurrentLocale()) ?? __('members.country') }} </label>
            <select  class="form-control {{ $errors->has('country_id') ? 'is-invalid' : '' }}" name="country_id">
                <option disabled selected>{{__('members.select')}}</option>
                @foreach($countries as $row)
                         <option @if($member ?? ''  && $member->id) @if($member->country_id == $row->id) selected @endif @endif  value="{{$row->id}}">{{countryname($row->id)}}</option>
                         @endforeach
                          </select>
                        </div>
        
         <div class="form-group col-sm-6">
                    <div class="custom-control custom-checkbox">
                        <label>{{__('members.gender')}}</label>
                        <select  class="form-control {{ $errors->has('gender') ? 'is-invalid' : '' }}" name="gender">
                            <option selected disabled value="">{{__('members.select')}}</option>
                           <option @if($member ?? ''  && $member->id) @if($member->gender == '1') selected @endif @endif  value="1">{{__('members.male')}}</option>
                         <option @if($member ?? ''  && $member->id) @if($member->gender == '0') selected @endif @endif  value="0">{{__('members.female')}}</option>
                         
                          </select>
                        </div>
                </div>
                
                 <div class="form-group col-sm-6">
                    <div class="custom-control custom-checkbox">
                        <label>{{__('members.category')}}</label>
                        <select  class="form-control {{ $errors->has('category_id') ? 'is-invalid' : '' }}" name="category_id">
                            <option selected disabled value="">{{__('members.select')}}</option>
                            @foreach($categories as $row)
                         <option @if($member ?? ''  && $member->id) @if($member->category_id == $row->id) selected @endif @endif  value="{{$row->id}}">{{categoryname($row->id)}}</option>
                        @endforeach
                          </select>
                        </div>
                </div>
                
               
        
              <div class="col-12">
        <div class="row" id="experiences">
            <div class="col-12">
                <h5>{{__('members.experiences')}}</h5>
            </div>
             @if($member ?? ''  && $member->id)
             <div class="col-12">
            @foreach($experiences as $key=>$row)
        <div class="row">
            
          <input type="hidden"  name="experience[{{$key}}][id]"value="{{$row->id}}">
          <input type="hidden"  name="experiences[]"value="{{$row->id}}">
           @foreach(languages() as $language)
        <div class="form-group col-lg-5">
            <label>الخبرات {{$language->local}}</label>
            <input type="text" class="form-control" name="experience[{{$key}}][name_{{$language->local}}]" placeholder="الخبرات {{$language->local}}" value="{{experincenamelang($row->id,$language->id)}}" />
            
        </div>
        @endforeach
        
        
        <div class="form-group col-lg-2" style="font-size: 30px;margin-top:35px;">
    	<i class="fas fa-times-circle" onclick="remove(this)"></i></div>
    	<div class="col-lg-12">
                <div class="form-group">
                    <div class="input_img">

                        <img src="{{asset($row->image)}}">
                       
                        <div class="icons">
                           
                            <a href="{{asset($row->image)}}" class="link" data-fancybox="">
                                <i class="far fa-eye text-warning"></i>
                            </a>
                            <a href="#0" class="edit_btn">
                                <i class="fas fa-pencil-alt text-info"></i>
                            </a>
                        </div>
                    </div>
                    <div class="box">
                        <div class="js--image-preview"></div>
                        <div class="upload-options">
                            <label>
                            <input type="file" name="image" class="image-upload" />
                          </label>
                        </div>
                    </div>
                </div>
            </div>
    	</div>
        
        
             
        @endforeach
        </div>
        @endif
        </div>
        </div>
          <div class="col-12">
              <a style="cursor: pointer;" @if($member ?? ''  && $member->id)  data-id="{{count($experiences)}}" @else data-id="0" @endif   onclick="addexperience(this)" class="btn_1 gray "><i class="fa fa-fw fa-plus-circle"></i>{{__("members.addexperience")}}</a>
       
          </div>
         <div class="col-12">
        <div class="row" id="certifications">
             <div class="col-12">
                <h5>{{__('members.certifications')}}</h5>
            </div>
               @if($member ?? ''  && $member->id)
               <div class="col-12">
            @foreach($certifications as $key=>$row)
        <div class="row">
         
          <input type="hidden"  name="certification[{{$key}}][id]"value="{{$row->id}}">
          <input type="hidden"  name="certifications[]"value="{{$row->id}}">
        @foreach(languages() as $language)
        <div class="form-group col-lg-5">
            <label>الشهادات {{$language->local}}</label>
            <input type="text" class="form-control" name="certification[{{$key}}][name_{{$language->local}}]" placeholder="الشهادات {{$row->local}}" value="{{certificationnamelang($row->id,$language->id)}}" />
            
        </div>
        @endforeach
        
        
        <div class="form-group col-lg-2" style="font-size: 30px;margin-top:35px;">
    	<i class="fas fa-times-circle" onclick="remove(this)"></i></div>
    	
    	 <div class="col-lg-12">
                <div class="form-group">
                    <div class="input_img">
                       
                        <img src="{{asset($row->image)}}">
                        
                        <div class="icons">
                            
                            <a href="{{asset($row->image)}}" class="link" data-fancybox="">
                                <i class="far fa-eye text-warning"></i>
                            </a>
                            <a href="#0" class="edit_btn">
                                <i class="fas fa-pencil-alt text-info"></i>
                            </a>
                        </div>
                    </div>
                    <div class="box">
                        <div class="js--image-preview"></div>
                        <div class="upload-options">
                            <label>
                            <input type="file" name="image" class="image-upload" />
                          </label>
                        </div>
                    </div>
                </div>
            </div>
            </div>
        @endforeach
        </div>
        @endif
        </div>
        </div>
         <div class="col-12">
                <a style="cursor: pointer;" @if($member ?? ''  && $member->id)  data-id="{{count($certifications)}}" @else data-id="0" @endif   onclick="addcertification(this)" class="btn_1 gray "><i class="fa fa-fw fa-plus-circle"></i>{{__("members.addcertification")}}</a>
       
          </div>
        
        </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')
<script>



function addexperience(elem){
    var num = $(elem).attr('data-id');
        
        var newrow = parseInt(num)+1; 
        $(elem).attr('data-id', newrow);
        
        $.ajax({
          url : urls_exper+'/'+num,
          type : 'GET',
          data : 'nothing',
          success:function(data)
          {
            $('#experiences').append(data);
          }
        }) 
}

function remove(elem){
            $(elem).closest('.row').remove();
        }
function addcertification(elem){
     var num = $(elem).attr('data-id');
        
        var newrow = parseInt(num)+1; 
        $(elem).attr('data-id', newrow);
        
        $.ajax({
          url : urls+'/'+num,
          type : 'GET',
          data : 'nothing',
          success:function(data)
          {
            $('#certifications').append(data);
          }
        }) 
}
    function changetype(elem){
        let valuedata = $(elem).val();
        if(valuedata == 'member'){
            $('#member').css('display','block');
            $('#consult').css('display','none');
        }
        else{
            $('#consult').css('display','block');
            $('#member').css('display','none');
        }
        

    }
</script>
  <script>
        function initImageUpload(box) {
            let uploadField = box.querySelector('.image-upload');

            uploadField.addEventListener('change', getFile);

            function getFile(e) {
                let file = e.currentTarget.files[0];
                checkType(file);
            }

            function previewImage(file) {
                let thumb = box.querySelector('.js--image-preview'),
                    reader = new FileReader();

                reader.onload = function() {
                    thumb.style.backgroundImage = 'url(' + reader.result + ')';
                }
                reader.readAsDataURL(file);
                thumb.className += ' js--no-default';
            }

            function checkType(file) {
                let imageType = /image.*/;
                if (!file.type.match(imageType)) {
                    throw 'Datei ist kein Bild';
                } else if (!file) {
                    throw 'Kein Bild gew채hlt';
                } else {
                    previewImage(file);
                }
            }

        }

        // initialize box-scope
        var boxes = document.querySelectorAll('.box');

        for (let i = 0; i < boxes.length; i++) {
            let box = boxes[i];
            initDropEffect(box);
            initImageUpload(box);
        }



        /// drop-effect
        function initDropEffect(box) {
            let area, drop, areaWidth, areaHeight, maxDistance, dropWidth, dropHeight, x, y;

            // get clickable area for drop effect
            area = box.querySelector('.js--image-preview');
            area.addEventListener('click', fireRipple);

            function fireRipple(e) {
                area = e.currentTarget
                    // create drop
                if (!drop) {
                    drop = document.createElement('span');
                    drop.className = 'drop';
                    this.appendChild(drop);
                }
                // reset animate class
                drop.className = 'drop';

                // calculate dimensions of area (longest side)
                areaWidth = getComputedStyle(this, null).getPropertyValue("width");
                areaHeight = getComputedStyle(this, null).getPropertyValue("height");
                maxDistance = Math.max(parseInt(areaWidth, 10), parseInt(areaHeight, 10));

                // set drop dimensions to fill area
                drop.style.width = maxDistance + 'px';
                drop.style.height = maxDistance + 'px';

                // calculate dimensions of drop
                dropWidth = getComputedStyle(this, null).getPropertyValue("width");
                dropHeight = getComputedStyle(this, null).getPropertyValue("height");

                // calculate relative coordinates of click
                // logic: click coordinates relative to page - parent's position relative to page - half of self height/width to make it controllable from the center
                x = e.pageX - this.offsetLeft - (parseInt(dropWidth, 10) / 2);
                y = e.pageY - this.offsetTop - (parseInt(dropHeight, 10) / 2) - 30;

                // position drop and animate
                drop.style.top = y + 'px';
                drop.style.left = x + 'px';
                drop.className += ' animate';
                e.stopPropagation();

            }
        }
    </script>
    	
    <script>
        $(".form-group").delegate( ".edit_btn", "click", function() {
            $(this).parentsUntil(".form-group").siblings(".box").toggleClass("d-block");
        })
    </script>
    <!-- Editor JS files -->
{{--    <script src="{{ asset('/assets/admin/js/lib/tinymce.min.js') }}"></script>--}}
            <script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
    <script>
        var route_prefix = "{{ url(config('lfm.prefix')) }}",
            url = "{{url('/')}}",
            urls = "{{url('/')}}/admin/addcertification",
            urls_exper = "{{url('/')}}/admin/addexperience",
            imagePath = "{{asset('')}}";
    </script>
    <script src="{{ asset('assets/admin/js/editor.js') }}"></script>
    <script src="{{ asset('assets/admin/js/lfm.js') }}"></script>
    


@endsection
