<script setup lang="ts">
import type { FormErrors } from '~/types/error'
import { toTypedSchema } from '@vee-validate/zod'
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'
import Select from '~/components/pub/my-ui/form/select.vue'
import { Form } from '~/components/pub/ui/form'

interface InstallationFormData {
  name: string
  code: string
  encounterClassCode: string
}

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

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

const formSchema = toTypedSchema(props.schema)

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

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

const items = ref([
  { label: 'Rujukan Internal', value: 'ri' },
  { label: 'SEP Rujukan', value: 'sr' },
])
</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="parentId">Cara Bayar</Label>
            <Field id="encounterClassCode" :errors="errors">
              <FormField v-slot="{ componentField }" name="encounterClassCode">
                <FormItem>
                  <FormControl>
                    <Select v-bind="componentField" :items="items" />
                  </FormControl>
                  <FormMessage />
                </FormItem>
              </FormField>
            </Field>
          </FieldGroup>
          <FieldGroup>
            <Label label-for="parentId">Poliklinik</Label>
            <Field id="encounterClassCode" :errors="errors">
              <FormField v-slot="{ componentField }" name="encounterClassCode">
                <FormItem>
                  <FormControl>
                    <Select v-bind="componentField" :items="items" />
                  </FormControl>
                  <FormMessage />
                </FormItem>
              </FormField>
            </Field>
          </FieldGroup>
          <FieldGroup>
            <Label label-for="parentId">Kunjungan</Label>
            <Field id="encounterClassCode" :errors="errors">
              <FormField v-slot="{ componentField }" name="encounterClassCode">
                <FormItem>
                  <FormControl>
                    <Select v-bind="componentField" :items="items" />
                  </FormControl>
                  <FormMessage />
                </FormItem>
              </FormField>
            </Field>
          </FieldGroup>
        </div>
      </div>
    </form>
  </Form>
</template>
