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

const data = ref([])

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

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

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

const headerPrep: HeaderPrep = {
  title: 'Alat Kesehatan',
  icon: 'i-lucide-paint-bucket',
  addNav: {
    label: 'Tambah',
    onClick: () => navigateTo('/tools-equipment-src/device/add'),
  },
}

async function getMaterialList() {
  isLoading.dataListLoading = true

  const resp = await xfetch('/api/v1/device')
  if (resp.success) {
    data.value = (resp.body as Record<string, any>).data
  }

  isLoading.dataListLoading = false
}

onMounted(() => {
  getMaterialList()
})

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

<template>
  <div class="rounded-md border p-4">
    <Header :prep="{ ...headerPrep }" :ref-search-nav="refSearchNav" />
    <div class="rounded-md border p-4">
      <AppMaterialList v-if="!isLoading.dataListLoading" :data="data" />
    </div>
  </div>
</template>
