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.
(() => {
	let ns = mw.config.get("wgCanonicalNamespace");
	let name = mw.config.get("wgPageName");
	var date = new Date();
	var startTime = date.getTime();
	var spent = 0;
	var newDate;
	
	function formatDate(d) {
	    let day = d.getDate();
	    let month = d.getMonth();
	    let year = d.getFullYear();
	    const months = [
	        "January", "February", "March", "April", "May", "June",
	        "July", "August", "September", "October", "November", "December"
	    ];
	    return day + ' ' + months[month] + ' ' + year;
	}

	function getTime() {
		newDate = new Date();
		var time = newDate.getTime();
		spent = (time - startTime) / 1000;
		return spent;
	}
	
	function postToLog(title, time, action, d) {
		var params = {
			action: "query",
			titles: "User:" + mw.config.get("wgUserName") + "/AfC time log",
			format: "json"
		},
		api = new mw.Api();
		api.get(params).done((data) => {
			if(data.query.pages["-1"] != null) {
				params = {
					action: "edit",
					title: "User:" + mw.config.get("wgUserName") + "/AfC time log",
					text: "This page measures the amount of time it takes for me to review articles at AfC. This information is obtained using a [[User:JJPMaster/afc.js|user script]].\r\n* [[" + title + "]] - " + action + " after " + time + " seconds (" + d + ")",
					summary: "Added [[" + title + "]] to AfC time log using [[User:JJPMaster/afc.js]]",
					format: "json"
				},
				api.postWithToken("csrf", params).done((data) => {
					console.log(data);
				});
			}
			else {
				params = {
					action: "edit",
					title: "User:" + mw.config.get("wgUserName") + "/AfC time log",
					appendtext: "\r\n* [[" + title + "]] - " + action + " after " + time + " seconds (" + d + ")",
					summary: "Added [[" + title + "]] to AfC time log using [[User:JJPMaster/afc.js]]",
					format: "json"
				},
				api.postWithToken("csrf", params).done((data) => {
					console.log(data);
				});
			}
			mw.notify($("<a target=\"_blank\" style=\"color: black; text-decoration: none;\" href=\"" + mw.config.get("wgServer") + mw.config.get("wgArticlePath").replace('$1', "User:" + mw.config.get("wgUserName")) + "/AfC time log\">Article successfully added to User:" + mw.config.get("wgUserName") + "/AfC time log.<br />Click here to redirect there.</a>"));
		});
	}
	
	$(document).ready(() => {
		if(ns == "Draft") {
			console.log("Draft detected, start time is " + startTime);
			$("#bodyContent").on("click", "#afchSubmitForm", () => {
				console.log("Spent " + getTime() + " before reviewing " + name); 
				if($("#afchSubmitForm").hasClass("accept")) {
					postToLog(name, getTime(), "Accepted", formatDate(newDate));
				}
				else postToLog(name, getTime(), "Declined", formatDate(newDate));
			});
		}
		else console.log("Not a draft");
	});
})();