@extends("admin.layout.base")
@extends("admin.layout.header")

@section("title")
    Opret Nyheder
@endsection

@section("path")
    <a href="{{ route('news.create') }}" class="text-white">Opret Nyheder</a> /
@endsection

@section("content")
    <style>
        #editor {
            margin-bottom: 1rem;
        }
    </style>
    <link href="{{ asset("/css/quill/quill.snow.css") }}" rel="stylesheet">
    <h1 id="errormesseages" >Opret Nyhed</h1>
    <form method="post" action="{{ route("news.store") }}">
        @csrf
        <label for="title">Titel på nyheden:</label>
        <label hidden id="error" for="errormesseages">Dette navn findes allerede</label>
        <input type="text" name="subname" id="title" placeholder="OBS: Menuplanen er ændret" required>
        <div id="editor"></div>
        <textarea name="content" class="d-none" id="hiddenArea"></textarea>
        <input type="submit" id="disable" class="btn btn-dark text-white" value="Opret">
    </form>

    <script src="{{ asset("/js/quill/quill.js") }}"></script>
    <script>
        var container = document.getElementById("editor");
        var fullEditor = new Quill(container, {
            modules: {
                'toolbar': [
                    [ 'bold', 'italic', 'underline', 'strike' ],
                    [{ 'color': [] }],
                    [{ 'script': 'super' }, { 'script': 'sub' }],
                    [{ 'header': '1' }, { 'header': '2' }, 'blockquote' ],
                    [{ 'list': 'ordered' }, { 'list': 'bullet'}, { 'indent': '-1' }, { 'indent': '+1' }],
                    [ {'direction': 'rtl'}, { 'align': [] }],
                    [ 'link' ],
                    [ 'clean' ]
                ],
            },
            theme: 'snow'
        });
    </script>
    <script>
        var form = document.querySelector('form');
        form.onsubmit = function() {
            var myEditor = document.getElementById('editor');
            var html = myEditor.children[0].innerHTML;
            document.getElementById('hiddenArea').innerText = html;
        }
    </script>

@endsection
@section("scripts")
    <script>
        $(function() {
            $('form').areYouSure(
                {
                    message: 'It looks like you have been editing something. '
                        + 'If you leave before saving, your changes will be lost.'
                }
            );
        });

        //Dont delete me, if the user reloads the pages with text in the text-box this code runs.
        $(document).ready(function () {
            $value = $('#title').val();
            $.ajax({
                type: 'get',
                url: '{{route('news.nameCheck')}}',
                data: {'nameCheck':$value},
                success:function (data) {
                    console.log(data);
                    if(data){
                        $("#error").show(100);
                        $("#error").css('color', 'red');
                        $("#disable").prop('disabled', true);
                        $('#disable').css('cursor','not-allowed');
                    }else{
                        $("#error").hide();
                        $("#disable").prop('disabled', false);
                        $('#disable').css('cursor','pointer');
                    }

                },
                error:function (data) {
                    console.log(data);
                }
            });
        });

        $('#title').on('keyup', function () {
            $value = $(this).val();
            $.ajax({
                type: 'get',
                url: '{{route('news.nameCheck')}}',
                data: {'nameCheck':$value},
                success:function (data) {
                    if(data){
                        $("#error").show(100);
                        $("#error").css('color', 'red');
                        $("#disable").prop('disabled', true);
                        $('#disable').css('cursor','not-allowed');
                    }else{
                        $("#error").hide();
                        $("#disable").prop('disabled', false);
                        $('#disable').css('cursor','pointer');
                    }

                },
                error:function (data) {
                    console.log(data);
                }
            });
        })
    </script>
@endsection
