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

const dialogueStore = useDialogueStore()
const userStore = useUserStore()

const state = reactive({
  apiParams: '',
  searchText: '',
  uid: computed(() => userStore.uid), //当前用户id
  hideFirstRecord: false, //是否隐藏前缀及搜索群/用户主体信息
})

onLoad((options) => {
  console.log(options)
  let talk_type = 0
  if (options.talk_type) {
    talk_type = Number(options.talk_type)
  }
  let receiver_id = 0
  if (options.receiver_id) {
    receiver_id = Number(options.receiver_id)
  }
  state.apiParams = encodeURIComponent(
    JSON.stringify({
      talk_type: talk_type, //1私聊2群聊
      receiver_id: receiver_id, //查详情的时候需传入
      last_group_id: 0, //最后一条群id
      last_member_id: 0, //最后一条用户id
    }),
  )
  if (options.searchText) {
    state.searchText = options.searchText
  }

  console.log(JSON.parse(decodeURIComponent(state.apiParams)))

  if (options.hideFirstRecord) {
    state.hideFirstRecord = options.hideFirstRecord === '1' ? true : false
  }
})

//分页查询时，最后一条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(searchResultKey)
  let result = JSON.parse(decodeURIComponent(res))
  console.log(result)
  let receiverInfo = lodash.cloneDeep(result)
  if (receiverInfo.talk_type === 1) {
    //单聊才需此判断
    if (receiverInfo.user_id === state.uid) {
      //发送人是自己，接收人不需要变
    }
    if (receiverInfo.receiver_id === state.uid) {
      //接收人是自己，这里需要变成对方
      let temp_id = receiverInfo.receiver_id
      let temp_name = receiverInfo.receiver_name
      let temp_avatar = receiverInfo.receiver_avatar
      receiverInfo.receiver_id = receiverInfo.user_id
      receiverInfo.receiver_name = receiverInfo.user_name
      receiverInfo.receiver_avatar = receiverInfo.user_avatar
      receiverInfo.user_id = temp_id
      receiverInfo.user_name = temp_name
      receiverInfo.user_avatar = temp_avatar
    }
  }
  dialogueStore.setDialogue({
    name: receiverInfo.receiver_name,
    talk_type: talk_type,
    receiver_id: receiverInfo.receiver_id,
  })
  if (searchResultKey === 'talk_record_infos_receiver') {
    uni.navigateTo({
      url: '/pages/dialog/index',
    })
  } else {
    uni.navigateTo({
      url: '/pages/dialog/index?msgInfo=' + res + '&keepDialogInfo=1',
    })
  }
}
</script>
<style scoped lang="scss"></style>
