
// =========
// Edit User
// =========

$(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();

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

    });

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

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

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

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

        formSubmit($sessionStorage);

    });

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

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

        if (formPost({action: "Users.userActive", user_serial: $sessionStorage.user_serial}) === true) {

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

        } else {

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

        }

    });

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

        $.post("./", {action: "Users.deleteUser", user_serial: $sessionStorage.user_serial})

                .done(function () {

                    $sessionStorage.action = $sessionStorage.deleteUser || "";
                    $sessionStorage.toast = "User was Deleted";

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

                    formSubmit($sessionStorage);

                });

    });

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

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

        var user_name = $("#user_name"),
                user_email = $("#user_email"),
                user_role = $("#user_role"),
                user_client_name = $("#user_client_name");

        clearInputErrors();

        user_name.val(titleCase(user_name.val()));
        user_email.val(trimString(user_email.val()));

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

        if (isBlank(user_email)) {
            createInputError(user_email, "Email is required");
            return false;
        }

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

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

        e.preventDefault();

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

                .done(function () {

                    $sessionStorage.action = $sessionStorage.editUser || "";
                    $sessionStorage.toast = "User was Saved";

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

                    formSubmit($sessionStorage);

                });

    });

});