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

let {
	form
}: {
	form?: { passwordSuccess?: boolean; passwordError?: string } | null;
} = $props();
</script>

<div class="rounded-sm border border-line bg-surface p-6 shadow-sm">
	<h2 class="mb-5 text-xs font-bold tracking-widest text-ink-3 uppercase">
		{m.profile_section_password()}
	</h2>

	{#if form?.passwordSuccess}
		<div class="mb-5 rounded border border-green-200 bg-green-50 p-3 text-sm text-green-700">
			{m.profile_password_changed()}
		</div>
	{/if}
	{#if form?.passwordError}
		<div class="mb-5 rounded border border-red-200 bg-red-50 p-3 text-sm text-red-700">
			{#if form.passwordError === 'PASSWORDS_DO_NOT_MATCH'}
				{m.profile_password_mismatch()}
			{:else}
				{form.passwordError}
			{/if}
		</div>
	{/if}

	<form method="POST" action="?/changePassword" use:enhance>
		<div class="space-y-4">
			<label class="block">
				<span class="mb-1 block font-sans text-xs font-bold tracking-widest text-ink-3 uppercase">
					{m.profile_label_current_password()}
				</span>
				<input
					type="password"
					name="currentPassword"
					required
					class="w-full rounded-sm border border-line px-3 py-2 font-serif text-sm focus:outline-none focus-visible:ring-2 focus-visible:ring-focus-ring"
				/>
			</label>

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

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

		<button
			type="submit"
			data-testid="submit-password"
			class="mt-5 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_save()}
		</button>
	</form>
</div>
