Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// A wizard for users to submit reports at [[Wikipedia:Edit filter/False positives/Reports]]
// <nowiki>

$.when($.ready, mw.loader.using(["mediawiki.api", "mediawiki.util", "mediawiki.widgets"])).then(function () {
    if (mw.config.get("wgPageName") != "User:CanonNi/WIP/EFFPR") return;
    var api = new mw.Api();
    var forSelf = null;

    var myselfButton = new OO.ui.ButtonWidget({
        label: "Myself",
        flags: ["primary", "progressive"],
    });
    var othersButton = new OO.ui.ButtonWidget({
        label: "Someone else",
        flags: ["progressive"],
    });

    var menu = new OO.ui.FieldsetLayout();
    menu.addItems([
        new OO.ui.MessageWidget({
            type: "notice",
            inline: true,
            label: "Thank you for reporting a false positive.",
        }),
        new OO.ui.LabelWidget({ label: "This wizard will guide you through filling out a report. First things first, are you reporting a false positive that blocked your edit, or one that affected someone else?" }),
        new OO.ui.FieldsetLayout(new OO.ui.ActionFieldLayout(myselfButton, othersButton, { align: "inline" })),
    ]);
    $("#effpr-wizard-container").empty().append(menu.$element);
    $("#catlinks").remove();

    function nextPage() {
        var fieldset = new OO.ui.FieldsetLayout();

        if (!forSelf) {
            var user = new mw.widgets.UserInputWidget({
                required: true,
            });
            var filterid = new OO.ui.TextInputWidget({
                placeholder: "Leave blank if you're not sure.",
                required: false,
                type: "number",
            });
            var logid = new OO.ui.TextInputWidget({
                placeholder: "Leave blank if you're not sure.",
                required: false,
                type: "number",
            });

            fieldset.addItems([
                new OO.ui.FieldLayout(user, {
                    label: "Username of the account:",
                    align: "top",
                }),
                new OO.ui.FieldLayout(filterid, {
                    label: "Filter ID:",
                    align: "top",
                }),
                new OO.ui.FieldLayout(logid, {
                    label: "Filter log entry ID:",
                    align: "top",
                }),
            ]);
        }

        var title = new mw.widgets.TitleInputWidget({
            value: mw.util.getParamValue("page") || "",
            required: true,
        });
        var comment = new OO.ui.MultilineTextInputWidget({
            placeholder: "Anything else you'd like the reviewer to know. For example, what was the edit trying to do?",
            required: false,
        });
        var submitButton = new OO.ui.ButtonWidget({
            label: "Submit report",
            flags: ["progressive", "primary"],
        });

        fieldset.addItems([
            new OO.ui.FieldLayout(title, {
                label: "Title of the page:",
                align: "top",
            }),
            new OO.ui.FieldLayout(comment, {
                label: "Additional comments:",
                align: "top",
            }),

            submitButton,
        ]);
        $("#effpr-wizard-container").empty().append(fieldset.$element);

        function makeWikitext() {
            var titleValue = title.getValue().trim();
            var commentValue = comment.getValue().trim();

            if (!forSelf) {
                return "{{subst:False positive other| filterid = " + filterid.getValue() || "" + " | logid = " + logid.getValue() || "" + " | username = " + user.getValue().trim() + " | page = " + titleValue + " | description = " + commentValue + " }}";
            } else {
                return "{{subst:False positive | page = " + titleValue + " | description = " + commentValue + " }}";
            }
        }

        submitButton.on("click", function () {
            submitButton.setDisabled(true);
            submitButton.setLabel("Submitting...");

            api.edit("User:CanonNi/WIP/EFFPR", function () {
                return {
                    appendtext: "\n\n" + makeWikitext(),
                    summary: "[[User:CanonNi/WIP/EFFPR|Reporting a false positive]] on [[" + title.getValue().trim() + "]]",
                };
            })
                .then(function () {
                    window.location.href = mw.util.getUrl("User:CanonNi/WIP/EFFPR#" + mw.config.get("wgUserName"));
                    window.location.reload();
                })
                .catch(function (err) {
                    submitButton.setDisabled(false);
                    submitButton.setLabel("Retry submitting");
                    fieldset.addItems([
                        new OO.ui.MessageWidget({
                            type: "error",
                            label: "An error occurred: " + err,
                        }),
                    ]);
                });
        });

        $(window).on("beforeunload", function (evt) {
            if (title.getValue().length > 0 || comment.getValue().length > 0) {
                evt.preventDefault();
                evt.returnValue = "The report has not yet been submitted. Are you sure you want to leave?";
            }
        });
    }

    myselfButton.on("click", function () {
        forSelf = true;
        nextPage();
    });
    othersButton.on("click", function () {
        forSelf = false;
        nextPage();
    });
});

// </nowiki>