
// =================
// Edit Subscription
// =================

$(document).ready(function () {

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

    // --------------
    // Select Picker.
    // --------------

    $(".search-dropdown").selectpicker({noneSelectedText: "",
        style: "",
        styleBase: "form-select form-select-sm",
        iconBase: "bi",
        virtualScroll: true,
        size: 10,
        selectOnTab: true,
        containter: "body",
        liveSearch: true});

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

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

        e.preventDefault();

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

    });

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

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

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

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

        formSubmit($sessionStorage);

    });

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

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

        if (formPost({action: "Subscriptions.subscriptionActive", subscription_serial: $sessionStorage.subscription_serial}) === true) {

            $("#continue-dialog").find(".modal-title").html("Cannot Delete Subscription").end()
                    .find(".modal-body").html("This Subscription is in use and cannot be deleted.").end()
                    .modal("show");

        } else {

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

        }

    });

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

        $.post("./", {action: "Subscriptions.deleteSubscription", subscription_serial: $sessionStorage.subscription_serial})
                .done(function () {
                    formSubmit({action: $sessionStorage.deleteSubscription});
                });

    });

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

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

        var subscription_title = $("#subscription_title"),
                subscription_description = $("#subscription_description"),
                subscription_project_type = $("subscription_projecttype"),
                subscription_area = $("#subscription_area");

        clearInputErrors();

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

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

        if (isBlank(subscription_description)) {
            createInputError(subscription_description, "Description is required");
            return false;
        }

        if ($("option:selected", subscription_project_type).val() === "") {
            createInputError(subscription_project_type, "Project Type is required");
            return false;
        }

        if ($("option:selected", subscription_area).val() === "") {
            createInputError(subscription_area, "Area is required");
            return false;
        }

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

                .done(function () {

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

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

                    formSubmit($sessionStorage);

                });

    });

});