<script setup lang="ts">
import { z } from 'zod'
import Entry from '~/components/app/general-consent/entry.vue'
import Action from '~/components/pub/my-ui/nav-footer/ba-dr-su-pr.vue'
import ActionDialog from '~/components/pub/my-ui/nav-footer/ba-su.vue'
import Dialog from '~/components/pub/my-ui/modal/dialog.vue'
import { GeneralConsentSchema } from '~/schemas/general-consent.schema'
import { toast } from '~/components/pub/ui/toast'
import { handleActionSave, handleActionEdit } from '~/handlers/general-consent.handler'
import { create } from '~/services/generate-file.service'
const { backToList } = useQueryMode('mode')

const route = useRoute()
const isOpenProcedure = ref(false)
const isOpenDiagnose = ref(false)
const isOpenFungsional = ref(false)
const procedures = ref([])
const diagnoses = ref([])
const fungsional = ref([])
const selectedProcedure = ref<any>(null)
const selectedDiagnose = ref<any>(null)
const selectedFungsional = ref<any>(null)
const schema = GeneralConsentSchema
const payload = ref({
  encounter_id: 0,
  value: '',
})
const model = ref({
  relatives: [],
  responsibleName: '',
  responsiblePhone: '',
  informant: '',
  witness1: '',
  witness2: '',
})

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

async function getDiagnoses() {
  isLoading.isTableLoading = true
  const resp = await xfetch('/api/v1/diagnose-src')
  if (resp.success) {
    diagnoses.value = (resp.body as Record<string, any>).data
  }
  isLoading.isTableLoading = false
}

async function getProcedures() {
  isLoading.isTableLoading = true
  const resp = await xfetch('/api/v1/procedure-src')
  if (resp.success) {
    procedures.value = (resp.body as Record<string, any>).data
  }
  isLoading.isTableLoading = false
}

onMounted(() => {
  getProcedures()
  getDiagnoses()
})

function handleClick(type: string) {
  if (type === 'prosedur') {
    isOpenProcedure.value = true
  } else if (type === 'diagnosa') {
    isOpenDiagnose.value = true
  } else if (type === 'fungsional') {
    isOpenDiagnose.value = true
  }
}
const entryGeneralConsent = ref()
async function actionHandler(type: string) {
  if (type === 'back') {
    backToList()
    return
  }
  if (type === 'print') {
    console.log('print')
    isOpenDiagnose.value = true
    return
  }
  const result = await entryGeneralConsent.value?.validate()
  if (result?.valid) {
    if (result.data.relatives.length > 0) {
      result.data.relatives = result.data.relatives.map((item: any) => {
        return item.name
      })
    }

    console.log('data', result)
    const resp = await handleActionSave(
      {
        ...payload.value,
        value: JSON.stringify(result.data),
        encounter_id: +route.params.id,
      },
      () => {},
      () => {},
      toast,
    )
    const data = resp.body?.data
    if (data) {
      const resp2 = await create({
        entityType_code: 'encounter',
        ref_id: +data?.id,
        type_code: 'general-consent',
      })
      console.log('resp2', resp2.body?.data)
    }
  } 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 || []
    // icdPreview.value.fungsional = selectedFungsional.value || []
  }
  isOpenProcedure.value = false
  isOpenDiagnose.value = false
}

provide('table_data_loader', isLoading)
provide('icdPreview', icdPreview)
</script>
<template>
  <Entry
    ref="entryGeneralConsent"
    v-model="model"
    :schema="schema"
    @click="handleClick"
  />
  <div class="my-2 flex justify-end py-2">
    <Action @click="actionHandler" />
  </div>
  <Dialog
    v-model:open="isOpenDiagnose"
    title="Preview General Content"
    size="xl"
    prevent-outside
  >
    <div class="my-2 flex justify-end py-2">
      <ActionDialog @click="actionDialogHandler" />
    </div>
  </Dialog>
</template>
