<script setup lang="ts">
import type { FormErrors } from '~/types/error'
import Combobox from '~/components/pub/my-ui/combobox/combobox.vue'
import { cn } from '~/lib/utils'
import { getValueLabelList as getDoctorLabelList } from '~/services/doctor.service'

import * as DE from '~/components/pub/my-ui/doc-entry'
import type { Item } from '~/components/pub/my-ui/combobox'

const props = defineProps<{
  fieldName?: string
  label?: string
  placeholder?: string
  errors?: FormErrors
  class?: string
  selectClass?: string
  fieldGroupClass?: string
  labelClass?: string
  isRequired?: boolean
}>()

const {
  fieldName = 'job',
  label = 'Pekerjaan',
  placeholder = 'Pilih pekerjaan',
  errors,
  class: containerClass,
  fieldGroupClass,
  labelClass,
} = props

const doctors = ref<Array<Item>>([])

async function fetchDpjp() {
  doctors.value = await getDoctorLabelList({
    serviceType: 1,
    serviceDate: new Date().toISOString().substring(0, 10),
    includes: 'employee-person',
  }, true)
}

onMounted(() => {
  fetchDpjp()
})

// function handleDpjpChange(selected: string) {
//   selectedDpjpId.value = selected ?? null
// }
</script>

<template>
  <DE.Cell :class="cn('select-field-group', fieldGroupClass, containerClass)">
    <DE.Label
      :label-for="fieldName"
      :class="cn('select-field-label', labelClass)"
      :is-required="isRequired"
    >
      {{ label }}
    </DE.Label>
    <DE.Field
      :id="fieldName"
      :errors="errors"
      :class="cn('select-field-wrapper')"
    >
      <FormField
        v-slot="{ componentField }"
        :name="fieldName"
      >
        <FormItem>
          <FormControl>
            <Combobox
              class="focus:ring-0 focus:ring-offset-0"
              :id="fieldName"
              v-bind="componentField"
              :items="doctors"
              :placeholder="placeholder"
              search-placeholder="Cari..."
              empty-message="Data tidak ditemukan"
            />
          </FormControl>
          <FormMessage />
        </FormItem>
      </FormField>
    </DE.Field>
  </DE.Cell>
</template>
