/*
Espe | Ein schlichtes Werkzeug zur Mitglieder-Verwaltung | Frontend
Copyright (C) 2024 Christian Fraß

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public 
License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later 
version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see 
<https://www.gnu.org/licenses/>. 
 */

lib_plankton.zoo_page.register(
	"create",
	(parameters, target_element) => {
		target_element.appendChild(template_request("create"));
		
		target_element.querySelector(".create-title").textContent = lib_plankton.translate.get("page.create.title");
		
		const form = new lib_plankton.zoo_form.class_form<
			{
				membership_number : string;
				name_real_value : string;
				email_address_private : (null | string);
			},
			{
				membership_number : string;
				name_real_value : string;
				email_address_private : string;
			}
		>(
			value => ({
				"membership_number": value.membership_number,
				"name_real_value": value.name_real_value,
				"email_address_private": (value.email_address_private ?? ""),
			}),
			representation => ({
				"membership_number": representation.membership_number,
				"name_real_value": representation.name_real_value,
				"email_address_private": representation.email_address_private,
			}),
			new lib_plankton.zoo_input.class_input_group(
				[
					{
						"name": "name_real_value",
						"input": new lib_plankton.zoo_input.class_input_text(),
						"label": "Echter Name",
					},
					{
						"name": "membership_number",
						"input": new lib_plankton.zoo_input.class_input_text(),
						"label": "Mitgliedsnummer",
					},
					{
						"name": "email_address_private",
						"input": new lib_plankton.zoo_input.class_input_text(),
						"label": "Private E-Mail-Adresse",
					},
				]
			),
			[
				{
					"label": "Senden",
					"procedure": async (get_value, get_representation) => {
						const value = await get_value();
						const id : int = await _espe.backend.member_project(
							value,
							lib_plankton.zoo_page.encode(
								{
									"name": "view",
									"parameters": {
										"id": "{{id}}",
									}
								}
							)
						);
						lib_plankton.zoo_page.set({"name": "view", "parameters": {"id": id}});
					},
				}
			]
		);
		form.setup(target_element.querySelector(".create-form") as HTMLElement);
	}
);
