<script lang="ts">
  import type { PageData } from "./$types";

  import { superForm } from "sveltekit-superforms";

  import { superformConfig } from "$lib/shared/util";

  import EntryBody from "$lib/components/entry/EntryBody.svelte";
  import EntryTodoButton from "$lib/components/ui/EntryTodoButton.svelte";
  import MarkdownInput from "$lib/components/ui/markdown/MarkdownInput.svelte";

  import { SchemaNewExecution } from "./schema";

  export let data: PageData;

  const {
    form, errors, enhance,
  } = superForm(data.form, {
    validators: SchemaNewExecution,
    ...superformConfig("Eintrag"),
  });
</script>

<EntryBody entry={data.entry} withExecution />

{#if !data.entry.execution?.done}
  <form class="print:hidden" method="POST" use:enhance>
    <MarkdownInput
      name="text"
      ariaInvalid={Boolean($errors.text)}
      errors={$errors.text}
      label="Eintrag erledigen"
      bind:value={$form.text}
    >
      <div class="row c-vlight gap-2 flex-wrap">
        <button class="btn btn-sm btn-primary" type="submit">Erledigt</button>
        <EntryTodoButton />
      </div>
    </MarkdownInput>
    <input name="old_execution_id" type="hidden" bind:value={$form.old_execution_id} />
  </form>
{/if}
