
// ========================
// Add Subscription To User
// ========================

$(document).ready(function () {

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

    // -----------
    // Input Masks
    // -----------

    $(".date-mask").inputmask({alias: "datetime",
        inputFormat: "mm/dd/yyyy",
        placeholder: "MM/DD/YYYY",
        showMaskOnHover: false});

    // --------------
    // 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});

    // ------------
    // Date Pickers
    // ------------

    $("#subscription_stop_date").datepicker({format: "mm-dd-yyyy",
        container: "div.subscription_stop_date",
        keyboardNavigation: false,
        showOnFocus: false,
        autoclose: true,
        orientation: "left bottom",
        todayBtn: "linked",
        todayHighlight: true});

    // ---------------------
    // Show Calendar Buttons
    // ---------------------

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

        e.preventDefault();

        $("#subscription_stop_date").focus().datepicker("show");

    });

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

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

        e.preventDefault();

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

    });

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

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

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

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

        formSubmit($sessionStorage);

    });

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

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

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

    });

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

        $.post("./", {action: "Subscriptions.deleteUsersSubscription", usersubscription_serial: $sessionStorage.usersubscription_serial})

                .done(function () {

                    $sessionStorage.action = $sessionStorage.deleteUsersSubscription || "";
                    $sessionStorage.toast = "Users Subscription was Deleted";

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

                    formSubmit($sessionStorage);

                });

    });

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

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

        var subscription_title = $("#subscription_serial"), subscription_stop_date = $("#subscription_stop_date");

        clearInputErrors();

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


        if (isBlank(subscription_stop_date)) {
            createInputError(subscription_stop_date, "Date is required");
            return false;
        }

        e.preventDefault();

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

                .done(function () {

                    $sessionStorage.action = $sessionStorage.editUsersSubscription || "";
                    $sessionStorage.toast = "Users Subscription was Updated";

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

                    formSubmit($sessionStorage);

                });

    });

});