
// ====================
// Subscription Summary
// ====================

$(document).ready(function () {

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

    var default_tab = "Counties";

    var subscription_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
    // ------------

    $("#subscription_completed").datepicker({format: "mm-dd-yyyy",
        container: "div.subscription_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.subscriptionSummary_tab = $sessionStorage.subscriptionSummary_tab || default_tab;

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

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

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

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

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

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

    });

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

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

        formPost({action: "Subscriptions.updateActiveStatus", subscription_serial: $sessionStorage.subscription_serial, subscription_active: $(this).val()});

        location.reload();
    });

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

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

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

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

        formSubmit($sessionStorage);

    });

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

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

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

        $sessionStorage.action = "Subscriptions.editSubscription";
        $sessionStorage.editSubscription = "Subscriptions.subscriptionSummary";
        $sessionStorage.deleteSubscription = "Subscriptions.setupSubscriptions";

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

        formSubmit($sessionStorage);

    });

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

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

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

        $sessionStorage.action = "Notes.addNote";
        $sessionStorage.note_type = "Subscription";
        $sessionStorage.note_reference = $sessionStorage.subscription_serial;
        $sessionStorage.addNote = "Subscriptions.subscriptionSummary";

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

        formSubmit($sessionStorage);

    });

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

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

        e.preventDefault();

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

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

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

        formSubmit($sessionStorage);

    });

    // ============
    // Counties Tab
    // ============

    // ---------------------------------
    // Add County to Subscription Button
    // ---------------------------------

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

        $sessionStorage.action = "Subscriptions.addCountyToSubscription";
        $sessionStorage.subscription_reference = $sessionStorage.subscription_serial;
        $sessionStorage.addCountyToSubscription = "Subscriptions.subscriptionSummary";

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

        formSubmit($sessionStorage);

    });

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

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

        e.preventDefault();

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

        $sessionStorage.action = "Subscriptions.editSubscriptionCounty";
        $sessionStorage.subscriptioncounty_serial = $(this).data("subscriptioncounty-serial");
        $sessionStorage.editSubscriptionCounty = "Subscriptions.subscriptionSummary";
        $sessionStorage.deleteSubscriptionCounty = "Subscriptions.subscriptionSummary";

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

        formSubmit($sessionStorage);

    });

    // ==========
    // Cities Tab
    // ==========

    // -------------------------------
    // Add City to Subscription Button
    // -------------------------------

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

        $sessionStorage.action = "Subscriptions.addCityToSubscription";
        $sessionStorage.subscription_reference = $sessionStorage.subscription_serial;
        $sessionStorage.addCityToSubscription = "Subscriptions.subscriptionSummary";

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

        formSubmit($sessionStorage);

    });

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

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

        e.preventDefault();

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

        $sessionStorage.action = "Subscriptions.editSubscriptionCity";
        $sessionStorage.subscriptioncity_serial = $(this).data("subscriptioncity-serial");
        $sessionStorage.editSubscriptionCity = "Subscriptions.subscriptionSummary";
        $sessionStorage.deleteSubscriptionCity = "Subscriptions.subscriptionSummary";

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

        formSubmit($sessionStorage);

    });

});