<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;' : ''
        "
      >
        <div class="search-result-list" v-if="state.searchText">
          <div
            class="search-result-each-part"
            v-for="(searchResultValue,
            searchResultKey,
            searchResultIndex) in state.searchResult"
            :key="searchResultKey"
          >
            <div class="result-title text-[28rpx] font-regular">
              {{ getResultKeysValue(searchResultKey) }}
            </div>
            <div class="result-list">
              <searchItem
                v-for="(item, index) in state?.searchResult[searchResultKey]"
                :key="index"
                :searchResultKey="searchResultKey"
                :searchItem="item"
                :searchText="state.searchText"
              ></searchItem>
            </div>
            <div class="result-has-more"></div>
          </div>
        </div>
        <div class="search-no-result" v-if="!state.searchText">
          <img
            src="/src/static//image/search/search-no-data.png"
            mode="widthFix"
          />
          <span class="text-[28rpx] font-regular">{{ $t('search.hint') }}</span>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup>
import searchItem from './components/searchItem.vue'
import { ServeSeachQueryAll } from '@/api/search/index'
import { ref, watch, computed, onMounted, onUnmounted, reactive } from 'vue'
import { useAuth } from '@/store/auth'
import { useI18n } from 'vue-i18n'
const { t } = useI18n()
const state = reactive({
  searchText: '', //搜索内容
  searchResult: null, //搜索结果
  searchResultPageSize: 3, //搜索结果每页数据量
})

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

//获取key对应值
const getResultKeysValue = (keys) => {
  let resultKey = ''
  switch (keys) {
    case 'user_infos':
      resultKey = t('index.mine.addressBook')
      break
    case 'group_infos':
      resultKey = t('chat.type.group')
      break
    case 'group_member_infos':
      resultKey = t('chat.type.group')
      break
    case 'general_infos':
      resultKey = t('chat.type.record')
      break
    default:
      resultKey = ''
  }
  return resultKey
}

// 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) {
      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;

    .search-result-list {
      width: 100%;
      padding: 10rpx 18rpx;

      .search-result-each-part {
        margin: 46rpx 0 0;

        .result-title {
          line-height: 40rpx;
          padding: 0 0 10rpx;
          color: $theme-hint-text;
        }
      }
    }

    .search-no-result {
      width: 476rpx;
      height: 261rpx;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: flex-start;

      img {
        width: 100%;
        height: 100%;
      }

      span {
        color: $theme-hint-text;
        margin: -20rpx 0 0;
        line-height: 40rpx;
      }
    }
  }
}
</style>
