<script lang="ts">
	import { t } from 'svelte-i18n';
	import type { PageData } from './$types.js';
	import UserTable from '$lib/components/admin/UserTable.svelte';
	import { superForm } from 'sveltekit-superforms/client';

	let { data }: { data: PageData } = $props();

	let showCreateForm = $state(false);

	const { form, errors, enhance } = superForm(data.createForm, {
		resetForm: true,
		onResult: ({ result }) => {
			if (result.type === 'success') {
				showCreateForm = false;
			}
		}
	});
</script>

<svelte:head>
	<title>{$t('admin.user_management')} — {$t('admin.panel')}</title>
</svelte:head>

<div>
	<div class="mb-6 flex items-center justify-between">
		<h1 class="text-2xl font-bold text-card-foreground">{$t('admin.user_management')}</h1>
		<button
			type="button"
			onclick={() => (showCreateForm = !showCreateForm)}
			class="rounded-xl bg-primary px-4 py-2 text-sm font-medium text-primary-foreground hover:bg-primary/90 focus:outline-none focus-visible:ring-2 focus-visible:ring-primary/30"
		>
			{showCreateForm ? $t('common.cancel') : $t('admin.create_user')}
		</button>
	</div>

	{#if showCreateForm}
		<div class="mb-6 rounded-lg border border-border bg-card p-6">
			<h2 class="mb-4 text-lg font-semibold text-card-foreground">{$t('admin.new_user')}</h2>
			<form method="POST" action="?/create" use:enhance class="space-y-4">
				<div class="grid grid-cols-1 gap-4 sm:grid-cols-2">
					<div>
						<label for="email" class="mb-1 block text-sm font-medium text-foreground">{$t('auth.email')}</label>
						<input
							id="email"
							name="email"
							type="email"
							bind:value={$form.email}
							class="w-full rounded-xl border border-input bg-background px-3 py-2 text-sm text-foreground"
							required
						/>
						{#if $errors.email}<span class="text-xs text-destructive">{$errors.email}</span>{/if}
					</div>
					<div>
						<label for="displayName" class="mb-1 block text-sm font-medium text-foreground">{$t('auth.display_name')}</label>
						<input
							id="displayName"
							name="displayName"
							type="text"
							bind:value={$form.displayName}
							class="w-full rounded-xl border border-input bg-background px-3 py-2 text-sm text-foreground"
							required
						/>
						{#if $errors.displayName}<span class="text-xs text-destructive">{$errors.displayName}</span>{/if}
					</div>
					<div>
						<label for="password" class="mb-1 block text-sm font-medium text-foreground">{$t('auth.password')}</label>
						<input
							id="password"
							name="password"
							type="password"
							bind:value={$form.password}
							class="w-full rounded-xl border border-input bg-background px-3 py-2 text-sm text-foreground"
						/>
						{#if $errors.password}<span class="text-xs text-destructive">{$errors.password}</span>{/if}
					</div>
					<div>
						<label for="role" class="mb-1 block text-sm font-medium text-foreground">{$t('admin.role_column')}</label>
						<select
							id="role"
							name="role"
							bind:value={$form.role}
							class="w-full rounded-xl border border-input bg-background px-3 py-2 text-sm text-foreground"
						>
							<option value="user">{$t('admin.role_user')}</option>
							<option value="admin">{$t('admin.role_admin')}</option>
						</select>
					</div>
				</div>
				{#if $errors._errors}
					<p class="text-sm text-destructive">{$errors._errors}</p>
				{/if}
				<button
					type="submit"
					class="rounded-xl bg-primary px-4 py-2 text-sm font-medium text-primary-foreground hover:bg-primary/90"
				>
					{$t('admin.create_user')}
				</button>
			</form>
		</div>
	{/if}

	<UserTable users={data.users} groups={data.groups} />
</div>
