<script setup lang="ts">
// Components
import Block from '~/components/pub/my-ui/doc-entry/block.vue'
import Cell from '~/components/pub/my-ui/doc-entry/cell.vue'
import Field from '~/components/pub/my-ui/doc-entry/field.vue'
import Label from '~/components/pub/my-ui/doc-entry/label.vue'
import Button from '~/components/pub/ui/button/Button.vue'
import Select from '~/components/pub/my-ui/form/select.vue'

// Types
import type { MedicalActionSrcFormData } from '~/schemas/medical-action-src.schema'
import { medicalActionTypeCode } from '~/lib/constants'

// Helpers
import type z from 'zod'
import { useForm } from 'vee-validate'
import { toTypedSchema } from '@vee-validate/zod'
import { mapToComboboxOptList } from '~/lib/utils'

interface Props {
  schema: z.ZodSchema<any>
  values: any
  isLoading?: boolean
  isReadonly?: boolean
}

const props = defineProps<Props>()
const isLoading = props.isLoading !== undefined ? props.isLoading : false
const isReadonly = props.isReadonly !== undefined ? props.isReadonly : false
const medicalActionTypeOptions = mapToComboboxOptList(medicalActionTypeCode)

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

const { defineField, errors, meta } = useForm({
  validationSchema: toTypedSchema(props.schema),
  initialValues: {
    code: '',
    name: '',
    type_code: '',
  } as Partial<MedicalActionSrcFormData>,
})

const [code, codeAttrs] = defineField('code')
const [name, nameAttrs] = defineField('name')
const [typeCode, typeCodeAttrs] = defineField('type_code')

if (props.values) {
  if (props.values.code !== undefined) code.value = props.values.code
  if (props.values.name !== undefined) name.value = props.values.name
  if (props.values.type_code !== undefined) typeCode.value = props.values.type_code
}

const resetForm = () => {
  code.value = ''
  name.value = ''
  typeCode.value = ''
}

function onSubmitForm() {
  const formData: MedicalActionSrcFormData = {
    code: code.value || '',
    name: name.value || '',
    type_code: typeCode.value || '',
  }
  emit('submit', formData, resetForm)
}

function onCancelForm() {
  emit('cancel', resetForm)
}
</script>

<template>
  <form
    id="form-medical-action-src"
    @submit.prevent
  >
    <Block
      labelSize="thin"
      class="!mb-2.5 !pt-0 xl:!mb-3"
      :colCount="1"
    >
      <Cell>
        <Label height="compact">Kode</Label>
        <Field :errMessage="errors.code">
          <Input
            id="code"
            v-model="code"
            v-bind="codeAttrs"
            :disabled="isLoading || isReadonly"
          />
        </Field>
      </Cell>
      <Cell>
        <Label height="compact">Nama</Label>
        <Field :errMessage="errors.name">
          <Input
            id="name"
            v-model="name"
            v-bind="nameAttrs"
            :disabled="isLoading || isReadonly"
          />
        </Field>
      </Cell>
      <Cell>
        <Label height="compact">Type Kode</Label>
        <Field :errMessage="errors.name">
          <Select
            id="type_code"
            :is-disabled="isLoading || isReadonly"
            :items="medicalActionTypeOptions"
            v-bind="typeCodeAttrs"
            v-model="typeCode"
            placeholder="Pilih medical action type"
            :preserve-order="false"
            class="text-sm transition-all duration-200 focus:outline-none focus:ring-1 focus:ring-black focus:ring-offset-0"
          />
        </Field>
      </Cell>
    </Block>
    <div class="my-2 flex justify-end gap-2 py-2">
      <Button
        type="button"
        variant="secondary"
        class="w-[120px]"
        @click="onCancelForm"
      >
        Kembali
      </Button>
      <Button
        v-if="!isReadonly"
        type="button"
        class="w-[120px]"
        :disabled="isLoading || !meta.valid"
        @click="onSubmitForm"
      >
        Simpan
      </Button>
    </div>
  </form>
</template>
