{{-- SCRIPT PARA RECLAMACIONES/CREAR Y EDIT QUICKLY A --}}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<script>
    $(document).ready(function() {

        //variables
        // var quickly_answers_id;

        //Mostrar el modal
        document.getElementById('addListGe').addEventListener('click', function() {
            $('#modalAgregarLG').modal('show');
        });

        //quickly a
        $('.quickly_answers').select2({
            placeholder: "Listado genérico",
            allowClear: true
        }).on('change', function() {
            var quickly_answers_id = $(".quickly_answers option:selected").val();
            if (!Livewire) {
                window.Livewire = new Livewire();
            }
            window.Livewire.emit('quickly_answers', quickly_answers_id);
        });

        window.addEventListener('quickly_answers_content', event => {
            var myContent = tinymce.get("description").getContent();

            tinymce.get("description").setContent(event.detail.quickly_answers_obj.content);



            let content = tinymce.get("description").getContent();
            if (!Livewire) {
                window.Livewire = new Livewire();
            }
            window.Livewire.emit('description_content', content);

        });

        //Mostrar Quickly as
        document.getElementById('view_list_generic').addEventListener('click', function() {

            var div_list_g = $(".div_list_g")
            if (div_list_g.is(":visible") == true) {
                div_list_g.addClass('d-none').css({
                    display: 'none'
                })
            } else {
                div_list_g.removeClass('d-none').css({
                    display: 'flex'
                })
            }
        });

        //Verificar si existe datos campo title descri list add
        var verify_description, verify_title;
        document.getElementById('buttonAddQuickly').addEventListener('click', function() {

            verify_description = tinymce.get("quickly_description").getContent();
            if (verify_description != '') {
                document.getElementById('err_rep_rap_des').classList.add("d-none");
            } else {
                document.getElementById('err_rep_rap_des').classList.remove("d-none");

            }

            verify_title = document.getElementById('quickly_add_title').value
            if (verify_title != '') {
                document.getElementById('err_rep_rap').classList.add("d-none");
            } else {
                document.getElementById('err_rep_rap').classList.remove("d-none");

            }

            if (verify_description != '' && verify_title != '') {
                if (!Livewire) {
                    window.Livewire = new Livewire();
                }
                var myContent = tinymce.get("quickly_description").getContent();
                window.Livewire.emit('storeQuickly', myContent);
            }


        });

        window.addEventListener('quick_close_modal', event => {
            $("#modalAgregarLG").modal('hide');
            Swal.fire(
                'Agregado!',
                'Respuesta rápida creada exitosamente.',
                'success'
            )

        });

        window.addEventListener('reset_list_quickly', event => {
            //reset
            resetQuickly()
        });


        //UPDATE GENERIC LIST
        document.getElementById('updateListGe').addEventListener('click', function() {
            $('#modalUpdateLG').modal('show');
        });

        // Editor update quickly a
        $('.list_generic_selec_edit').select2({
            placeholder: "Buscar...",
            allowClear: true
        }).on('change', function() {
            var quickly_answers_id = $(".list_generic_selec_edit option:selected").val();
            if (!Livewire) {
                window.Livewire = new Livewire();
            }
            window.Livewire.emit('quickly_answers_upd', quickly_answers_id);
        });

        //ADD CONTENT DESCRIPTION UPDATE
        window.addEventListener('quickly_answers_content_upd', event => {
            //add value title / description
            document.getElementById('quickly_upd_title').value = event.detail.quickly_answers_obj.title;
            document.getElementById("quickly_description_upd").value = event.detail.quickly_answers_obj
                .content

            //update quickly
            if (tinyMCE.get('quickly_description_upd')) {
                tinymce.get("quickly_description_upd").setContent('<p></p>');
                tinymce.remove('#quickly_description_upd');
            }
            tinyMCE.init({
                selector: '#quickly_description_upd',
                plugins: 'contextmenu preview paste importcss searchreplace directionality code visualchars fullscreen  link codesample table charmap hr pagebreak nonbreaking anchor toc advlist lists  textpattern noneditable help charmap emoticons image',
                menubar: 'edit view format tools table help insert', //insert
                toolbar: 'undo redo | bold italic underline strikethrough |  formatselect | alignleft aligncenter alignright alignjustify | outdent indent |  numlist bullist | forecolor backcolor removeformat | pagebreak | charmap emoticons | fullscreen  preview save print | ltr rtl | image', //insertfile image media template link anchor codesample |
                toolbar_sticky: true,
                contextmenu: false,
                height: 400,
                paste_data_images: false,
                automatic_uploads: true,
                setup: function(editor) {
                    editor.on('init change', function() {
                        editor.save();
                    });
                    editor.on('blur', function(e) {
                        @this.set('quickly_description_upd', editor.getContent());
                    });
                }
            });
        });

        //Event button update quickly
        $("#buttonListGUpdate").on('click', function() {
            var myContent = tinymce.get("quickly_description_upd").getContent();
            var title = document.getElementById('quickly_upd_title').value;
            var quickly_answers_id = $(".list_generic_selec_edit option:selected").val();
            if (myContent && title) {
                $('#err_descr_rejec').addClass('d-none');
                $('#modalUpdateLG').modal('hide');
                if (!Livewire) {
                    window.Livewire = new Livewire();
                }
                var content = [quickly_answers_id, title, myContent];
                window.Livewire.emit('update_quickly', content); //myContent
            } else {
                if (!myContent)
                    $('#err_descr_listUpd').removeClass('d-none');
                else
                    $('#err_descr_listUpd').addClass('d-none');

                if (!title)
                    $('#err_title_listUpd').removeClass('d-none');
                else
                    $('#err_title_listUpd').addClass('d-none');
            }

        });

        //Close modal upd quickly and event informativo
        window.addEventListener('quick_close_modal_upd', event => {
            $("#modalUpdateLG").modal('hide');
            Swal.fire(
                'Actualizado!',
                'Se actualizo la respueta rápida.',
                'success'
            )

        });

        //Informa si fue eliminado quickly a
        window.addEventListener('quickly_deleted', event => {
            Swal.fire(
                'Eliminado!',
                'Se elimino la respueta rápida.',
                'success'
            )

        });

    });

    function resetQuickly() {

        $('.quickly_answers').select2({
            placeholder: "Listado genérico",
            allowClear: true
        }).on('change', function() {
           var quickly_answers_id = $(".quickly_answers option:selected").val();
            if (!Livewire) {
                window.Livewire = new Livewire();
            }
            window.Livewire.emit('quickly_answers', quickly_answers_id);
        });

        $('.list_generic_selec_edit').select2({
            placeholder: "Buscar...",
            allowClear: true
        }).on('change', function() {
            var quickly_answers_id = $(".list_generic_selec_edit option:selected").val();
            if (!Livewire) {
                window.Livewire = new Livewire();
            }
            window.Livewire.emit('quickly_answers_upd', quickly_answers_id);
        });

        var buttonAdd = document.getElementById('buttonAddQuickly')
        // Validar que el div exista.
        if (buttonAdd != null || buttonAdd != undefined) {

            buttonAdd.addEventListener('click', function() {
                verify_description = tinymce.get("quickly_description").getContent();
                if (verify_description != '') {
                    document.getElementById('err_rep_rap_des').classList.add("d-none");
                } else {
                    document.getElementById('err_rep_rap_des').classList.remove("d-none");

                }

                verify_title = document.getElementById('quickly_add_title').value
                if (verify_title != '') {
                    document.getElementById('err_rep_rap').classList.add("d-none");
                } else {
                    document.getElementById('err_rep_rap').classList.remove("d-none");

                }

                if (verify_description != '' && verify_title != '') {
                    if (!Livewire) {
                        window.Livewire = new Livewire();
                    }
                    var myContent = tinymce.get("quickly_description").getContent();
                    window.Livewire.emit('storeQuickly', myContent);
                }
            });

        }



        //DELETE QUICKLY A
        $("#deleteListGe").on('click', function() {
            var quickly_answers_id = $(".list_generic_selec_edit option:selected").val();
            if (quickly_answers_id) {
                tinymce.get("description").setContent('');
                if (!Livewire) {
                    window.Livewire = new Livewire();
                }
                window.Livewire.emit('quickly_delete', quickly_answers_id);
            }
        });

        //create quickly
        if (tinyMCE.get('quickly_description')) {
            tinymce.get("quickly_description").setContent('<p></p>');
            tinymce.remove('#quickly_description');
        }
        tinyMCE.init({
            selector: '#quickly_description',
            plugins: 'contextmenu preview paste importcss searchreplace directionality code visualchars fullscreen  link codesample table charmap hr pagebreak nonbreaking anchor toc advlist lists  textpattern noneditable help charmap emoticons image',
            menubar: 'edit view format tools table help insert', //insert
            toolbar: 'undo redo | bold italic underline strikethrough |  formatselect | alignleft aligncenter alignright alignjustify | outdent indent |  numlist bullist | forecolor backcolor removeformat | pagebreak | charmap emoticons | fullscreen  preview save print | ltr rtl | image', //insertfile image media template link anchor codesample |
            toolbar_sticky: true,
            contextmenu: false,
            height: 400,
            paste_data_images: false,
            automatic_uploads: true,
            setup: function(editor) {
                editor.on('init change', function() {
                    editor.save();
                });
                editor.on('blur', function(e) {
                    @this.set('quickly_description', editor.getContent());
                });
            }
        });

        //update quickly
        if (tinyMCE.get('quickly_description_upd')) {
            tinymce.get("quickly_description_upd").setContent('<p></p>');
            tinymce.remove('#quickly_description_upd');
        }
        tinyMCE.init({
            selector: '#quickly_description_upd',
            plugins: 'contextmenu preview paste importcss searchreplace directionality code visualchars fullscreen  link codesample table charmap hr pagebreak nonbreaking anchor toc advlist lists  textpattern noneditable help charmap emoticons image',
            menubar: 'edit view format tools table help insert', //insert
            toolbar: 'undo redo | bold italic underline strikethrough |  formatselect | alignleft aligncenter alignright alignjustify | outdent indent |  numlist bullist | forecolor backcolor removeformat | pagebreak | charmap emoticons | fullscreen  preview save print | ltr rtl | image', //insertfile image media template link anchor codesample |
            toolbar_sticky: true,
            contextmenu: false,
            height: 400,
            paste_data_images: false,
            automatic_uploads: true,
            setup: function(editor) {
                editor.on('init change', function() {
                    editor.save();
                });
                editor.on('blur', function(e) {
                    @this.set('quickly_description_upd', editor.getContent());
                });
            }
        });
    }
</script>
