
// ===========
// Edit Report
// ===========

$(document).ready(function () {

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

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

    $(".btnSave").on("click", function (e) {

        e.preventDefault();

        $("#editReport").submit();

    });

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

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

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

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

        formSubmit($sessionStorage);

    });

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

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

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

    });

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

        $.post("./", {action: "Reports.deleteReport", report_serial: $sessionStorage.report_serial})

                .done(function () {

                    $sessionStorage.action = $sessionStorage.editReport || "";
                    $sessionStorage.toast = "Report was Deleted";

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

                    formSubmit($sessionStorage);

                });

    });

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

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

        var report_name = $("#report_name"),
                report_serial = $("#report_serial"),
                report_class = $("#report_class"),
                report_type = $("#report_type"),
                report_description = $("#report_description");

        clearInputErrors();

        report_name.val(report_name.val().replace(/\s+/g, " ").trim());

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

        if (isBlank(report_class)) {
            createInputError(report_class, "Class is required");
            return false;
        }

        if (formPost({action: "Reports.reportExists", report_serial: report_serial.val(), report_class: report_class.val()}) === true) {
            createInputError(report_class, "Class already exists");
            return false;
        }

        if (isBlank(report_description)) {
            createInputError(report_description, "Description is required");
            return false;
        }

        e.preventDefault();

        $.post("./", $(this).serialize())

                .done(function () {

                    $sessionStorage.action = $sessionStorage.editReport || "";
                    $sessionStorage.toast = "Report was Saved";

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

                    formSubmit($sessionStorage);

                });

    });

});