@extends('admin.layouts.container')

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

<div class="border-item">
    <form class="form-row" enctype="multipart/form-data" method="post" action="{{ $action }}">
        @if($code ?? ''  && $code->id)
            @method('PUT')
        @endif
        @CSRF
         
         
               
                 
        <div class="form-group col-lg-6">
            <label>{{ meta('name', getCurrentLocale()) ?? __('calories.name') }} </label>
            <input type="text" class="form-control {{ $errors->has('name') ? 'is-invalid' : '' }}" name="name" placeholder="{{ meta('name', getCurrentLocale()) ??__('calories.name') }}" value='{{ old("name", $code->name ?? "") }}'/>
        </div>
       <div class="row col-12 pricing-list-item border-item mb-4 parent_container" style="width:100%" id="meals">
            @foreach($codes as $data=>$row)
             <div class="row col-12 pricing-list-item border-item mb-4">
          <div class="form-group col-lg-6">
            <label>الكود </label>
            <input class="form-control" name="code[{{$data}}][name]" placeholder="الكود" value="{{$row->name}}">
          
        </div>

        
         
        <div class="form-group col-lg-6" style="font-size: 30px;margin-top:35px;">
    	<i class="fas fa-times-circle" onclick="remove(this)"></i></div>
    	<div class="form-group col-lg-12">
            <label>الوجبة </label>
             <textarea class="form-control my-editor parent_container" name="code[{{$data}}][description]" >{{$row->description}}</textarea>
          
                            </div>
                            </div>
            @endforeach
        </div>
<div class="form-group col-lg-12">
                   <a style="cursor: pointer;" @if($code ?? ''  && $code->id) data-id="{{count($codes)}}" @else data-id="0" @endif onclick="addmealtype(this)" class="btn_1 gray "><i class="fa fa-fw fa-plus-circle"></i>اضافة كود </a>
                   
              </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 src="{{ asset('/assets/admin/js/lib/tinymce.min.js') }}"></script>

<script>
$('.append_link').click(function (e) {
            e.preventDefault();
            
        })
        


    function addmealtype(elem){
        var num = $(elem).attr('data-id');
        var code = ''
        $('.parent_container').append(`
        <div class="row col-12 pricing-list-item border-item mb-4">
          <div class="form-group col-lg-6">
            <label>الكود </label>
            <input class="form-control" name="code[`+num+`][name]" placeholder="الكود">
          
        </div>
        <div class="form-group col-lg-6" style="font-size: 30px;margin-top:35px;">
    	<i class="fas fa-times-circle" onclick="remove(this)"></i></div>
       
         <div class="form-group col-lg-12">
          <label>الوجبة </label>
                <textarea name="code[`+num+`][description]" class="appended_text_area"></textarea>
                </div>
               </div>
            `);
            tinymce.init({
                selector: '.appended_text_area',
                plugins: 'image code',
                toolbar: 'undo redo | link image | code',
                image_title: true,
                automatic_uploads: true,
                file_picker_types: 'image',
                file_picker_callback: function (cb, value, meta) {
    var input = document.createElement('input');
    input.setAttribute('type', 'file');
    input.setAttribute('accept', 'image/*');

   

    input.onchange = function () {
      var file = this.files[0];

      var reader = new FileReader();
      reader.onload = function () {
       
        var id = 'blobid' + (new Date()).getTime();
        var blobCache =  tinymce.activeEditor.editorUpload.blobCache;
        var base64 = reader.result.split(',')[1];
        var blobInfo = blobCache.create(id, file, base64);
        blobCache.add(blobInfo);

        /* call the callback and populate the Title field with the file name */
        cb(blobInfo.blobUri(), { title: file.name });
      };
      reader.readAsDataURL(file);
    };

    input.click();
  },
                init_instance_callback: function (editor) {
                    editor.on('click', function (e) {
                        console.log('Element clicked:', e.target.nodeName);
                    });
                }
            });
            

        
        
        var newrow = parseInt(num)+1; 
        $(elem).attr('data-id', newrow);
        
       
    }
     function remove(elem){
            $(elem).closest('.row').remove();
        }
</script>
    <!-- Editor JS files -->
   
          
    <script>
        let route_prefix = "{{ url(config('lfm.prefix')) }}",
            url = "{{url('/')}}/",
            urls = "{{url('/')}}/admin/addmealtype",
            imagePath = "{{asset('')}}";
    </script>
    <script src="{{ asset('assets/admin/js/editor.js') }}"></script>
    <script src="{{ asset('assets/admin/js/lfm.js') }}"></script>
@endsection
