<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'

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) => {
  if (state.searchResultKey === 'general_infos') {
    uni.navigateTo({
      url: '/pages/search/moreResult/moreResultDetail?searchText=' + searchText,
    })
  }
}
</script>
<style scoped lang="scss"></style>
