<template>
  <div class="outer-layer search-page">
    <div class="root">
      <searchList
        :searchResultPageSize="10"
        :listLimit="false"
        :apiRequest="state.apiRequest"
        :apiParams="state.apiParams"
        :searchText="state.searchText"
        :isPagination="true"
        @lastIdChange="lastIdChange"
        @clickSearchItem="clickSearchItem"
      ></searchList>
    </div>
  </div>
</template>
<script setup>
import searchList from '../components/searchList.vue'
import { onLoad } from '@dcloudio/uni-app'
import {
  ServeQueryUser,
  ServeQueryGroup,
  ServeTalkRecord,
} from '@/api/search/index'
import { reactive } from 'vue'

import { useDialogueStore } from '@/store'

const dialogueStore = useDialogueStore()

const state = reactive({
  apiRequest: Function,
  apiParams: String,
  searchText: String,
  searchResultKey: String,
})

onLoad((options) => {
  console.log(options)
  if (options.searchResultKey) {
    state.searchResultKey = options.searchResultKey
    if (options.searchResultKey === 'user_infos') {
      state.apiParams = encodeURIComponent(
        JSON.stringify({
          last_id: 0, //最后一条用户id
        }),
      )
      state.apiRequest = ServeQueryUser
    } else if (options.searchResultKey === 'combinedGroup') {
      state.apiParams = encodeURIComponent(
        JSON.stringify({
          last_group_id: 0, //最后一条群id
          last_member_id: 0, //最后一条用户id
        }),
      )
      state.apiRequest = ServeQueryGroup
    } else if (options.searchResultKey === 'general_infos') {
      state.apiParams = encodeURIComponent(
        JSON.stringify({
          talk_type: 0, //1私聊2群聊
          receiver_id: 0, //查详情的时候需传入
          last_group_id: 0, //最后一条群id
          last_member_id: 0, //最后一条用户id
        }),
      )
      state.apiRequest = ServeTalkRecord
    }
  }
  if (options.searchText) {
    state.searchText = options.searchText
  }
})

//分页查询时，最后一条id变化
const lastIdChange = (last_id, last_group_id, last_member_id) => {
  let idChanges = {
    last_id,
    last_group_id,
    last_member_id,
  }
  state.apiParams = encodeURIComponent(
    JSON.stringify(
      Object.assign(
        {},
        JSON.parse(decodeURIComponent(state.apiParams)),
        idChanges,
      ),
    ),
  )
}

//点击了搜索结果项
const clickSearchItem = (
  searchText,
  searchResultKey,
  talk_type,
  receiver_id,
  res,
) => {
  console.log(state.searchResultKey)
  const result = JSON.parse(decodeURIComponent(res))
  console.log(result)
  if (state.searchResultKey === 'user_infos') {
  } else if (state.searchResultKey === 'combinedGroup') {
    dialogueStore.setDialogue({
      name: result.name || result.group_name,
      talk_type: result.type || 2,
      receiver_id: result.id || result.group_id,
    })
    uni.navigateTo({
      url: '/pages/dialog/index',
    })
  } else if (state.searchResultKey === 'general_infos') {
    uni.navigateTo({
      url: '/pages/search/moreResult/moreResultDetail?searchText=' + searchText,
    })
  }
}
</script>
<style scoped lang="scss"></style>
