// ============
// Content Page
// ============

$(document).ready(function () {

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

    // ============
    // Navbar Links
    // ============

    // ------------
    // Manage Links
    // ------------

    $(".user-subscriptions").on("click", function () {

        $sessionStorage.action = "Subscriptions.userSubscriptions";

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

        console.log($sessionStorage);

        formSubmit($sessionStorage);

    });

    // -------
    // Support
    // -------

    $(".start-support-ticket").on("click", function () {

        $sessionStorage.action = "SupportTickets.startSupportTicket";

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

        console.log($sessionStorage);

        formSubmit($sessionStorage);

    });

    $(".manage-permits").on("click", function () {

        $sessionStorage.action = "Permits.managePermits";
        $sessionStorage.pagination = "first";
        $sessionStorage.find_permit = "";
        $sessionStorage.permits_order_by = "";

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

        formSubmit($sessionStorage);

    });

    // -------------
    // Consolidation
    // -------------

    $(".rdi-consolidation").on("click", function () {

        $sessionStorage.action = "Consolidation.consolidationSequence";
        $sessionStorage.step = "prompt";

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

        formSubmit($sessionStorage);

    });

    // -----------------
    // Import Permit CSV
    // -----------------

    $(".import-permit-csv").on("click", function () {

        $sessionStorage.action = "Import.uploadCSVFile";
        $sessionStorage.step = "prompt";

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

        formSubmit($sessionStorage);

    });

    // =============
    // Reports Links
    // =============

    // -------------
    // Select Report
    // -------------

    $(".select-report").on("click", function () {

        $sessionStorage.action = "Reports.selectReport";
        $sessionStorage.pagination = "first";
        $sessionStorage.find_report = "";

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

        formSubmit($sessionStorage);

    });

    // ===========
    // Setup Links
    // ===========

    // --------
    // Counties
    // --------

    $(".setup-counties").on("click", function () {

        $sessionStorage.action = "Counties.setupCounties";
        $sessionStorage.pagination = "first";
        $sessionStorage.find_county = "";

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

        formSubmit($sessionStorage);

    });
    // -----
    // Users
    // -----

    $(".setup-users").on("click", function () {

        $sessionStorage.action = "Users.setupUsers";
        $sessionStorage.pagination = "first";
        $sessionStorage.find_user = "";

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

        formSubmit($sessionStorage);

    });

    // -------------
    // Subscriptions
    // -------------

    $(".setup-subscriptions").on("click", function () {

        $sessionStorage.action = "Subscriptions.setupSubscriptions";
        $sessionStorage.pagination = "first";
        $sessionStorage.find_subscription = "";

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

        formSubmit($sessionStorage);

    });

    // ---------
    // Dropdowns
    // ---------

    $(".setup-dropdowns").on("click", function () {

        sessionStorage.removeItem($application);

        formSubmit({action: "Dropdowns.setupDropdowns"});

    });


    // ===================
    // User Dropdown Links
    // ===================

    // -----------------------
    // Toggle Dark/Light Theme
    // -----------------------

    $(".toggle-theme").on("click", function () {

        formPost({action: "toggleTheme"});

        if (document.documentElement.getAttribute("data-bs-theme") === "dark") {
            document.documentElement.setAttribute("data-bs-theme", "light");
        } else {
            document.documentElement.setAttribute("data-bs-theme", "dark");
        }

    });

    // ---------------
    // Sign out system
    // ---------------

    $(".sign-out").on("click", function () {

        formPost({action: "signout"});

        window.location.reload();

    });

    // --------------------
    // Setup Dropdown Types
    // --------------------

    $(".setup-dropdowntypes").on("click", function () {

        $sessionStorage.action = "Dropdowns.setupDropdowntypes";
        $sessionStorage.pagination = "first";

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

        formSubmit($sessionStorage);

    });

    // ------------
    // View Journal
    // ------------

    $(".view-journal").on("click", function () {

        $sessionStorage.action = "Journal.viewJournal";
        $sessionStorage.pagination = "first";

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

        formSubmit($sessionStorage);

    });


    // --------------------
    // View Support Tickets
    // --------------------

    $(".view-support-tickets").on("click", function () {

        $sessionStorage.action = "SupportTickets.viewSupportTickets";
        $sessionStorage.pagination = "first";

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

        formSubmit($sessionStorage);

    });

    // --------------
    // Setup Popovers
    // --------------

    $(".setup-popovers").on("click", function () {

        $sessionStorage.action = "Popovers.setupPopovers";
        $sessionStorage.pagination = "first";
        $sessionStorage.find_popover = "";

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

        formSubmit($sessionStorage);

    });

    // --------------
    // Setup Alerts
    // --------------

    $(".setup-alerts").on("click", function () {

        $sessionStorage.action = "Alerts.setupAlerts";
        $sessionStorage.pagination = "first";
        $sessionStorage.find_alert = "";

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

        formSubmit($sessionStorage);

    });


    // -------------
    // Setup Reports
    // -------------

    $(".setup-reports").on("click", function () {

        $sessionStorage.action = "Reports.setupReports";
        $sessionStorage.pagination = "first";
        $sessionStorage.find_report = "";

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

        formSubmit($sessionStorage);

    });

    // --------------
    // Setup Statuses
    // --------------

    $(".setup-statuses").on("click", function () {

        $sessionStorage.action = "Statuses.setupStatuses";
        $sessionStorage.pagination = "first";
        $sessionStorage.find_status = "";

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

        formSubmit($sessionStorage);

    });

    // --------------
    // Setup Statuses
    // --------------

    $(".run-developer-stub").on("click", function () {

        $sessionStorage.action = "testing";

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

        formSubmit($sessionStorage);

    });

});
