<script>
	import {
		GroupContactService,
		RunnerOrganizationService,
	} from "@odit/lfk-client-js";
	import toast from "svelte-french-toast";
	import { _ } from "svelte-i18n";
	import { tick } from "svelte";
	import Select from "svelte-select";
	import store from "../../store";
	import PromiseError from "../base/PromiseError.svelte";
	import GenerateRunnerCards from "../pdf_generation/GenerateRunnerCards.svelte";
	import GenerateRunnerCertificates from "../pdf_generation/GenerateRunnerCertificates.svelte";
	import GenerateSponsoringContracts from "../pdf_generation/GenerateSponsoringContracts.svelte";
	import ImportRunnerModal from "../runners/ImportRunnerModal.svelte";
	import ConfirmOrgDeletionModal from "./ConfirmOrgDeletionModal.svelte";
	$: address_valid_or_none =
		(isAddress1Valid && iszipcodevalid && iscityvalid) ||
		editable.address_checked === false;
	$: save_enabled = data_changed && address_valid_or_none;
	let original = "";
	let original_object = {};
	let contacts = [];
	let valueCopy = null;
	let areaDom;
	export let params;
	$: editable = {};
	$: contact = {};
	$: data_loaded = false;
	$: data_changed = !(JSON.stringify(editable) === original);
	$: isAddress1Valid = editable.address?.address1?.trim().length !== 0;
	$: iszipcodevalid = editable.address?.postalcode?.trim().length !== 0;
	$: iscityvalid = editable.address?.city?.trim().length !== 0;
	$: sponsoring_contracts_show = true;
	$: cards_show = true;
	$: certificates_show = true;
	$: generate_orgs = [original_object];
	$: registrationLink = `${config.baseurl_selfservice}/register/${editable.registrationKey}`;
	const getContactLabel = (option) =>
		option.firstname + " " + (option.middlename || "") + " " + option.lastname;
	const promise = RunnerOrganizationService.runnerOrganizationControllerGetOne(
		params.orgid
	).then((value) => {
		data_loaded = true;
		value.address_checked = value.address.address1 !== null;
		if (value.address_checked === false) {
			value.address = {
				address1: "",
				address2: "",
				city: "",
				postalcode: "",
				country: "",
			};
		}
		editable = Object.assign(editable, value);
		editable = editable;
		original_object = Object.assign(editable, value);
		original = JSON.stringify(value);
		GroupContactService.groupContactControllerGetAll().then((val) => {
			contacts = val.map((r) => {
				return { label: getContactLabel(r), value: r };
			});
			if (editable.contact) {
				contact = contacts.find((g) => g.value.id == editable.contact.id);
			} else {
				contact = null;
			}
		});
	});
	let modal_open = false;
	let delete_org = {};
	function deleteOrganization() {
		RunnerOrganizationService.runnerOrganizationControllerRemove(
			original_object.id,
			false
		)
			.then((resp) => {
				toast.success($_("organization-deleted"));
				location.replace("./");
			})
			.catch((err) => {});
	}
	function submit() {
		if (data_loaded === true && save_enabled) {
			toast($_("updating-organization"));
			let postdata = Object.assign({}, editable);
			if (postdata.address_checked === false) {
				postdata.address = null;
			}
			postdata.contact = postdata.contact?.id;
			RunnerOrganizationService.runnerOrganizationControllerPut(
				original_object.id,
				postdata
			)
				.then((resp) => {
					editable.registrationKey = resp.registrationKey;
					original_object = Object.assign({}, editable);
					original = JSON.stringify(original_object);
					toast.success($_("updated-organization"));
				})
				.catch((err) => {});
		} else {
		}
	}
	async function copy() {
		if (!editable.registrationKey) {
			toast.error($_("you-have-to-save-your-changes-to-generate-a-link"));
			return;
		}
		valueCopy = registrationLink;
		await tick();
		areaDom.focus();
		areaDom.select();
		try {
			const successful = document.execCommand("copy");
			if (!successful) {
				throw new Error();
			}
			toast($_("copied-link-to-clipboard"));
		} catch (err) {
			toast.error($_("error-whyile-copying-to-clipboard"));
		}
		// we can notifi by event or storage about copy status
		valueCopy = null;
	}
	export let import_modal_open = false;
</script>

