<template>
  <div class="header-search" :class="{ show: isShow }">
    <div style="display: inline-block" @click.stop="onShowClick">
      <!-- 子组件无法绑定click，只能emit -->
      <svg-icon class-name="search-icon" icon="search" />
    </div>
    <el-select
      ref="handleSearchSelectRef"
      class="header-search-select"
      v-model="search"
      filterable
      default-first-option
      remote
      :remote-method="querySearch"
      @change="onSelectChange"
    >
      <el-option
        v-for="option in searchOptions"
        :key="option.item.path"
        :label="option.item.title.join(' > ')"
        :value="option.item"
      ></el-option>
    </el-select>
  </div>
</template>

<script setup>
import { computed, ref, watch } from 'vue'
import { filterRoutes } from '@/utils/route'
import { useRouter } from 'vue-router'
import Fuse from 'fuse.js'
import { generateRoutes } from './FuseData'
import { watchSwitchLang } from '@/utils/i18n'

//数据源
const router = useRouter()
let searchPool = computed(() => {
  const fRoutes = filterRoutes(router.getRoutes())
  return generateRoutes(fRoutes)
})
//控制search展示

//搜索库相关
let fuse
const initFuse = (searchPool) => {
  console.log('searchPool.value', searchPool)

  fuse = new Fuse(searchPool, {
    //是否按优先级排序
    shouldSort: true,
    //匹配长度超过这个值才会被认为是匹配
    minMatchCharLength: 1,
    //被搜索键列表 name:搜索的键 weight:权重
    keys: [
      { name: 'title', weight: 0.7 },
      { name: 'path', weight: 0.3 }
    ]
  })
}
initFuse(searchPool.value)

//监听search打开，处理close事件
const isShow = ref(false)
watch(isShow, (val) => {
  if (val) {
    handleSearchSelectRef.value.focus()
    document.body.addEventListener('click', onClose)
  } else {
    document.body.removeEventListener('click', onClose)
  }
})

//关闭search处理事件
const handleSearchSelectRef = ref(null)
const onClose = () => {
  handleSearchSelectRef.value.blur()
  isShow.value = false
  searchOptions.value = []
}

const onShowClick = () => {
  isShow.value = !isShow.value
}

//search相关
const search = ref('')
//搜索方法
const searchOptions = ref([])
//搜索方法
const querySearch = (query) => {
  if (query !== '') {
    console.log('fuse.search(query)', fuse.search(query))
    searchOptions.value = fuse.search(query)
  } else {
    searchOptions.value = []
  }
}

//选中回调
const onSelectChange = (val) => {
  router.push(val.path)
  console.log('onSelectChange')
}

//语言发生变化，fuse重新初始化
watchSwitchLang(() => {
  searchPool = computed(() => {
    const routes = filterRoutes(router.getRoutes)
    return generateRoutes(routes)
  })
  initFuse(searchPool)
})
</script>

<style lang="scss" scoped>
.header-search {
  .search-icon {
    cursor: pointer;
    font-size: 18px;
    vertical-align: middle;
  }
  .header-search-select {
    font-size: 18px;
    transition: width 0.2s;
    width: 0;
    overflow: hidden;
    background: transparent;
    border-radius: 0;
    display: inline-block;
    vertical-align: middle;
    :deep(.el-input__inner) {
      border-radius: 0;
      border: 0;
      padding-left: 0;
      padding-right: 0;
      box-shadow: none !important;
      border-bottom: 1px solid #d9d9d9;
      vertical-align: middle;
    }
  }
  &.show {
    .header-search-select {
      width: 210px;
      margin-left: 10px;
    }
  }
}
</style>
