
// ===========
// Edit County
// ===========

$(document).ready(function () {

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

    // =================
    // Process Enter Key
    // =================

    $(document).on('keypress', function (e) {
        if (e.which == 13) {
            e.preventDefault();
            $("#editCounty").submit();
        }
    });

    // --------------
    // Select Pickers
    // --------------

    $(".search-dropdown").selectpicker({noneSelectedText: "",
        style: "",
        styleBase: "form-select form-select-sm",
        iconBase: "bi",
        virtualScroll: true,
        size: 10,
        selectOnTab: true,
        containter: "body",
        liveSearch: true});

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

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

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

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

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

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

        formSubmit($sessionStorage);

    });

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

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

        if (formPost({action: "Counties.countyActive", county_serial: $sessionStorage.county_serial}) === true) {

            $("#continue-dialog").find(".modal-title").html("Cannot Delete County").end()
                    .find(".modal-body").html("This County is in use and cannot be deleted.").end()
                    .modal("show");

        } else {

            $("#confirm-dialog").removeClass().addClass("modal confirm-delete-county")
                    .find(".modal-title").html("Confirm Delete County").end()
                    .find(".modal-body").html("Are you sure you wish to delete this County?").end()
                    .modal("show");

        }

    });

    $("body").on("click", ".confirm-delete-county .btnConfirmDialogConfirm", function () {

        $.post("./", {action: "Counties.deleteCounty", county_serial: $sessionStorage.county_serial})
                .done(function () {
                    formSubmit({action: $sessionStorage.deleteCounty});
                });

    });

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

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

        var county_serial = $("#county_serial"),
                county_name = $("#county_name"),
                county_area = $("#county_area");

        clearInputErrors();

        county_name.val(trimString(county_name.val()));

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

        if (formPost({action: "Counties.countyExists", county_serial: county_serial.val(), county_name: county_name.val()}) === true) {
            createInputError(county_name, "Name already exists");
            return false;
        }

        if ($("option:selected", county_area).val() === "") {
            createInputError(county_area, "Area is required");
            return false;
        }

        e.preventDefault();

        $.post("./", $(this).serialize())
                .done(function () {
                    formSubmit({action: $sessionStorage.editCounty});
                });

    });

});