<script setup lang="ts">
import type { FormErrors } from '~/types/error'
import { toTypedSchema } from '@vee-validate/zod'
import Block from '~/components/pub/my-ui/form/block.vue'
import Combobox from '~/components/pub/my-ui/combobox/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'
import { Form } from '~/components/pub/ui/form'
import DatepickerSingle from '~/components/pub/my-ui/datepicker/datepicker-single.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)
}
const doctorOpts = ref([
  { label: 'Pilih', value: null },
  { label: 'Dr. A', value: 1 },
])
const paymentOpts = ref([
  { label: 'Umum', value: 'umum' },
  { label: 'BPJS', value: 'bpjs' },
])
const sepOpts = ref([
  { label: 'Rujukan Internal', value: 'ri' },
  { label: 'SEP Rujukan', value: 'sr' },
])

// file refs untuk tombol "Pilih Berkas"
const sepFileInput = ref<HTMLInputElement | null>(null)
const sippFileInput = ref<HTMLInputElement | null>(null)

function pickSepFile() {
  sepFileInput.value?.click()
}
function pickSippFile() {
  sippFileInput.value?.click()
}

function onSepFileChange(e: Event) {
  const f = (e.target as HTMLInputElement).files?.[0]
  // set ke form / emit / simpan di state sesuai form library-mu
  console.log('sep file', f)
}

function onSippFileChange(e: Event) {
  const f = (e.target as HTMLInputElement).files?.[0]
  console.log('sipp file', f)
}

function onAddSep() {
  // contoh handler tombol "+" di sebelah No. SEP
  console.log('open modal tambah SEP')
}
</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">
          <div class="p-2">
            <h2 class="text-md font-semibold">Data Kunjungan</h2>
          </div>
          <Block>
            <!-- Tanggal Daftar (DatePicker) -->
            <FieldGroup :column="3">
              <Label label-for="register_date">Dengan Rujukan</Label>
              <Field id="register_date" :errors="errors">
                <FormField v-slot="{ componentField }" name="register_date">
                  <FormItem>
                    <FormControl>
                      <Input
                        id="bpjs_number"
                        v-bind="componentField"
                        placeholder="Pilih jenis pembayaran terlebih dahulu"
                      />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                </FormField>
              </Field>
            </FieldGroup>

            <!-- Jenis Pembayaran (Combobox) -->
            <FieldGroup :column="3">
              <Label label-for="payment_type">Rujukan</Label>
              <Field id="payment_type" :errors="errors">
                <FormField v-slot="{ componentField }" name="payment_type">
                  <FormItem>
                    <FormControl>
                      <!-- <Combobox id="payment_type" v-bind="componentField" :items="paymentOpts" /> -->
                      <Select id="payment_type" v-bind="componentField" :items="paymentOpts" />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                </FormField>
              </Field>
            </FieldGroup>

            <FieldGroup :column="3">
              <Label label-for="bpjs_number">No. Rujukan</Label>
              <Field id="bpjs_number" :errors="errors">
                <FormField v-slot="{ componentField }" name="bpjs_number">
                  <FormItem>
                    <FormControl>
                      <Input
                        id="bpjs_number"
                        v-bind="componentField"
                        placeholder="Pilih jenis pembayaran terlebih dahulu"
                      />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                </FormField>
              </Field>
            </FieldGroup>

            <!-- No. Kartu BPJS -->
            <FieldGroup :column="2">
              <Label label-for="bpjs_number">Tanggal Rujukan</Label>
              <Field id="bpjs_number" :errors="errors">
                <FormField v-slot="{ componentField }" name="bpjs_number">
                  <FormItem>
                    <FormControl>
                      <DatepickerSingle v-bind="componentField" placeholder="Pilih tanggal" />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                </FormField>
              </Field>
            </FieldGroup>

            <!-- Jenis SEP -->
            <FieldGroup :column="2">
              <Label label-for="sep_type">Diagnosis</Label>
              <Field id="sep_type" :errors="errors">
                <FormField v-slot="{ componentField }" name="sep_type">
                  <FormItem>
                    <FormControl>
                      <Select id="sep_type" v-bind="componentField" :items="sepOpts" />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                </FormField>
              </Field>
            </FieldGroup>
            <!-- No. SEP (input + tombol +) -->

            <FieldGroup>
              <Label label-for="sep_type">Status Kecelakaan</Label>
              <Field id="sep_type" :errors="errors">
                <FormField v-slot="{ componentField }" name="sep_type">
                  <FormItem>
                    <FormControl>
                      <Select id="sep_type" v-bind="componentField" :items="sepOpts" />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                </FormField>
              </Field>
            </FieldGroup>
          </Block>
        </div>
      </div>
    </form>
  </Form>
</template>
