<script setup lang="ts">
import type { FormErrors } from '~/types/error'
import { toTypedSchema } from '@vee-validate/zod'
import Combobox from '~/components/pub/my-ui/form/combobox.vue'
import FieldGroup from '~/components/pub/my-ui/form/field-group.vue'
import Field from '~/components/pub/my-ui/form/field.vue'
import Label from '~/components/pub/my-ui/form/label.vue'

interface UnitFormData {
  name: string
  code: string
  parentId: string
}

const props = defineProps<{
  unit: {
    msg: {
      placeholder: string
      search: string
      empty: string
    }
    items: {
      value: string
      label: string
      code: string
    }[]
  }
  schema: any
  initialValues?: Partial<UnitFormData>
  errors?: FormErrors
}>()

const emit = defineEmits<{
  'submit': [values: UnitFormData, resetForm: () => void]
  'cancel': [resetForm: () => void]
}>()

const formSchema = toTypedSchema(props.schema)

// Form submission handler
function onSubmitForm(values: any, { resetForm }: { resetForm: () => void }) {
  const formData: UnitFormData = {
    name: values.name || '',
    code: values.code || '',
    parentId: values.parentId || '',
  }
  emit('submit', formData, resetForm)
}

// Form cancel handler
function onCancelForm({ resetForm }: { resetForm: () => void }) {
  emit('cancel', resetForm)
}
</script>

<template>
  <Form
    v-slot="{ handleSubmit, resetForm }" as="" keep-values :validation-schema="formSchema"
    :initial-values="initialValues"
    >
    <form id="entry-form" @submit="handleSubmit($event, (values) => onSubmitForm(values, { resetForm }))">
      <div class="mb-5 border-b border-b-slate-300 pb-3 text-lg xl:text-xl">
        <div class="flex flex-col justify-between">
          <FieldGroup>
            <Label label-for="name">Nama</Label>
            <Field id="name" :errors="errors">
              <FormField v-slot="{ componentField }" name="name">
                <FormItem>
                  <FormControl>
                    <Input
                      id="name" type="text" placeholder="Masukkan nama unit" autocomplete="off"
                      v-bind="componentField"
                      />
                  </FormControl>
                  <FormMessage />
                </FormItem>
              </FormField>
            </Field>
          </FieldGroup>

          <FieldGroup>
            <Label label-for="code">Kode</Label>
            <Field id="code" :errors="errors">
              <FormField v-slot="{ componentField }" name="code">
                <FormItem>
                  <FormControl>
                    <Input id="code" type="text" placeholder="Masukkan kode unit" autocomplete="off" v-bind="componentField" />
                  </FormControl>
                  <FormMessage />
                </FormItem>
              </FormField>
            </Field>
          </FieldGroup>

          <FieldGroup>
            <Label label-for="parentId">Instalasi</Label>
            <Field id="parentId" :errors="errors">
              <FormField v-slot="{ componentField }" name="parentId">
                <FormItem>
                  <FormControl>
                    <Combobox
                      id="parentId" v-bind="componentField" :items="unit.items"
                      :placeholder="unit.msg.placeholder" :search-placeholder="unit.msg.search"
                      :empty-message="unit.msg.empty"
                      />
                  </FormControl>
                  <FormMessage />
                </FormItem>
              </FormField>
            </Field>
          </FieldGroup>
        </div>
      </div>

      <div class="flex justify-end gap-2 mt-4">
        <Button type="button" variant="outline" @click="onCancelForm({ resetForm })">
          Batal
        </Button>
        <Button type="submit">
          Simpan
        </Button>
      </div>
    </form>
  </Form>
</template>
