<script lang="ts">
	/* eslint-disable @typescript-eslint/no-explicit-any */
	import { Field as FormField, Control, FieldErrors } from 'formsnap';
	import MoneyField from '$lib/components/money-field.svelte';
	import * as Field from '$lib/components/ui/field/index.js';
	import { Button } from '$lib/components/ui/button/index.js';
	import * as Dialog from '$lib/components/ui/dialog/index.js';
	import { Input } from '$lib/components/ui/input/index.js';
	import * as NativeSelect from '$lib/components/ui/native-select/index.js';
	import { Textarea } from '$lib/components/ui/textarea/index.js';
	let {
		editingRecord,
		editForm,
		editData,
		enhanceEdit,
		onClose
	}: {
		editingRecord: any;
		editForm: any;
		editData: any;
		enhanceEdit: any;
		onClose: () => void;
	} = $props();
</script>

<Dialog.Root open={!!editingRecord} onOpenChange={(open) => !open && onClose()}>
	<Dialog.Content class="max-h-[90vh] overflow-y-auto sm:max-w-2xl">
		<Dialog.Header>
			<Dialog.Title>Edit contract</Dialog.Title>
			<Dialog.Description>Update this contract record.</Dialog.Description>
		</Dialog.Header>
		{#if editingRecord}
			<form method="POST" action="?/edit" class="grid gap-2" use:enhanceEdit>
				<input type="hidden" name="id" value={$editData.id} />
				<FormField form={editForm} name="title">
					<Field.Field>
						<Control id="edit-title">
							{#snippet children({ props })}
								<Field.Label>Title</Field.Label>
								<Input {...props} type="text" bind:value={$editData.title} />
							{/snippet}
						</Control>
						<Field.Error><FieldErrors /></Field.Error>
					</Field.Field>
				</FormField>
				<FormField form={editForm} name="startDate">
					<Field.Field>
						<Control id="edit-startDate">
							{#snippet children({ props })}
								<Field.Label>Start date</Field.Label>
								<Input {...props} type="date" bind:value={$editData.startDate} />
							{/snippet}
						</Control>
						<Field.Error><FieldErrors /></Field.Error>
					</Field.Field>
				</FormField>
				<FormField form={editForm} name="endDate">
					<Field.Field>
						<Control id="edit-endDate">
							{#snippet children({ props })}
								<Field.Label>End date</Field.Label>
								<Input {...props} type="date" bind:value={$editData.endDate} />
							{/snippet}
						</Control>
						<Field.Error><FieldErrors /></Field.Error>
					</Field.Field>
				</FormField>
				<FormField form={editForm} name="status">
					<Field.Field>
						<Control id="edit-status">
							{#snippet children({ props })}
								<Field.Label>Status</Field.Label>
								<NativeSelect.Root {...props} class="w-full" bind:value={$editData.status}>
									<NativeSelect.Option value="">Not set</NativeSelect.Option>
									<NativeSelect.Option value="draft">Draft</NativeSelect.Option>
									<NativeSelect.Option value="active">Active</NativeSelect.Option>
									<NativeSelect.Option value="expired">Expired</NativeSelect.Option>
									<NativeSelect.Option value="cancelled">Cancelled</NativeSelect.Option>
								</NativeSelect.Root>
							{/snippet}
						</Control>
						<Field.Error><FieldErrors /></Field.Error>
					</Field.Field>
				</FormField>
				<MoneyField
					form={editForm}
					data={editData}
					name="valueGbp"
					label="Value"
					id="edit-valueGbp"
				/>
				<FormField form={editForm} name="notes">
					<Field.Field>
						<Control id="edit-notes">
							{#snippet children({ props })}
								<Field.Label>Notes</Field.Label>
								<Textarea {...props} bind:value={$editData.notes} />
							{/snippet}
						</Control>
						<Field.Error><FieldErrors /></Field.Error>
					</Field.Field>
				</FormField>
				<Dialog.Footer>
					<Dialog.Close
						>{#snippet child({ props })}<Button variant="outline" {...props}>Cancel</Button
							>{/snippet}</Dialog.Close
					>
					<Button type="submit">Save changes</Button>
				</Dialog.Footer>
			</form>
		{/if}
	</Dialog.Content>
</Dialog.Root>
