
// ============
// User Summary
// ============

$(document).ready(function () {

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

    var default_tab = "Subscriptions";

    var user_role = $("#USER_ROLE").val();

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

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

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

    $(".numeric-mask").inputmask("9{0,9}", {showMaskOnHover: false});

    $(".numeric-2-mask").inputmask({alias: "numeric",
        showMaskOnHover: false,
        placeholder: "0",
        groupSeparator: ",",
        allowMinus: false,
        digitsOptional: false,
        digits: 2});

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

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

    // Disable standard Drag/Drop processing.

    $("html").on("dragenter dragover dragleave drop", function (e) {

        e.preventDefault();

    });

    // ---------------------------
    // Set and Show the Active Tab
    // --------------------------

    $sessionStorage.userSummary_tab = $sessionStorage.userSummary_tab || default_tab;

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

    $("#tab-" + ($sessionStorage.userSummary_tab || default_tab)).tab("show");

    // --------------------
    // Capture a Tab Change
    // --------------------

    $(".nav-tabs a[data-bs-toggle='tab']").on("shown.bs.tab", function (e) {

        $sessionStorage.userSummary_tab = $(e.target).attr("data-tab");

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

    });

    // ----------------------
    // Active Status Dropdown
    // ----------------------

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

        formPost({action: "Users.updateActiveStatus", user_serial: $sessionStorage.user_serial, user_active: $(this).val()});

        location.reload();
    });

    // --------------------
    // Reset Users Password
    // ---------------------

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

        formPost({action: "Users.resetPassword", user_serial: $sessionStorage.user_serial});

        $sessionStorage.toast = "Passsword Reset";
        location.reload();

    });

    // ==============
    // Action Buttons
    // ==============

    // -----------
    // Done Button
    // -----------

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

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

        formSubmit($sessionStorage);

    });

    // -----------
    // Edit Button
    // -----------

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

        if (user_role === "Guest") {
            return false;
        }

        $sessionStorage.action = "Users.editUser";
        $sessionStorage.editUser = "Users.userSummary";
        $sessionStorage.deleteUser = "Users.setupUsers";

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

        formSubmit($sessionStorage);

    });

    // ----------------
    // Copy User Button
    // ----------------

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

        if (user_role === "Guest") {
            return false;
        }

        $sessionStorage.action = "Users.copyUser";
        $sessionStorage.copyUser_serial = $sessionStorage.user_serial;
        $sessionStorage.copyUser = "Users.setupUsers";

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

        formSubmit($sessionStorage);

    });

    // ------------
    // Print Button
    // ------------

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

        e.preventDefault();

        window.open("", "PrintTab");

        $("input[name = 'user_serial']", "#PrintUserSummaryForm").val($sessionStorage.user_serial);
        $("#PrintUserSummaryForm").submit();

    });

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

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

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

                });

    });

    // =========
    // Notes Tab
    // =========

    // ---------------
    // Add Note Button
    // ---------------

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

        $sessionStorage.action = "Notes.addNote";
        $sessionStorage.note_type = "User";
        $sessionStorage.note_reference = $sessionStorage.user_serial;
        $sessionStorage.addNote = "Users.userSummary";

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

        formSubmit($sessionStorage);

    });

    // ---------
    // Edit Note
    // ---------

    $("#notes-table tbody").on("click touch", "tr", function (e) {

        e.preventDefault();

        if (user_role === "Guest") {
            return false;
        }

        $sessionStorage.action = "Notes.editNote";
        $sessionStorage.note_serial = $(this).data("note-serial");
        $sessionStorage.editNote = "Users.userSummary";
        $sessionStorage.deleteNote = "Users.userSummary";

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

        formSubmit($sessionStorage);

    });
    // =================
    // Subscriptions Tab
    // =================

    // -------------------------------
    // Add Subscription to User Button
    // -------------------------------

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

        $sessionStorage.action = "Subscriptions.addSubscriptionToUser";
        $sessionStorage.user_reference = $sessionStorage.user_serial;
        $sessionStorage.addSubscriptionToUser = "Users.userSummary";

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

        formSubmit($sessionStorage);

    });

    // --------------------------------
    // Edit Subscription to User Button
    // --------------------------------

    $("#usersubscriptions-table tbody").on("click touch", "tr", function (e) {

        e.preventDefault();

        if (user_role === "Guest") {
            return false;
        }

        $sessionStorage.action = "Subscriptions.editUsersSubscription";
        $sessionStorage.usersubscription_serial = $(this).data("usersubscription-serial");
        $sessionStorage.editUsersSubscription = "Users.userSummary";
        $sessionStorage.deleteUsersSubscription = "Users.userSummary";

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

        formSubmit($sessionStorage);

    });

});