<script setup lang="ts">
// Components
import Dialog from '~/components/pub/my-ui/modal/dialog.vue'
import Header from '~/components/pub/my-ui/nav-header/prep.vue'
import AppRoomList from '~/components/app/room/list.vue'
import AppRoomEntryForm from '~/components/app/room/entry-form.vue'
import RecordConfirmation from '~/components/pub/my-ui/confirmation/record-confirmation.vue'

// Constants
import { infraGroupCodesKeys } from '~/lib/constants'

// Helpers
import { usePaginatedList } from '~/composables/usePaginatedList'
import { toast } from '~/components/pub/ui/toast'

// Types
import { ActionEvents, type HeaderPrep } from '~/components/pub/my-ui/data/types'
import { InfraSchema, type InfraFormData } from '~/schemas/infra.schema'

// Handlers
import {
  recId,
  recAction,
  recItem,
  isReadonly,
  isProcessing,
  isFormEntryDialogOpen,
  isRecordConfirmationOpen,
  onResetState,
  handleActionSave,
  handleActionEdit,
  handleActionRemove,
  handleCancelForm,
} from '~/handlers/infra.handler'

// Services
import { getList, getDetail, getValueLabelList } from '~/services/infra.service'
import { getValueLabelList as getSpecialistList } from '~/services/specialist.service'
import { getValueLabelList as getSubspecialistList } from '~/services/subspecialist.service'
import { getValueLabelList as getUnitList } from '~/services/unit.service'
import { getDetail as getItemDetail } from '~/services/item.service'

const parents = ref<{ value: string | number; label: string }[]>([])
const specialists = ref<{ value: string | number; label: string }[]>([])
const specialistsFiltered = ref<{ value: string | number; label: string }[]>([])
const subspecialists = ref<{ value: string | number; label: string }[]>([])
const subspecialistsFiltered = ref<{ value: string | number; label: string }[]>([])
const units = ref<{ value: string | number; label: string }[]>([])
const selectedUnit = ref<string | number | null>(null)
const selectedSpecialist = ref<string | number | null>(null)
const title = ref('')

const {
  data,
  isLoading,
  paginationMeta,
  searchInput,
  handlePageChange,
  handleSearch,
  fetchData: getItemList,
} = usePaginatedList({
  fetchFn: async (params: any) => {
    const result = await getList({
      search: params.search,
      sort: 'createdAt:asc',
      'page-number': params['page-number'] || 2,
      'page-size': params['page-size'] || 10,
      'infraGroup-code': infraGroupCodesKeys.room,
      includes: 'parent',
    })
    return { success: result.success || false, body: result.body || {} }
  },
  entityName: 'room',
})

const headerPrep: HeaderPrep = {
  title: 'Ruangan',
  icon: 'i-lucide-layout-list',
  refSearchNav: {
    placeholder: 'Cari (min. 3 karakter)...',
    minLength: 3,
    debounceMs: 500,
    showValidationFeedback: true,
    onInput: (val: string) => {
      searchInput.value = val
    },
    onClick: () => {},
    onClear: () => {},
  },
  addNav: {
    label: 'Tambah',
    icon: 'i-lucide-plus',
    onClick: () => {
      recItem.value = null
      recId.value = 0
      isFormEntryDialogOpen.value = true
      isReadonly.value = false
    },
  },
}

provide('rec_id', recId)
provide('rec_action', recAction)
provide('rec_item', recItem)
provide('table_data_loader', isLoading)

const getCurrentDetail = async (id: number | string) => {
  const result = await getDetail(id)
  if (result.success) {
    const currentValue = result.body?.data || {}
    if (currentValue.item_id) {
      const itemResult = await getItemDetail(currentValue.item_id)
      if (itemResult.success) {
        currentValue.item = itemResult.body?.data || {}
      }
    }
    if (currentValue.rooms) {
      const rooms: any = Array.isArray(currentValue.rooms) && currentValue.rooms.length > 0 ? currentValue.rooms[0] : {}
      specialistsFiltered.value = rooms?.specialist
        ? [
            {
              value: rooms.specialist?.id ? Number(rooms.specialist.id) : '',
              label: rooms.specialist?.name || '',
            },
          ]
        : []
      subspecialistsFiltered.value = rooms?.subspecialist
        ? [
            {
              value: rooms.subspecialist?.id ? Number(rooms.subspecialist.id) : '',
              label: rooms.subspecialist?.name || '',
            },
          ]
        : []
      currentValue.unit_id = rooms?.unit_id || null
      currentValue.specialist_id = rooms?.specialist_id || null
      currentValue.subspecialist_id = rooms?.subspecialist_id || null
    }
    recItem.value = currentValue
    isFormEntryDialogOpen.value = true
  }
}

