
// =================
// Edit Project Type
// =================

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

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

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

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

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

        formSubmit($sessionStorage);

    });

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

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

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

            $("#continue-dialog").find(".modal-title").html("Cannot Delete Project Type").end()
                    .find(".modal-body").html("This ProjectType 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 ProjectType").end()
                    .find(".modal-body").html("Are you sure you wish to delete this ProjectType?").end()
                    .modal("show");

        }

    });

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

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

    });

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

    $("#editProjectType").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: "ProjectTypes.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.editProjectType});
                });

    });

});