
// ===========
// Edit City
// ===========

$(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();
            $("#editCity").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();
        $("#editCity").submit();
    });

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

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

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

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

        formSubmit($sessionStorage);

    });

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

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

        if (formPost({action: "Cities.cityActive", city_serial: $sessionStorage.city_serial}) === true) {

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

        } else {

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

        }

    });

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

        $.post("./", {action: "Cities.deleteCity", city_serial: $sessionStorage.city_serial})
                .done(function () {
                    formSubmit({action: $sessionStorage.deleteCity});
                });

    });

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

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

        var city_serial = $("#city_serial"),
                city_name = $("#city_name"),
                city_area = $("#city_area");

        clearInputErrors();

        city_name.val(trimString(city_name.val()));

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

        if (formPost({action: "Cities.cityExists", city_serial: city_serial.val(), city_name: city_name.val()}) === true) {
            createInputError(city_name, "Name already exists");
            return false;
        }

        e.preventDefault();

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

    });

});