<script setup lang="ts">
import type { ExposedForm } from '~/types/form';
import Action from '~/components/pub/my-ui/nav-footer/ba-dr-su.vue'
import Confirmation from '~/components/pub/my-ui/confirmation/confirmation.vue'
import { ActionEvents } from '~/components/pub/my-ui/data/types'

// #region Props & Emits
const props = defineProps<{
  callbackUrl?: string
}>()

// form related state
const personPatientForm = ref<ExposedForm<any> | null>(null)
// #endregion

// #region State & Computed
const router = useRouter()
const isConfirmationOpen = ref(false)

const recId = ref<number>(0)
const recAction = ref<string>('')
const recItem = ref<any>(null)
const recDate = ref<any>(null)
// #endregion

// #region Lifecycle Hooks
// #endregion

// #region Functions
function goBack() {
  router.go(-1)
}

async function handleConfirmAdd() {
  // handleActionClick('submit')
  console.log(`tersubmit wak`)
}

function handleCancelAdd() {
  isConfirmationOpen.value = false
}
// #endregion region

// #region Utilities & event handlers
async function handleActionClick(eventType: string) {
  if (eventType === 'submit') {
    isConfirmationOpen.value = true
    // const patient: Patient = await composeFormData()
    // let createdPatientId = 0

    // const response = await handleActionSave(
    //   patient,
    //   () => {},
    //   () => {},
    //   toast,
    // )

    // const data = (response?.body?.data ?? null) as PatientBase | null
    // if (!data) return
    // createdPatientId = data.id

    // If has callback provided redirect to callback with patientData
    // if (props.callbackUrl) {
    //   await navigateTo(props.callbackUrl + '?patient-id=' + patient.id)
    //   return
    // }

    // Navigate to patient list or show success message
    // await navigateTo('/outpatient/encounter')
    // return
  }

  if (eventType === 'back') {
    if (props.callbackUrl) {
      await navigateTo(props.callbackUrl)
      return
    }

    goBack()
    // handleCancelForm()
  }
}
// #endregion

// #region Provide
provide('rec_id', recId)
provide('rec_action', recAction)
provide('rec_item', recItem)
provide('rec_date', recDate) // for divergence if we trigger same action on same Record Item multiple times
// #endregion

// #region Watchers
watch([recId, recAction, recDate], () => {
  switch (recAction.value) {
    case ActionEvents.showConfirmVerify:
      handleActionClick('submit')
      break
  }
})
// #endregion
</script>

<template>
  <AppTherapyProtocolDetail />

  <Confirmation
    v-model:open="isConfirmationOpen"
    title="Konfirmasi"
    message="Apakah Anda yakin ingin Konfirmasi data ini?"
    confirm-text="Konfirmasi"
    @confirm="handleConfirmAdd"
    @cancel="handleCancelAdd"

  />
</template>
