<script setup lang="ts">
import type { FormErrors } from '~/types/error'
import { toTypedSchema } from '@vee-validate/zod'
import Block from '~/components/pub/custom-ui/form/block.vue'
import Combobox from '~/components/pub/custom-ui/form/combobox.vue'
import FieldGroup from '~/components/pub/custom-ui/form/field-group.vue'
import Field from '~/components/pub/custom-ui/form/field.vue'
import Label from '~/components/pub/custom-ui/form/label.vue'
import { educationCodes, genderCodes, occupationCodes, religionCodes, relationshipCodes } from '~/lib/constants'
import { mapToComboboxOptList } from '~/lib/utils'

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

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

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

const relationshipOpts = mapToComboboxOptList(relationshipCodes)
const educationOpts = mapToComboboxOptList(educationCodes)
const occupationOpts = mapToComboboxOptList(occupationCodes)
const genderOpts = mapToComboboxOptList(genderCodes)

const formSchema = toTypedSchema(props.schema)

// Form submission handler
function onSubmitForm(values: any, { resetForm }: { resetForm: () => void }) {
  const formData: DivisionFormData = {
    name: values.name || '',
    code: values.code || '',
    parentId: values.parentId || '',
  }
  emit('submit', formData, 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">
          <Block>
            <!-- Relationship_Code -->
            <FieldGroup>
              <Label label-for="relationshipCode">Hubungan</Label>
              <Field id="relationshipCode" :errors="errors">
                <FormField v-slot="{ componentField }" name="relationshipCode">
                  <FormItem>
                    <FormControl>
                      <Select id="relationshipCode" v-bind="componentField" :items="relationshipOpts" />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                </FormField>
              </Field>
            </FieldGroup>

            <!-- Name -->
            <FieldGroup>
              <Label label-for="relativeName">Nama</Label>
              <Field id="relativeName" :errors="errors">
                <FormField v-slot="{ componentField }" name="relativeName">
                  <FormItem>
                    <FormControl>
                      <Input
                        id="relativeName"
                        type="text"
                        placeholder="Masukkan nama kerabat"
                        autocomplete="off"
                        v-bind="componentField"
                      />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                </FormField>
              </Field>
            </FieldGroup>

            <!-- Address -->
            <FieldGroup>
              <Label label-for="relativeAddress">Alamat</Label>
              <Field id="relativeAddress" :errors="errors">
                <FormField v-slot="{ componentField }" name="relativeAddress">
                  <FormItem>
                    <FormControl>
                      <Input
                        id="relativeAddress"
                        type="text"
                        placeholder="Alamat kerabat"
                        autocomplete="off"
                        v-bind="componentField"
                      />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                </FormField>
              </Field>
            </FieldGroup>

            <!-- Village_Code -->
            <FieldGroup>
              <Label label-for="relativeVillageCode">Desa/Kelurahan</Label>
              <Field id="relativeVillageCode" :errors="errors">
                <FormField v-slot="{ componentField }" name="relativeVillageCode">
                  <FormItem>
                    <FormControl>
                      <Combobox id="relativeVillageCode" v-bind="componentField" :items="genderOpts" />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                </FormField>
              </Field>
            </FieldGroup>

            <!-- Gender_Code -->
            <FieldGroup>
              <Label label-for="relativeGender">Jenis Kelamin</Label>
              <Field id="relativeGender" :errors="errors">
                <FormField v-slot="{ componentField }" name="relativeGender">
                  <FormItem>
                    <FormControl>
                      <Select id="relativeGender" v-bind="componentField" :items="genderOpts" />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                </FormField>
              </Field>
            </FieldGroup>

            <!-- PhoneNumber -->
            <FieldGroup>
              <Label label-for="phoneNumber">Nomor Telepon</Label>
              <Field id="phoneNumber" :errors="errors">
                <FormField v-slot="{ componentField }" name="phoneNumber">
                  <FormItem>
                    <FormControl>
                      <Input
                        id="phoneNumber"
                        type="text"
                        placeholder="0812xxxxxx"
                        autocomplete="off"
                        v-bind="componentField"
                      />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                </FormField>
              </Field>
            </FieldGroup>

            <!-- Education_Code -->
            <FieldGroup>
              <Label label-for="relativeEducation">Pendidikan</Label>
              <Field id="relativeEducation" :errors="errors">
                <FormField v-slot="{ componentField }" name="relativeEducation">
                  <FormItem>
                    <FormControl>
                      <Select id="relativeEducation" v-bind="componentField" :items="educationOpts" />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                </FormField>
              </Field>
            </FieldGroup>

            <!-- Occupation_Code -->
            <FieldGroup>
              <Label label-for="relativeOccupationCode">Pekerjaan</Label>
              <Field id="relativeOccupationCode" :errors="errors">
                <FormField v-slot="{ componentField }" name="relativeOccupationCode">
                  <FormItem>
                    <FormControl>
                      <Select id="relativeOccupationCode" v-bind="componentField" :items="occupationOpts" />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                </FormField>
              </Field>
            </FieldGroup>

            <!-- Occupation_Name -->
            <FieldGroup>
              <Label label-for="relativeOccupationName">Nama Pekerjaan</Label>
              <Field id="relativeOccupationName" :errors="errors">
                <FormField v-slot="{ componentField }" name="relativeOccupationName">
                  <FormItem>
                    <FormControl>
                      <Input
                        id="relativeOccupationName"
                        type="text"
                        placeholder="Contoh: Guru, Dokter, Petani"
                        autocomplete="off"
                        v-bind="componentField"
                      />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                </FormField>
              </Field>
            </FieldGroup>
          </Block>
        </div>
      </div>
    </form>
  </Form>
</template>
