
// ========
// Add User
// ========

$(document).ready(function () {

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

    console.log(sessionStorage);

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

    // ------------------------
    // Get the User Information
    // ------------------------

    $("#user_name").on("change", function () {

        clearInputErrors();

        let user = formPost({action: "Users.getUser_AJAX", user_name: $(this).val()});

        if (user.exists === true) {
            createInputError($(this), "User ID already exists");
            return false;
        }

    });

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

    $(".btnSave").on("click", function (e) {
        e.preventDefault();
        $("#addUser").submit();
    });

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

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

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

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

        formSubmit($sessionStorage);

    });
    // --------------------
    // Toggle Show Password
    // --------------------

    $("#togglePassword").click(function () {
        var passwordField = $("#user_password");
        var type = passwordField.attr("type") === "password" ? "text" : "password";
        passwordField.attr("type", type);

        // Toggle eye icon if using Bootstrap Icons
        var icon = $(this).find("i");
        icon.toggleClass("bi-eye bi-eye-slash");
    });

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

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

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

        clearInputErrors();

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

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

        let user = formPost({action: "Users.getUser_AJAX", user_name: user_name.val()});

        if (user.valid === false) {
            createInputError(user_name, "User Name is invalid");
            return false;
        }

        if (user.exists === true) {
            createInputError(user_name, "User Name already exists");
            return false;
        }

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

        if (isBlank(user_email)) {
            createInputError(user_email, "Email 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 () {
                    formSubmit({action: $sessionStorage.addUser});
                });

    });

});