<script lang="ts">
import { enhance } from '$app/forms';
import { m } from '$lib/paraglide/messages.js';

let { data, form } = $props();

function germanToIso(german: string): string {
	const match = german.match(/^(\d{2})\.(\d{2})\.(\d{4})$/);
	if (!match) return '';
	return `${match[3]}-${match[2]}-${match[1]}`;
}

let birthDateIso = $state('');

function handleBirthDateInput(e: Event) {
	const input = e.target as HTMLInputElement;
	const digits = input.value.replace(/\D/g, '').slice(0, 8);
	let formatted: string;
	if (digits.length <= 2) {
		formatted = digits;
	} else if (digits.length <= 4) {
		formatted = `${digits.slice(0, 2)}.${digits.slice(2)}`;
	} else {
		formatted = `${digits.slice(0, 2)}.${digits.slice(2, 4)}.${digits.slice(4)}`;
	}
	input.value = formatted;
	birthDateIso = germanToIso(formatted);
}
</script>

<div class="mx-auto max-w-3xl px-4 py-8 sm:px-6 lg:px-8">
	<a
		href="/admin"
		class="group mb-4 inline-flex items-center text-xs font-bold tracking-widest text-ink-2 uppercase transition-colors hover:text-ink"
	>
		<svg
			class="mr-2 h-4 w-4 transform transition-transform group-hover:-translate-x-1"
			fill="none"
			viewBox="0 0 24 24"
			stroke="currentColor"
			stroke-width="2"
		>
			<path stroke-linecap="round" stroke-linejoin="round" d="M15 19l-7-7 7-7" />
		</svg>
		{m.btn_back_to_overview()}
	</a>

	<h1 class="mb-6 font-serif text-3xl font-bold text-ink">{m.admin_user_new_heading()}</h1>

	{#if form?.error}
		<div class="mb-5 rounded border border-red-200 bg-red-50 p-3 text-sm text-red-700">
			{form.error}
		</div>
	{/if}

	<div class="rounded-sm border border-line bg-surface p-6 shadow-sm">
		<form method="POST" use:enhance class="space-y-5">
			<!-- Account -->
			<h2 class="text-xs font-bold tracking-widest text-ink-3 uppercase">
				{m.admin_section_users()}
			</h2>

			<label class="block">
				<span class="mb-1 block font-sans text-xs font-bold tracking-widest text-ink-3 uppercase">
					{m.admin_col_login()}
				</span>
				<input
					type="text"
					name="username"
					required
					class="w-full rounded-sm border border-line px-3 py-2 font-serif text-sm focus:border-ink focus:outline-none"
				/>
			</label>

			<label class="block">
				<span class="mb-1 block font-sans text-xs font-bold tracking-widest text-ink-3 uppercase">
					{m.admin_label_initial_password()}
				</span>
				<input
					type="password"
					name="password"
					required
					class="w-full rounded-sm border border-line px-3 py-2 font-serif text-sm focus:border-ink focus:outline-none"
				/>
			</label>

			<!-- Profile -->
			<h2 class="pt-2 text-xs font-bold tracking-widest text-ink-3 uppercase">
				{m.profile_section_personal()}
			</h2>

			<div class="grid grid-cols-1 gap-4 sm:grid-cols-2">
				<label class="block">
					<span class="mb-1 block font-sans text-xs font-bold tracking-widest text-ink-3 uppercase">
						{m.profile_label_first_name()}
					</span>
					<input
						type="text"
						name="firstName"
						class="w-full rounded-sm border border-line px-3 py-2 font-serif text-sm focus:border-ink focus:outline-none"
					/>
				</label>

				<label class="block">
					<span class="mb-1 block font-sans text-xs font-bold tracking-widest text-ink-3 uppercase">
						{m.profile_label_last_name()}
					</span>
					<input
						type="text"
						name="lastName"
						class="w-full rounded-sm border border-line px-3 py-2 font-serif text-sm focus:border-ink focus:outline-none"
					/>
				</label>
			</div>

			<label class="block">
				<span class="mb-1 block font-sans text-xs font-bold tracking-widest text-ink-3 uppercase">
					{m.profile_label_birth_date()}
				</span>
				<input
					type="text"
					placeholder="TT.MM.JJJJ"
					oninput={handleBirthDateInput}
					class="w-full rounded-sm border border-line px-3 py-2 font-serif text-sm focus:border-ink focus:outline-none"
				/>
				<input type="hidden" name="birthDate" value={birthDateIso} />
			</label>

			<label class="block">
				<span class="mb-1 block font-sans text-xs font-bold tracking-widest text-ink-3 uppercase">
					{m.profile_label_email()}
				</span>
				<input
					type="email"
					name="email"
					class="w-full rounded-sm border border-line px-3 py-2 font-serif text-sm focus:border-ink focus:outline-none"
				/>
			</label>

			<label class="block">
				<span class="mb-1 block font-sans text-xs font-bold tracking-widest text-ink-3 uppercase">
					{m.profile_label_contact()}
				</span>
				<textarea
					name="contact"
					rows="3"
					placeholder={m.profile_contact_placeholder()}
					class="w-full rounded-sm border border-line px-3 py-2 font-serif text-sm focus:border-ink focus:outline-none"
				></textarea>
			</label>

			<!-- Groups -->
			<h2 class="pt-2 text-xs font-bold tracking-widest text-ink-3 uppercase">
				{m.admin_col_groups()}
			</h2>

			<div class="flex flex-wrap gap-3">
				{#each data.groups as group (group.id)}
					<label class="inline-flex items-center gap-2 text-sm text-ink-2">
						<input
							type="checkbox"
							name="groupIds"
							value={group.id}
							class="rounded border-line text-ink focus:ring-accent"
						/>
						{group.name}
					</label>
				{/each}
			</div>

			<!-- Save bar -->
			<div
				class="mt-4 flex items-center justify-between rounded-sm border border-line bg-surface px-6 py-4 shadow-sm"
			>
				<a
					href="/admin"
					class="font-sans text-xs font-bold tracking-widest text-ink-2 uppercase hover:text-ink"
				>
					{m.btn_cancel()}
				</a>
				<button
					type="submit"
					class="rounded-sm bg-primary px-5 py-2 font-sans text-xs font-bold tracking-widest text-white uppercase transition-opacity hover:opacity-80"
				>
					{m.btn_create()}
				</button>
			</div>
		</form>
	</div>
</div>
