<template>
  <div class="outer-layer search-page">
    <div class="root">
      <searchList
        :searchResultPageSize="3"
        :listLimit="true"
        :apiRequest="ServeSeachQueryAll"
        @toMoreResultPage="toMoreResultPage"
        @clickSearchItem="clickSearchItem"
      ></searchList>
    </div>
  </div>
</template>
<script setup>
import searchList from './components/searchList.vue'
import { ServeSeachQueryAll, ServeGetSessionId } from '@/api/search/index'
import { onMounted } from 'vue'
import { handleSetWebviewStyle } from '@/utils/common'

import { useDialogueStore, useTalkStore } from '@/store'
import { ServeCreateTalkList } from '@/api/chat/index.js'
import { formatTalkItem } from '@/utils/talk'

const dialogueStore = useDialogueStore()

onMounted(() => {
  handleSetWebviewStyle()
})

//点击跳转到更多结果页面
const toMoreResultPage = (searchResultKey, searchText) => {
  uni.navigateTo({
    url:
      '/pages/search/moreResult/moreResult?searchResultKey=' +
      searchResultKey +
      '&searchText=' +
      searchText,
  })
}

//点击了搜索结果项
const clickSearchItem = async (
  searchText,
  searchResultKey,
  talk_type,
  receiver_id,
  res,
) => {
  console.log(searchResultKey)
  const result = JSON.parse(decodeURIComponent(res))
  console.log(result)
  console.log(talk_type, receiver_id)
  const sessionId = await getSessionId(talk_type, receiver_id)
  if (searchResultKey === 'user_infos') {
    if (useTalkStore().findTalkIndex(`${talk_type}_${receiver_id}`) === -1) {
      ServeCreateTalkList({
        talk_type,
        receiver_id,
        erp_user_id: result.erp_user_id,
      }).then(async ({ code, data }) => {
        if (code == 200) {
          let item = formatTalkItem(data)
          useTalkStore().addItem(item)
        }
      })
    }
    dialogueStore.setDialogue({
      name: result.nickname,
      talk_type: 1,
      receiver_id: receiver_id,
    })
    uni.navigateTo({
      url: '/pages/dialog/index?sessionId=' + sessionId,
    })
  } else if (searchResultKey === 'combinedGroup') {
    dialogueStore.setDialogue({
      name: result.name || result.group_name,
      talk_type: result.type || 2,
      receiver_id: result.group_id || result.id,
    })
    uni.navigateTo({
      url: '/pages/dialog/index?sessionId=' + sessionId,
    })
  } else if (searchResultKey === 'general_infos') {
    uni.navigateTo({
      url:
        '/pages/search/moreResult/moreResultDetail?searchText=' +
        searchText +
        '&talk_type=' +
        talk_type +
        '&receiver_id=' +
        receiver_id,
    })
  }
}

//获取会话Id
const getSessionId = (talk_type, receiver_id) => {
  return new Promise((resolve, reject) => {
    let params = {
      talkType: talk_type,
      receiverId: receiver_id,
    }
    const resp = ServeGetSessionId(params)
    console.log(resp)
    resp.then(({ code, data }) => {
      console.log(data)
      if (code == 200) {
        resolve(data?.sessionId)
      } else {
      }
    })
    resp.catch(() => {})
  })
}
</script>
<style scoped lang="scss"></style>
