<x-app-layout>
    {{-- <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            {{ __('Dashboard') }}
        </h2>
    </x-slot> --}}
    <div class="py-4">
        <div class="container-fluid">
            @if ($errors->any())
                <div class="alert alert-danger">
                    <ul>
                        @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif
            <div class="app-content content">
                <div class="details-wrapper">
                    <div class="content-detached content-right">
                    <div class="content-body">
                        <section id="configuration">
                            <div class="row">
                            <div class="col-12">
                            <nav aria-label="breadcrumb ">
                          <ol class="breadcrumb bg-white">
                            <li class="breadcrumb-item"><a href="{{ url('news/list/Published') }}">News & Developments</a></li>
                            <li class="breadcrumb-item active" aria-current="page">Edit</li>
                          </ol>
                        </nav>
                        </div></div>
                        <div class="row">
                            <div class="col-12">
                                <div class="card">
                                <form method="POST" action="{{ route('newslist.update',$news->id) }}" class="edit-form" id ="editnews" enctype="multipart/form-data">
                                    @csrf
                                    @method('PUT')
                                        <div class="card-header">
                                            <h4 class="card-title">Edit News </h4>
                                            <button class="btn btn-primary" type="button" id="editnewsbutton" onclick="updateNews();">Update </button>
                                        </div>
                                        <div class="card-content collapse show">
                                            <div class="card-body card-dashboard">
                                                <div class="tab-content form-content" id="edit-tabcontent">

                                                    <div class="tab-pane fade" id="overview" role="tabpanel" aria-labelledby="overview-tab">
                                                    </div>		
                                                    
                                                    <div class="tab-pane fade active show" id="club-details" role="tabpanel" aria-labelledby="club-details-tab">
                                                        <div class="flex-rowblock">
                                                            <div class="left-flexbox">
                                                                <div class="row">
                                                                    <div class="col-md-4 col-12">
                                                                        <label class="control-label"> Title<span class="text-danger">*</span></label>
                                                                        <input type="text" name="title" class="form-control" placeholder="" value="{{$news->title}}" required autofocus>
                                                                    </div>
                                                                    <div class="col-md-4 col-12">
                                                                        <label class="control-label">Author</label>
                                                                        <input type="text" name="author" class="form-control" placeholder="" value="{{$news->author}}">
                                                                    </div>
                                                                    <div class="col-md-4 col-12">
                                                                        <label class="control-label">Display Date</label>
                                                                        <input type="date" name="date" class="form-control" placeholder="" value="{{$news->date}}">
                                                                    </div>
                                                                    <div class="col-12">
                                                                    <div class="form-group" id="txtArea" style="display:none;">
                                                                     <label class="control-label">Description<span class="text-danger">*</span></label>
                                                                      <textarea style="height:auto;" name="description" id="description" class="form-control autoExpand">{{$news->description}}</textarea>
                                                                    </div>
                                                                      <input  class="ml10" type="hidden" name="hascontent" id="hascontent" value="@if( $news->description != '' ) 1 @endif" />
                                                                      <input class="ml10" type="hidden" name="editorcount" id="editorcount" value="1" />
                                                                          <p id="pretxt" class="alert alert-warning tc">Loading Content Editor ...</p>
                                                                    </div>
                                                                    <div class="col-md-3 col-sm-4">
                                                                        <label class="control-label">Add Image</label>
                                                                        <div class="upload-imgblock">
                                                                            <div class="upload-field">
                                                                                @if( $news->image_path != '' )
                                                                                    <img style="height:100%; object-fit:contain;" class="" src="{{$news->image_path}}" id="userimage">
                                                                                @else
                                                                                     <img style="height:100%; object-fit:contain;" class="" id="userimage" src="{!! asset('images/upload-img.png') !!}">
                                                                                @endif
                                                                                <div class="imgControls">
                                                                                    <div class="position-relative uploadImg">
                                                                                        <a href="{{ url('crop/news')}}" name="upload-img" id="upload-img" data-toggle="modal"  class="aupload btn btn-upload">
                                                                                        <span id="uploadimg"><i class='fas fa-upload'></i></span>
                                                                                    </a>
                                                                                    </div>
                                                                                    <div class="remove-icon" onclick="removeImage()" id="removeimg" style="display: none;">
                                                                                        <i class="fas fa-times icon-img"></i>
                                                                                    </div>
                                                                                    @if($news->image_path != '')
                                                                                    <div class="remove-icon" onclick="removeImage()" id="remove-img">
                                                                                        <i class="fas fa-times icon-img"></i>
                                                                                    </div>
                                                                                    @endif
                                                                                </div>
                                                                            </div>
                                                                        </div>
                                                                        <span class="doc-up-note"><strong>Note:</strong> Please upload an image of size greater than <sab>800px X 400px</sab>. You can upload only  .jpg, png, jpeg files.</span>
                                                                        <input type="hidden" id="tempimage" name="tempimage" value="">
                                                                    </div>
                                                                    <div class="col-md-3 col-sm-4">
                                                                        <label class="control-label">Add Thumbnail</label>
                                                                        <div class="upload-imgblock">
                                                                            <div class="upload-field">
                                                                                @if( $news->thumbnail_path != '' )
                                                                                    <img style="height:100%; object-fit:contain;" class="" src="{{$news->thumbnail_path}}" id="thumbnailimage">
                                                                                @else
                                                                                     <img style="height:100%; object-fit:contain;" class="" id="thumbnailimage" src="{!! asset('images/upload-img.png') !!}">
                                                                                @endif
                                                                                <div class="imgControls">
                                                                                    <div class="position-relative uploadImg">
                                                                                        <a href="{{ url('crop/thumbnail')}}" name="upload-thumbnail" id="upload-thumbnail" data-toggle="modal"  class="aupload btn btn-upload">
                                                                                        <span id="uploadthumbnail"><i class='fas fa-upload'></i></span>
                                                                                    </a>
                                                                                    </div>
                                                                                    <div class="remove-icon" onclick="removeThumbnail()" id="removethumbnail" style="display: none;">
                                                                                        <i class="fas fa-times icon-img"></i>
                                                                                    </div>
                                                                                    @if($news->thumbnail_path != '')
                                                                                    <div class="remove-icon" onclick="removeThumbnail()" id="remove-thumbnail">
                                                                                        <i class="fas fa-times icon-img"></i>
                                                                                    </div>
                                                                                    @endif
                                                                                </div>
                                                                            </div>
                                                                        </div>
                                                                        <span class="doc-up-note"><strong>Note:</strong> Please upload an image of size greater than <sab>240px X 240px</sab>. You can upload only  .jpg, png, jpeg files.</span>
                                                                        <input type="hidden" id="tempthumbnail" name="tempthumbnail" value="">
                                                                    </div>
                                                                   </div>
                                                                   <input type="hidden" name="isremoveimage" id="isremoveimage" value="0">
                                                                   <input type="hidden" name="isremovethumbnail" id="isremovethumbnail" value="0">
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </section>
                    </div>
                </div>
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        $('#categoryList').on('change', function () {
        $("#subcategoryList").attr('disabled', false); 
        $("#subcategoryList").val("");
        $(".subcategory").attr('disabled', true); 
        $(".subcategory").hide(); 
        $(".parent-" + $(this).val()).attr('disabled', false); 
        $(".parent-" + $(this).val()).show(); 
    });
    </script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" type="text/javascript"></script>
    <script src="https://cloud.tinymce.com/5/tinymce.min.js?apiKey=f4xmcmxdltpuabqrju9bvz7gtauleb4ebiua6d1n6farq25z"></script> 
    <script type="text/javascript">
       tinymce.init({
          selector: 'textarea',
          menubar: false,
          height: 500,
          paste_as_text: true,
          relative_urls: false,
          convert_urls: false,
         remove_script_host : false,
         plugins: 'searchreplace autolink directionality visualblocks visualchars fullscreen image link media charmap hr pagebreak nonbreaking anchor toc insertdatetime advlist lists textcolor wordcount imagetools contextmenu colorpicker textpattern, paste',
         toolbar1: 'formatselect | italic underline forecolor backcolor | media | link | image | alignleft aligncenter alignright alignjustify  | numlist bullist outdent indent',
          image_advtab: true,
          setup: function (ed) {
            ed.on('init', function(args) {
                $('#pretxt').hide();
                $('#txtArea').show();
            });
            ed.on('change', function(e) {
               calculateEditorCountPrimary();
           });
           ed.on('keyup', function(e) {
               calculateEditorCountPrimary();
                //$("#channelcontent").val(ed.getContent());
           });
          }
     });
        $(document).ready(function () {
            $(".aupload").colorbox({
            iframe: true,
            width: "650px",
            height: "650px"
        });
            <?php if($news->image_path != '') { ?>
               $("#uploadimg").hide();
            <?php } ?>     
            <?php if($news->thumbnail_path != '') { ?>
               $("#upload-thumbnail").hide();
            <?php } ?>    
            $('#editnews').validate({
            ignore: [],    
            rules : {
                title : 'required',
                hascontent : 'required',
                editorcount : 'required',
            },
            messages : {
                title :'Please enter title.',
                hascontent : 'Please enter description',
                editorcount : 'Maximum word limit for the content is 12,000.',
            }
            });
            $(document).on('change','#upload-img',function(event){
                var ext = $('#upload-img').val().split('.').pop().toLowerCase();
                if($.inArray(ext, ['gif','png','jpg','jpeg']) == -1) {
                    alert('Upload a JPG/GIF/PNG image with a dimension of 1600 x 400px (Width/Height).');
                }else{
                    var _URL = window.URL || window.webkitURL;
                    var file, img;
                if ((file = this.files[0])) {
                    img = new Image();
                    var fileExtension = ['jpeg', 'jpg', 'png', 'gif'];
                    img.onload = function () {
                    var w = this.width ;
                    var h = this.height ;
                    var tmppath = URL.createObjectURL(event.target.files[0]);
                    $("#userimage").attr("src",tmppath);
                    $("#removeimg").show();
                    $("#uploadimg").hide();

                };
                img.src = _URL.createObjectURL(file);
                }
                }
            });
        });
        function updateNews(){
         if($("#editnews").valid()){
            $("#editnewsbutton").prop('disabled',true);
            $("#editnews").submit();
         }else{
            setTimeout(function(){
                $('html, body').animate({
                    scrollTop: ($('.error:visible').first().offset().top - 100)
            }, 500);
            //$('.error:visible').first().focus();
            },500);
         }
        }
        function removeImage(){
            if (confirm('Are you sure you want to delete this image?')) {
                $("#userimage").attr("src","{{asset('images/upload-img.png')}}");
                $("#isremoveimage").val('1');
                $("#removeimg").hide();
                $("#remove-img").hide();
                $("#uploadimg").show();
                $("#upload-img").show();
            }
        }
        function removeThumbnail(){
            if (confirm('Are you sure you want to delete this image?')) {
                $("#thumbnailimage").attr("src","{{asset('images/upload-img.png')}}");
                $("#isremovethumbnail").val('1');
                $("#removethumbnail").hide();
                $("#remove-thumbnail").hide();
                $("#uploadthumbnail").show();
                $("#upload-thumbnail").show();
            }
        }
        function calculateEditorCountPrimary(){
            var editorwordcount = getStatsPrimary('description').words;
            editorwordcount = editorwordcount - 1;
            $("#editorcount").val('');
            $("#hascontent").val('');
            if( editorwordcount <= 12000 ){
                $("#editorcount").val(1);
            }
            if( editorwordcount > 0 ){
                $("#hascontent").val(1);
            }

            $("#editorcount").valid();
            $("#hascontent").valid();
        }
        function getStatsPrimary(id) {
            var body = tinymce.get(id).getBody(), text = tinymce.trim(body.innerText || body.textContent);

            return {
                chars: text.length,
                words: text.split(/[\w\u2019\'-]+/).length
            };
        } 
        function newsImage(imagekey,imagePath){
            $("#tempimage").val(imagekey);
            $("#userimage").attr("src",imagePath);
            $("#userimage").show();
            $("#upload-img").hide();
            $("#removeimg").show();
        }
        function thumbnailImage(imagekey,imagePath){
            $("#tempthumbnail").val(imagekey);
            $("#thumbnailimage").attr("src",imagePath);
            $("#thumbnailimage").show();
            $("#upload-thumbnail").hide();
            $("#removethumbnail").show();
        }
    </script>
</x-app-layout>