watch([recId, recAction], () => {
  switch (recAction.value) {
    case ActionEvents.showDetail:
      getCurrentDetail(recId.value)
      title.value = 'Detail Ruangan'
      isReadonly.value = true
      break
    case ActionEvents.showEdit:
      getCurrentDetail(recId.value)
      title.value = 'Edit Ruangan'
      isReadonly.value = false
      break
    case ActionEvents.showConfirmDelete:
      isRecordConfirmationOpen.value = true
      break
  }
})

watch(selectedUnit, async (value: string | number | null) => {
  specialistsFiltered.value = []
  if (value) {
    selectedSpecialist.value = null
    specialistsFiltered.value = specialists.value.filter((item: any) => Number(item.parent) === Number(value))
    subspecialistsFiltered.value = []
  } else {
    selectedSpecialist.value = null
    specialistsFiltered.value = []
    subspecialistsFiltered.value = []
  }
})

watch(selectedSpecialist, async (value: string | number | null) => {
  subspecialistsFiltered.value = []
  if (value) {
    subspecialistsFiltered.value = subspecialists.value.filter((item: any) => Number(item.parent) === Number(value))
  } else {
    subspecialistsFiltered.value = []
  }
})

onMounted(async () => {
  parents.value = await getValueLabelList({ sort: 'createdAt:asc', 'infraGroup-code': infraGroupCodesKeys.floor })
  specialists.value = await getSpecialistList({ sort: 'createdAt:asc', 'page-size': 100 })
  subspecialists.value = await getSubspecialistList({ sort: 'createdAt:asc', 'page-size': 100 })
  units.value = await getUnitList({ sort: 'createdAt:asc', 'page-size': 100 })
  await getItemList()
})
</script>

<template>
  <Header
    v-model="searchInput"
    :prep="headerPrep"
    :ref-search-nav="headerPrep.refSearchNav"
    @search="handleSearch"
  />

  <AppRoomList
    :data="data"
    :pagination-meta="paginationMeta"
    @page-change="handlePageChange"
  />

  <Dialog
    v-model:open="isFormEntryDialogOpen"
    :title="!!recItem ? title : 'Tambah Ruangan'"
    size="lg"
    prevent-outside
    @update:open="
      (value: any) => {
        onResetState()
        isFormEntryDialogOpen = value
      }
    "
  >
    <AppRoomEntryForm
      :schema="InfraSchema"
      :specialists="specialistsFiltered"
      :subspecialists="subspecialistsFiltered"
      :units="units"
      :parents="parents"
      :values="recItem"
      :is-loading="isProcessing"
      :is-readonly="isReadonly"
      @update:selected-unit="(val: any) => (selectedUnit = val)"
      @update:selected-specialist="(val: any) => (selectedSpecialist = val)"
      @submit="
        (values: InfraFormData | Record<string, any>, resetForm: () => void) => {
          if (recId > 0) {
            handleActionEdit(recId, values, getItemList, resetForm, toast)
            return
          }
          handleActionSave(values, getItemList, resetForm, toast)
        }
      "
      @cancel="handleCancelForm"
    />
  </Dialog>

  <RecordConfirmation
    v-model:open="isRecordConfirmationOpen"
    action="delete"
    :record="recItem"
    @confirm="() => handleActionRemove(recId, getItemList, toast)"
    @cancel=""
  >
    <template #default="{ record }">
      <div class="text-sm">
        <p>
          <strong>ID:</strong>
          {{ record?.id }}
        </p>
        <p v-if="record?.name">
          <strong>Nama:</strong>
          {{ record.name }}
        </p>
        <p v-if="record?.code">
          <strong>Kode:</strong>
          {{ record.code }}
        </p>
      </div>
    </template>
  </RecordConfirmation>
</template>
