
// ============
// Edit Popover
// ============

$(document).ready(function () {

    var $application = $("#APPLICATION_MNEMONIC").val(),
            $sessionStorage = JSON.parse(sessionStorage.getItem($application) || "{}"),
            $CKEditor;

    // ----------------------
    // Initialize the Editor.
    // ----------------------

    ClassicEditor
            .create(document.querySelector('#popover_text'), {
                toolbar: {
                    items: [
                        'heading',
                        '|',
                        'bold',
                        'italic',
                        'underline',
                        'link',
                        'bulletedList',
                        'numberedList',
                        '|',
                        'fontColor',
                        'fontSize',
                        'fontFamily',
                        '|',
                        'indent',
                        'outdent',
                        'alignment',
                        '|',
                        'horizontalLine',
                        'insertTable',
                        '|',
                        'undo',
                        'redo',
                        '|'
                    ]},
                link: {
                    addTargetToExternalLinks: true,
                    defaultProtocol: "http://"

                }
            })
            .then(editor => {
                $CKEditor = editor;
            })
            .catch(error => {
                console.error('CK-Editor error...');
                console.error(error);
            });

    // -----------
    // Save Button
    // -----------

    $(".btnSave").on("click", function (e) {

        e.preventDefault();

        $("#editPopover").submit();

    });

    // --------------
    // Cancel Button.
    // --------------

    $(".btnCancel").on("click", function () {

        $sessionStorage.action = $sessionStorage.editPopover || "";

        sessionStorage.setItem($application, JSON.stringify($sessionStorage));

        formSubmit($sessionStorage);

    });

    // -------------
    // Delete Button
    // -------------

    $(".btnDelete").on("click", function () {

        $("#confirm-dialog").removeClass().addClass("modal confirm-delete-popover")
                .find(".modal-title").html("Confirm Delete Popover").end()
                .find(".modal-body").html("Are you sure you wish to delete this Popover?").end()
                .modal("show");

    });

    $("body").on("click", ".confirm-delete-popover .btnConfirmDialogConfirm", function () {

        $.post("./", {action: "Popovers.deletePopover", popover_serial: $sessionStorage.popover_serial})

                .done(function () {

                    $sessionStorage.action = $sessionStorage.editPopover || "";

                    sessionStorage.setItem($application, JSON.stringify($sessionStorage));

                    formSubmit($sessionStorage);

                });

    });

    // ---------------
    // Validate Inputs
    // ---------------

    $("#editPopover").submit(function (e) {

        var popover_serial = $("#popover_serial"),
                popover_name = $("#popover_name"),
                popover_title = $("#popover_title"),
                popover_text = $("#popover_text");

        clearInputErrors();

        popover_name.val(popover_name.val().replace(/\s+/g, " ").trim());
        popover_title.val(popover_title.val().replace(/\s+/g, " ").trim());

        if (isBlank(popover_name)) {
            createInputError(popover_name, "Name is required");
            return false;
        }

        if (formPost({action: "Popovers.popoverExists", popover_serial: popover_serial.val(), popover_name: popover_name.val()}) === true) {
            createInputError(popover_name, "Name already exists");
            return false;
        }

        if (isBlank(popover_title)) {
            createInputError(popover_title, "Title is required");
            return false;
        }

        e.preventDefault();

        $CKEditor.updateSourceElement();

        $.post("./", $(this).serialize())

                .done(function () {

                    $sessionStorage.action = $sessionStorage.editPopover || "";

                    sessionStorage.setItem($application, JSON.stringify($sessionStorage));

                    formSubmit($sessionStorage);

                });

    });

});