// Copyright (c) 2022, JINGROW
// For license information, please see license.txt

jingrow.ui.form.on("RQ Job", {
	refresh: function (frm) {
		// Nothing in this form is supposed to be editable.
		frm.disable_form();
		frm.dashboard.set_headline_alert(
			__("This is a virtual pagetype and data is cleared periodically.")
		);

		if (["started", "queued"].includes(frm.pg.status)) {
			frm.add_custom_button(__("Force Stop job"), () => {
				jingrow.confirm(
					__(
						"This will terminate the job immediately and might be dangerous, are you sure? "
					),
					() => {
						jingrow
							.xcall("jingrow.core.pagetype.rq_job.rq_job.stop_job", {
								job_id: frm.pg.name,
							})
							.then((r) => {
								jingrow.show_alert(__("Job Stopped Successfully"));
								frm.reload_pg();
							});
					}
				);
			});
		}
	},
});
