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

jingrow.ui.form.on("Energy Point Log", {
	refresh: function (frm) {
		frm.events.make_reference_name_link(frm);
		if (frm.pg.reverted) {
			frm.set_intro(__("This document has been reverted"));
		} else if (frm.pg.type === "Auto" && jingrow.user_roles.includes("System Manager")) {
			frm.add_custom_button(__("Revert"), () => frm.events.show_revert_dialog(frm));
		}
	},
	show_revert_dialog(frm) {
		const revert_dialog = new jingrow.ui.Dialog({
			title: __("Revert"),
			fields: [
				{
					fieldname: "reason",
					fieldtype: "Small Text",
					label: __("Reason"),
					reqd: 1,
				},
			],
			primary_action: (values) => {
				return frm
					.call("revert", {
						reason: values.reason,
					})
					.then((res) => {
						let revert_log = res.message;
						revert_dialog.hide();
						revert_dialog.clear();
						jingrow.model.pageinfo[frm.pg.reference_pagetype][
							frm.pg.reference_name
						].energy_point_logs.unshift(revert_log);
						frm.refresh();
					});
			},
			primary_action_label: __("Submit"),
		});
		revert_dialog.show();
	},
	make_reference_name_link(frm) {
		let dt = frm.pg.reference_pagetype;
		let dn = frm.pg.reference_name;
		frm.fields_dict.reference_name.$input_wrapper
			.find(".control-value")
			.wrapInner(`<a href='/app/${jingrow.router.slug(dt)}/${dn}'></a>`);
	},
});
