<script setup lang="ts">
import type { DataTableLoader } from '~/components/pub/my-ui/data-table/type'
import type { HeaderPrep, RefSearchNav } from '~/components/pub/my-ui/data/types'
import Modal from '~/components/pub/my-ui/modal/modal.vue'
import Header from '~/components/pub/my-ui/nav-header/prep.vue'

const data = ref([])
const entry = ref<any>({})

const refSearchNav: RefSearchNav = {
  onClick: () => {
    // open filter modal
  },
  onInput: (_val: string) => {
    // filter patient list
  },
  onClear: () => {
    // clear url param
  },
}

// Loading state management
const isLoading = reactive<DataTableLoader>({
  summary: false,
  isTableLoading: false,
})

const isOpen = ref(false)

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

const hreaderPrep: HeaderPrep = {
  title: 'Golongan Obat',
  icon: 'i-lucide-users',
  addNav: {
    label: 'Tambah',
    onClick: () => (isOpen.value = true),
  },
}

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

onMounted(() => {
  getPatientList()
})

provide('rec_id', recId)
provide('rec_action', recAction)
provide('rec_item', recItem)
provide('table_data_loader', isLoading)
</script>

<template>
  <Header :prep="{ ...hreaderPrep }" :ref-search-nav="refSearchNav" />

  <AppMedicineGroupList :data="data" />

  <Modal v-model:open="isOpen" title="Tambah Golongan Obat" size="lg" prevent-outside>
    <AppMedicineGroupEntryForm v-model="entry" />
  </Modal>
</template>
