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

jingrow.query_reports["Database Storage Usage By Tables"] = {
	filters: [],
	onload: function (report) {
		report.page.add_inner_button(
			__("Optimize"),
			function () {
				let d = new jingrow.ui.Dialog({
					title: "Optimize Pagetype",
					fields: [
						{
							label: "Select a PageType",
							fieldname: "pagetype_name",
							fieldtype: "Link",
							options: "PageType",
							get_query: function () {
								return {
									filters: { issingle: ["=", false], is_virtual: ["=", false] },
								};
							},
						},
					],
					size: "small",
					primary_action_label: "Optimize",
					primary_action(values) {
						jingrow.call({
							method: "jingrow.core.report.database_storage_usage_by_tables.database_storage_usage_by_tables.optimize_pagetype",
							args: {
								pagetype_name: values.pagetype_name,
							},
							callback: function (r) {
								if (!r.exec) {
									jingrow.show_alert(
										__(
											`${values.pagetype_name} has been added to queue for optimization`
										)
									);
								}
							},
						});
						d.hide();
					},
				});
				d.show();
			},
			__("Actions")
		);
	},
};
