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

let { data, form } = $props();
</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">
			<AccountSection />

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

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

			<!-- 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-primary-fg uppercase transition-opacity hover:opacity-80"
				>
					{m.btn_create()}
				</button>
			</div>
		</form>
	</div>
</div>
