
// ============
// View Exports
// ============

$(document).ready(function () {

    setTimeout(() => {
        location.reload();
    }, 60_000); // 60 seconds


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

    // -----------
    // Find Button
    // -----------

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

        $sessionStorage.action = "Export.usersExports";
        $sessionStorage.pagination = "first";

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

        $("#spinner-dialog").modal("show");

        formSubmit($sessionStorage);

    });

    $("#find_exportjob").on("keyup", function (e) {
        if (e.key === "Enter") {
            $(".btnFind").trigger("click");
        }
    });

    // -----------------
    // Clear Find Button
    // -----------------

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

        $("#find_exportjob").val("").trigger("change");

        $sessionStorage.action = "Export.usersExports";
        $sessionStorage.pagination = "first";

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

        $("#spinner-dialog").modal("show");

        formSubmit($sessionStorage);

    });

    // ------------------
    // Pagination Buttons
    // ------------------

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

        $sessionStorage.action = "Export.usersExports";
        $sessionStorage.pagination = $(this).data("pagination");

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

        $("#spinner-dialog").modal("show");

        formSubmit($sessionStorage);

        delete $sessionStorage.pagination;

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

    });


    // -------------
    // Exports Table
    // -------------

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

        e.preventDefault();

        $("#spinner-dialog").modal("show");

        const $row = $(this).closest("tr");
        const exportCode = $row.data("export-code");

        if (!exportCode) {
            console.error("export code not found on row", $row);
            return;
        }


        $sessionStorage.action = "Export.downloadExport";
        $sessionStorage.export_code = exportCode;


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

        // Submit in a new tab
        formSubmit($sessionStorage, "_blank");

        // Hide spinner immediately, since the main page isn't going anywhere
        setTimeout(function () {
            $("#spinner-dialog").modal("hide");
        }, 2000); // optional: add a slight delay so it feels responsive

    });

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

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

        $sessionStorage.action = "";

        formSubmit($sessionStorage);

    });

    // --------------
    // Refresh Button
    // --------------

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

        $sessionStorage.action = "Export.usersExports";
        $sessionStorage.pagination = "first";

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

        $("#spinner-dialog").modal("show");

        formSubmit($sessionStorage);

    });

    // ------------------
    // Save preset Inputs
    // ------------------

    $("input[type=radio]:checked").trigger("change");

    // -----------------
    // Per Page Dropdown
    // -----------------

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

        $(".btnFind").trigger("click");

    });

    // -------------------
    // Save Changed Inputs
    // -------------------

    $(".autosave").on("change", function (e) {

        switch (e.target.type) {

            case "text"       :
                $sessionStorage[e.target.name] = $(this).val();
                break;
            case "radio"      :
                $sessionStorage[e.target.name] = $(this).val();
                break;
            case "checkbox"   :
                $sessionStorage[e.target.name] = $(this).prop("checked");
                break;
            case "select-one" :
                $sessionStorage[e.target.name] = $(this).val();
                break;
            case "hidden"     :
                $sessionStorage[e.target.name] = $(this).val();
                break;

            default           :
                $sessionStorage[e.target.name] = $(this).val();
                break;

        }

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

    });

    // ----------------
    // Initalize Inputs
    // ----------------

    $.each($sessionStorage, function (name, value) {

        var $input = $(":input").filter("[name='" + name + "']");

        if ($input.length > 0) {

            switch ($input.prop("type")) {

                case "text"       :
                    $input.val(value);
                    break;
                case "radio"      :
                    $input.filter("[value=" + value + "]").prop("checked", true);
                    break;
                case "checkbox"   :
                    $input.prop("checked", value);
                    break;
                case "select-one" :
                    $input.val(value);
                    break;
                case "hidden"     :
                    $input.val(value);
                    break;

                default           :
                    $input.val(value);
                    break;

            }

        }

    });

});