<script setup lang="ts">
import { z } from 'zod'
import Entry from '~/components/app/initial-nursing/entry-form.vue'
import Action from '~/components/pub/my-ui/nav-footer/ba-dr-su.vue'
import ActionDialog from '~/components/pub/my-ui/nav-footer/ba-su.vue'
import Dialog from '~/components/pub/my-ui/modal/dialog.vue'
import { InitialNursingSchema } from '~/schemas/soapi.schema'
import { toast } from '~/components/pub/ui/toast'
import { handleActionSave, handleActionEdit } from '~/handlers/soapi-early.handler'
const { backToList } = useQueryMode('mode')

const route = useRoute()
const isOpenProcedure = ref(false)
const isOpenDiagnose = ref(false)
const procedures = ref([])
const diagnoses = ref([])
const selectedProcedure = ref<any>(null)
const selectedDiagnose = ref<any>(null)
const schema = InitialNursingSchema
const payload = ref({
  encounter_id: 0,
  time: '',
  typeCode: 'early-nursery',
  value: '',
})
const listProblem = ref([])

const model = ref({
  'pri-complain': '',
  'med-type': '',
  'med-name': '',
  'med-reaction': '',
  'food-type': '',
  'food-name': '',
  'food-reaction': '',
  'other-type': '',
  'other-name': '',
  'other-reaction': '',
  'pain-asst': '',
  'pain-scale': '',
  'pain-time': '',
  'pain-duration': '',
  'pain-freq': '',
  'pain-loc': '',
  'nut-screening': '',
  'spiritual-asst': '',
  'general-condition': '',
  'support-exam': '',
  'risk-fall': '',
  bracelet: '',
  'bracelet-alg': '',
})

const isLoading = reactive<DataTableLoader>({
  isTableLoading: false,
})

function handleOpen(event: any) {
  console.log('handleOpen', event.type)
  const type = event.type
  if (type === 'add-problem') {
    listProblem.value = event.data
  }
}

const entryRehabRef = ref()
async function actionHandler(type: string) {
  if (type === 'back') {
    backToList()
    return
  }
  const result = await entryRehabRef.value?.validate()
  if (result?.valid) {
    if (listProblem.value?.length > 0) {
      result.data.listProblem = listProblem.value || []
    }
    console.log('data', result.data)
    handleActionSave(
      {
        ...payload.value,
        value: JSON.stringify(result.data),
        encounter_id: +route.params.id,
        time: new Date().toISOString(),
      },
      () => {},
      () => {},
      toast,
    )

    backToList()
  } else {
    console.log('Ada error di form', result)
  }
}

const icdPreview = ref({
  procedures: [],
  diagnoses: [],
})

function actionDialogHandler(type: string) {
  if (type === 'submit') {
    icdPreview.value.procedures = selectedProcedure.value || []
    icdPreview.value.diagnoses = selectedDiagnose.value || []
  }
  isOpenProcedure.value = false
  isOpenDiagnose.value = false
}

provide('table_data_loader', isLoading)
provide('icdPreview', icdPreview)
</script>
<template>
  <Entry
    ref="entryRehabRef"
    v-model="model"
    :schema="schema"
    type="early-rehab"
    @click="handleOpen"
  />
  <div class="my-2 flex justify-end py-2">
    <Action @click="actionHandler" />
  </div>
  <Dialog
    v-model:open="isOpenDiagnose"
    title="Pilih Fungsional"
    size="xl"
    prevent-outside
  >
    <AppIcdMultiselectPicker
      v-model:model-value="selectedDiagnose"
      :data="diagnoses"
    />
    <div class="my-2 flex justify-end py-2">
      <ActionDialog @click="actionDialogHandler" />
    </div>
  </Dialog>
</template>
