/*
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(
	"invitation_list",
	async (parameters, target_element) => {
		// types
		type type_item = {
			id : int;
			preview : {
				key : string;
				expiry : (null | int);
				name_value : string;
				label_value : string;
			};
		};
		
		// parameters
		const term : (null | string) = (parameters["term"] ?? "");
		
		// exec
		target_element.appendChild(_espe.helpers.template_request("invitation_list"));

		target_element.querySelector(".invitation_list-title").textContent = lib_plankton.translate.get("page.invitation_list.title");
		
		// exec : create link
		{
			const element : HTMLElement = target_element.querySelector(".invitation_list-create");
			element.setAttribute(
				"href",
				lib_plankton.zoo_page.encode(
					{
						"name": "invitation_create",
						"parameters": {
						}
					}
				)
			);
			element.textContent = lib_plankton.translate.get("page.invitation_create.title");
		}
		
		const search : lib_plankton.zoo_search.type_search<type_item> = lib_plankton.zoo_search.make<type_item>(
			(term) => _espe.backend.invitation_list(),
			{
				"encode_item": (item) => lib_plankton.string.coin(
					"{{label}}",
					{
						"label": item.preview.label_value,
					}
				),
				"hooks_begin": [
					(term) => {
						lib_plankton.zoo_page.set(
							{
								"name": "invitation_list",
								"parameters": {
									"term": term,
								}
							}
						);
					}
				],
				"hooks_select": [
					(item) => {
						lib_plankton.zoo_page.set(
							{
								"name": "invitation_view",
								"parameters": {
									"id": item.id.toFixed(0),
								}
							}
						);
					}
				]
			}
		);
		lib_plankton.zoo_search.render(
			search,
			target_element.querySelector(".invitation_list-search"),
			{
				"state": {
					"term": "",
				}
			}
		);
		
		/*
		const data = await _espe.backend.invitation_list();
		(target_element.querySelector(".invitation_list-data") as HTMLElement).textContent = JSON.stringify(
			data,
			undefined,
			"    "
		);
		 */
	}
);
