<template>
  <div class="outer-layer search-page">
    <div class="root">
      <div class="searchRoot">
        <tm-input
          class="searchRoot_input"
          placeholder="请输入…"
          color="#F9F9FD"
          :round="1"
          prefix="tmicon-search"
          prefixColor="#46299D"
          v-model.lazy="state.searchText"
          @input="inputSearchText"
        ></tm-input>
        <span
          class="searchRoot_cancelBtn text-[32rpx] font-medium"
          @click="cancelSearch"
        >
          {{ $t('cancel') }}
        </span>
      </div>
      <div
        class="search-result"
        :style="
          !state.searchText ? 'align-items:center;justify-content:center;' : ''
        "
      >
        <searchList
          :searchResult="state.searchResult"
          :searchText="state.searchText"
        ></searchList>
      </div>
    </div>
  </div>
</template>
<script setup>
import searchList from './components/searchList.vue'
import { ServeSeachQueryAll } from '@/api/search/index'
import { ref, watch, computed, onMounted, onUnmounted, reactive } from 'vue'
import { useAuth } from '@/store/auth'
import { nextTick } from 'process'
const state = reactive({
  searchText: '', //搜索内容
  searchResult: null, //搜索结果
  searchResultPageSize: 3, //搜索结果每页数据量
})

//输入搜索文本
const inputSearchText = (e) => {
  console.log(e)
  let searchText = e
  queryAllSearch(searchText)
}

// ES搜索聊天记录-主页搜索什么都有
const queryAllSearch = (searchText) => {
  let params = {
    key: searchText, //关键字
    size: state.searchResultPageSize,
  }
  const resp = ServeSeachQueryAll(params)
  resp.then(({ code, data }) => {
    console.log(data)
    if (code == 200) {
      // data.group_infos = [
      //   {
      //     id: 888898,
      //     type: 4,
      //     name: '泰丰国际',
      //     avatar: '', //群头像
      //     created_at: '2024-10-31 14:31:11',
      //     group_num: 730,
      //   },
      // ]
      if ((data.user_infos || []).length > 0) {
        ;(data.user_infos || []).forEach((item) => {
          item.group_type = 0
        })
      }
      if ((data.group_infos || []).length > 0) {
        ;(data.group_infos || []).forEach((item) => {
          item.group_type = item.type
          item.groupTempType = 'group_infos'
        })
      }
      if ((data.group_member_infos || []).length > 0) {
        ;(data.group_member_infos || []).forEach((item) => {
          item.groupTempType = 'group_member_infos'
        })
      }
      let tempGeneral_infos = Array.isArray(data.general_infos)
        ? [...data.general_infos]
        : data.general_infos
      delete data.general_infos
      data.combinedGroup = (data.group_infos || []).concat(
        data.group_member_infos || [],
      )
      data.general_infos = tempGeneral_infos
      state.searchResult = data
    } else {
    }
  })

  resp.catch(() => {})
}

//点击取消搜索
const cancelSearch = () => {
  uni.navigateBack({
    delta: 1,
  })
}
</script>
<style scoped lang="scss">
uni-page-body,
page {
  height: 100%;
}

.outer-layer {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.root {
  flex: 1;
  padding: 20rpx 32rpx;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.search-page {
  .searchRoot {
    padding: 0 16rpx;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: flex-start;

    .searchRoot_input {
      width: 100%;
    }

    .searchRoot_cancelBtn {
      line-height: 44rpx;
      color: $theme-primary;
      margin: 0 0 0 20rpx;
      flex-shrink: 0;
    }
  }

  .search-result {
    width: 100%;
    flex: 1;

    min-height: 0;
    display: flex;
    flex-direction: row;
    align-items: flex-start;
    justify-content: flex-start;
  }
}
</style>