{#if valueCopy != null}<textarea bind:this={areaDom}>{valueCopy}</textarea>{/if}
<ImportRunnerModal
	on:cancelDelete={(event) => {
		import_modal_open = false;
	}}
	current_runners={[]}
	passed_team={{}}
	passed_orgs={[]}
	passed_org={editable}
	opened_from="OrgDetail"
	bind:import_modal_open
/>
<ConfirmOrgDeletionModal bind:modal_open bind:delete_org />
{#if data_loaded}
	<section class="container p-5">
		<div class="flex flex-row mb-4">
			<div class="w-full">
				<nav class="w-full flex">
					<ol class="list-none flex flex-row items-center justify-start">
						<li class="flex items-center">
							<a class="mr-2" href="./"
								><svg
									xmlns="http://www.w3.org/2000/svg"
									width="24"
									height="24"
									viewBox="0 0 24 24"
									fill="none"
									stroke="currentColor"
									stroke-width="2"
									stroke-linecap="round"
									stroke-linejoin="round"
									class="inline-block"
									><path d="m12 19-7-7 7-7" /><path d="M19 12H5" /></svg
								>
								{$_("organizations")}</a
							>
						</li>
					</ol>
				</nav>
			</div>
		</div>
		<div class="mb-4 text-3xl font-extrabold leading-tight">
			{original_object.name} [#{params.orgid}]
			<div data-id="org_actions_${editable.id}">
				<GenerateSponsoringContracts
					bind:sponsoring_contracts_show
					bind:generate_orgs
				/>
				<GenerateRunnerCards bind:cards_show bind:generate_orgs />
				<GenerateRunnerCertificates bind:certificates_show bind:generate_orgs />
				{#if store.state.jwtinfo.userdetails.permissions.includes("RUNNER:IMPORT")}
					<button
						on:click={() => {
							import_modal_open = true;
						}}
						type="button"
						class="w-full inline-flex justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-blue-600 text-base font-medium text-white hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 sm:w-auto sm:text-sm"
					>
						{$_("import-runners")}
					</button>
				{/if}
				{#if store.state.jwtinfo.userdetails.permissions.includes("RUNNER:DELETE")}
					<button
						on:click={() => {
							modal_open = true;
							delete_org = original_object;
						}}
						type="button"
						class="w-full justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-red-600 text-base font-medium text-white hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500 sm:w-auto sm:text-sm"
						>{$_("delete-organization")}</button
					>
				{/if}
				<button
					on:click={submit}
					disabled={!save_enabled}
					class:opacity-50={!save_enabled}
					type="button"
					class="w-full justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-blue-600 text-base font-medium text-white hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 sm:w-auto sm:text-sm mb-1 lg:mb-0"
					>{$_("save-changes")}</button
				>
			</div>
		</div>
		<div class="text-sm w-full mt-2">
			<label for="name" class="font-semibold text-gray-700">{$_("name")}</label>
			<input
				autocomplete="off"
				placeholder={$_("name")}
				type="text"
				bind:value={editable.name}
				name="name"
				class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm rounded-l-md sm:text-sm border-gray-300 border bg-gray-50 text-neutral-800 rounded-md p-2"
			/>
		</div>
		<div class="text-sm w-full mt-2">
			<label for="contact" class="font-semibold text-gray-700"
				>{$_("contact")}</label
			>
			<Select
				containerClasses="rounded-l-md mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 border bg-gray-50 text-neutral-800 rounded-md p-2"
				itemFilter={(label, filterText, option) =>
					label.toLowerCase().includes(filterText.toLowerCase()) ||
					option.value.id.toString().startsWith(filterText.toLowerCase())}
				items={contacts}
				showChevron={true}
				placeholder={$_("no-contact-selected")}
				noOptionsMessage={$_("no-contact-found")}
				bind:selectedValue={contact}
				on:select={(selectedValue) =>
					(editable.contact = selectedValue.detail.value)}
				on:clear={() => (editable.contact = null)}
			/>
		</div>
		<div>
			<div class="flex items-start mt-2">
				<div class="flex items-center h-5">
					<input
						bind:checked={editable.registrationEnabled}
						id="toggle_selfservice_feature"
						name="toggle_selfservice_feature"
						type="checkbox"
						class="focus:ring-indigo-500 h-4 w-4 text-indigo-600 border-gray-300 rounded"
					/>
				</div>
				<div class="ml-3 text-sm">
					<label
						for="toggle_selfservice_feature"
						class="font-semibold text-gray-700"
						>{$_("selfservice-registration")}</label
					>
				</div>
			</div>
			<div>
				{#if editable.registrationEnabled}
					<div class="text-sm w-full mt-2">
						<button on:click={copy} class="inline-flex w-full">
							<p
								name="token"
								class="focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm rounded-l-md sm:text-sm border-gray-300 border bg-gray-50 text-neutral-800 p-2 break-all font-mono text-left"
							>
								{#if editable.registrationKey}
									{registrationLink}
								{:else}
									{$_("you-have-to-save-your-changes-to-generate-a-link")}
								{/if}
							</p>
							<div
								class="bg-gray-200 border-gray-300 border-t border-b border-r text-black rounded-r-md sm:text-sm p-2 cursor-pointer flex items-center justify-center"
							>
								<svg
									xmlns="http://www.w3.org/2000/svg"
									viewBox="0 0 24 24"
									width="24"
									height="24"
									><path fill="none" d="M0 0h24v24H0z" />
									<path
										fill="currentColor"
										d="M7 4V2h10v2h3l1 1v16a1 1 0 01-1 1H4a1 1 0 01-1-1V5l1-1h3zm0 2H5v14h14V6h-2v2H7V6zm2-2v2h6V4H9z"
									/></svg
								>
							</div>
						</button>
						{#if editable.registrationKey}
							<p class="text-gray-500 text-xs">
								{$_("click-to-copy-the-link-into-your-clipboard")}
							</p>
						{/if}
					</div>
				{/if}
				<!--  -->
				<div>
					<div class="flex items-start mt-2">
						<div class="flex items-center h-5">
							<input
								bind:checked={editable.address_checked}
								id="toggle_address_checkbox"
								name="toggle_address_checkbox"
								type="checkbox"
								class="focus:ring-indigo-500 h-4 w-4 text-indigo-600 border-gray-300 rounded"
							/>
						</div>
						<div class="ml-3 text-sm">
							<label
								for="toggle_address_checkbox"
								class="font-semibold text-gray-700">{$_("address")}</label
							>
						</div>
					</div>
				</div>
				{#if editable.address_checked === true}
					<div class="col-span-6">
						<label
							for="address1"
							class="block text-sm font-medium text-gray-700"
							>{$_("address")}</label
						>
						<input
							autocomplete="off"
							placeholder="Address"
							class:border-red-500={!isAddress1Valid}
							class:focus:border-red-500={!isAddress1Valid}
							class:focus:ring-red-500={!isAddress1Valid}
							bind:value={editable.address.address1}
							type="text"
							name="address1"
							class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm rounded-l-md sm:text-sm border-gray-300 border bg-gray-50 text-neutral-800 rounded-md p-2"
						/>
						{#if !isAddress1Valid}
							<span
								class="flex items-center font-medium tracking-wide text-red-500 text-xs mt-1 ml-1"
							>
								{$_("address-is-required")}
							</span>
						{/if}
					</div>
					<div class="col-span-6">
						<label
							for="address2"
							class="block text-sm font-medium text-gray-700"
							>{$_("apartment-suite-etc")}</label
						>
						<input
							autocomplete="off"
							placeholder={$_("apartment-suite-etc")}
							bind:value={editable.address.address2}
							type="text"
							name="address2"
							class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm rounded-l-md sm:text-sm border-gray-300 border bg-gray-50 text-neutral-800 rounded-md p-2"
						/>
					</div>
					<div class="col-span-6">
						<label for="zipcode" class="block text-sm font-medium text-gray-700"
							>{$_("zip-postal-code")}</label
						>
						<input
							autocomplete="off"
							placeholder={$_("zip-postal-code")}
							class:border-red-500={!iszipcodevalid}
							class:focus:border-red-500={!iszipcodevalid}
							class:focus:ring-red-500={!iszipcodevalid}
							bind:value={editable.address.postalcode}
							type="text"
							name="zipcode"
							class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm rounded-l-md sm:text-sm border-gray-300 border bg-gray-50 text-neutral-800 rounded-md p-2"
						/>
						{#if !iszipcodevalid}
							<span
								class="flex items-center font-medium tracking-wide text-red-500 text-xs mt-1 ml-1"
							>
								{$_("valid-zipcode-postal-code-is-required")}
							</span>
						{/if}
					</div>
					<div class="col-span-6">
						<label for="city" class="block text-sm font-medium text-gray-700"
							>{$_("city")}</label
						>
						<input
							autocomplete="off"
							placeholder={$_("city")}
							class:border-red-500={!iscityvalid}
							class:focus:border-red-500={!iscityvalid}
							class:focus:ring-red-500={!iscityvalid}
							bind:value={editable.address.city}
							type="text"
							name="city"
							class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm rounded-l-md sm:text-sm border-gray-300 border bg-gray-50 text-neutral-800 rounded-md p-2"
						/>
						{#if !iscityvalid}
							<span
								class="flex items-center font-medium tracking-wide text-red-500 text-xs mt-1 ml-1"
							>
								{$_("valid-city-is-required")}
							</span>
						{/if}
					</div>
				{/if}
				<div class="text-sm w-full mt-2">
					<span class="font-semibold text-gray-700">{$_("distance")}</span>
					<br />
					<span class="text-gray-700"
						>{(original_object.total_distance / 1000).toFixed(2)} km</span
					>
				</div>
			</div>
		</div>
	</section>
{:else}
	{#await promise}
		{$_("organization-detail-is-being-loaded")}
	{:catch error}
		<PromiseError />
	{/await}
{/if}
