<script lang="ts" setup>
//
import { LucideCheck } from 'lucide-vue-next';
import { useForm } from 'vee-validate'
import { toTypedSchema } from '@vee-validate/zod'

// Components
import type z from 'zod'
import ComboBox from '~/components/pub/my-ui/combobox/combobox.vue'
import * as DE from '~/components/pub/my-ui/doc-entry'
import type { CheckInFormData } from '~/schemas/encounter.schema'
import type { Encounter } from '~/models/encounter'
import { now } from '@internationalized/date';

interface Props {
  schema: z.ZodSchema<any>
  values: any
  doctors: { value: string; label: string }[]
  // employees: { value: string; label: string }[]
  encounter: Encounter
  isLoading?: boolean
  isReadonly?: boolean
}
const props = defineProps<Props>()

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

const { defineField, errors, meta } = useForm({
  validationSchema: toTypedSchema(props.schema),
  initialValues: {
    responsible_doctor_id: 0,
    adm_employee_id: 0,
    registeredAt: props.values.values?.registeredAt || '',
  } as Partial<CheckInFormData>,
})

const [responsible_doctor_code, responsible_doctor_codeAttrs] = defineField('responsible_doctor_code')
// const [adm_employee_id, adm_employee_idAttrs] = defineField('discharge_method_code')
const [registeredAt, registeredAtAttrs] = defineField('registeredAt')

function submitForm() {
  const formData: CheckInFormData = {
    responsible_doctor_code: responsible_doctor_code.value,
    // adm_employee_id: adm_employee_id.value,
    registeredAt: registeredAt.value || '',
  }
  emit('submit', formData)
}

function setTime() {
  const today = new Date()
  registeredAt.value = today.toISOString().substring(0, 10) + ' ' + today.toLocaleTimeString('id-ID').substring(0, 5).replace('.', ':');
}
</script>

<template>
  <DE.Block :cell-flex="false">
    <DE.Cell>
      <DE.Label>Dokter</DE.Label>
      <DE.Field>
        <ComboBox
          id="doctor"
          v-model="responsible_doctor_code"
          v-bind="responsible_doctor_codeAttrs"
          :items="doctors"
          :disabled="isLoading || isReadonly"
          placeholder="Pilih Dokter DPJP"
          search-placeholder="Pilih DPJP"
          empty-message="DPJP tidak ditemukan"
        />
      </DE.Field>
    </DE.Cell>
    <!-- <DE.Cell>
      <DE.Label>PJ Berkas</DE.Label>
      <DE.Field>
        <ComboBox
          id="doctor"
          v-model="adm_employee_id"
          v-bind="adm_employee_idAttrs"
          :items="employees"
          :disabled="isLoading || isReadonly"
          placeholder="Pilih Dokter DPJP"
          search-placeholder="Pilih petugas"
          empty-message="Petugas tidak ditemukan"
        />
      </DE.Field>
    </DE.Cell> -->
    <DE.Cell>
      <DE.Label>Waktu Masuk</DE.Label>
      <DE.Field>
        <Input
            id="name"
            v-model="registeredAt"
            v-bind="registeredAtAttrs"
            @click="setTime"
            readonly
          />
      </DE.Field>
    </DE.Cell>
  </DE.Block>
  <div class="text-center">
    <Button @click="submitForm">
      <LucideCheck />
      Simpan
    </Button>
  </div>
</template>

<style>

</style>
