
// ================
// Add 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();

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

    });

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

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

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

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

        formSubmit($sessionStorage);

    });

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

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

        var subscription_title = $("#subscription_title"),
                subscription_project_type = $("#subscription_service_level");

        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 ($("option:selected", subscription_project_type).val() === "") {
            createInputError(subscription_project_type, "Service Level is required");
            return false;
        }

        e.preventDefault();

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

                .done(function () {

                    $sessionStorage.action = $sessionStorage.addSubscription || "";
                    $sessionStorage.toast = "Subscription was Added";

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

                    formSubmit($sessionStorage);

                });

    });

});