<script setup >
import {computed, ref} from 'vue'
import { NInput, NPopconfirm, NScrollbar } from 'naive-ui'
import {getSessionList, sessionDetail} from '@/api/api'
import { SvgIcon } from '@/components/common'
import { useAppStore, useChatStore } from '@/store'
import { useBasicLayout } from '@/hooks/useBasicLayout'
import { debounce } from '@/utils/functions/debounce'
const { isMobile } = useBasicLayout()
const appStore = useAppStore()
const chatStore = useChatStore()
const dataSources = computed(() => chatStore.history)
async function handleSelect({ listUuid }) {
	currentListUuid.value=listUuid
	getSessionDetail()
  if (isActive(listUuid)){
		return
	}

  if (chatStore.active)
    chatStore.updateHistory(chatStore.active, { isEdit: false })
  await chatStore.setActive(listUuid)

  if (isMobile.value)
    appStore.setSiderCollapsed(true)
}
const dataList=ref([])
const currentListUuid=ref('')
const detailData=ref([])
const getSessionDetail =async () => {
const res= await 	sessionDetail({listUuid:currentListUuid.value})
if (res.code===0){
	detailData.value=res.data
}
}
const getDataList = async () => {
  const data = {
    page: 1,
    pageSize: 9999,
  }
const res=await getSessionList(data).then((res)=>{
	if (res.code===0){
		dataList.value=res.data.data
		console.log(dataList.value,'dataList.value')
	}
})

}
getDataList()
function handleEdit({ listUuid }, isEdit, event) {
  event?.stopPropagation()
  chatStore.updateHistory(listUuid, { isEdit })
}

function handleDelete(index, event) {
  event?.stopPropagation()
  chatStore.deleteHistory(index)
  if (isMobile.value)
    appStore.setSiderCollapsed(true)
}

const handleDeleteDebounce = debounce(handleDelete, 600)

function handleEnter({ listUuid }, isEdit, event) {
  event?.stopPropagation()
  if (event.key === 'Enter')
    chatStore.updateHistory(listUuid, { isEdit })
}

function isActive(listUuid) {
  return chatStore.active === listUuid
}
</script>

<template>
  <NScrollbar class="px-4">
    <div class="flex flex-col gap-2 text-sm">
      <template v-if="!dataList.length">
        <div class="flex flex-col items-center mt-4 text-center text-neutral-300">
          <SvgIcon icon="ri:inbox-line" class="mb-2 text-3xl" />
          <span>{{ $t('common.noData') }}</span>
        </div>
      </template>
      <template v-else>
        <div v-for="(item, index) of dataList" :key="item.listUuid">
          <a
            class="relative flex items-center gap-3 px-3 py-3 break-all border rounded-md cursor-pointer hover:bg-neutral-100 group dark:border-neutral-800 dark:hover:bg-[#24272e]"
            :class="isActive(item.listUuid) && ['border-[#4b9e5f]', 'bg-neutral-100', 'text-[#4b9e5f]', 'dark:bg-[#24272e]', 'dark:border-[#4b9e5f]', 'pr-14']"
            @click="handleSelect(item)"
          >
            <span>
              <SvgIcon icon="ri:message-3-line" />
            </span>
            <div class="relative flex-1 overflow-hidden break-all text-ellipsis whitespace-nowrap">
              <NInput
                v-if="item.isEdit"
                v-model:value="item.title" size="tiny"
                @keypress="handleEnter(item, false, $event)"
              />
              <span v-else>{{ item.title }}</span>
            </div>
            <div v-if="isActive(item.listUuid)" class="absolute z-10 flex visible right-1">
              <template v-if="item.isEdit">
                <button class="p-1" @click="handleEdit(item, false, $event)">
                  <SvgIcon icon="ri:save-line" />
                </button>
              </template>
              <template v-else>
                <button class="p-1">
                  <SvgIcon icon="ri:edit-line" @click="handleEdit(item, true, $event)" />
                </button>
                <NPopconfirm placement="bottom" @positive-click="handleDeleteDebounce(index, $event)">
                  <template #trigger>
                    <button class="p-1">
                      <SvgIcon icon="ri:delete-bin-line" />
                    </button>
                  </template>
                  {{ $t('chat.deleteHistoryConfirm') }}
                </NPopconfirm>
              </template>
            </div>
          </a>
        </div>
      </template>
    </div>
  </NScrollbar>
</template>