<template>
  <div class="page_item">
    <div class="tag_group" v-if="searchList.provinceNode.length > 1">
      <div class="tag_group_title">省份：</div>
      <div class="tag_group_content">
        <div class="tag_item" v-for="item in searchList.provinceNode" :key="item.provinceCode" @click="selectItem(item.provinceCode, 'provinceCode', item)" :class="item.provinceCode === searchData.provinceCode ? 'active' : ''">
          {{ item.provinceName }}
        </div>
      </div>
    </div>
    <div class="tag_group" v-if="cityNode.length > 0">
      <div class="tag_group_title">城市：</div>
      <div class="tag_group_content">
        <div class="tag_item" v-for="item in cityNode" :key="item.cityCode" @click="selectItem(item.cityCode, 'cityCode', item)" :class="item.cityCode === searchData.cityCode ? 'active' : ''">
          {{ item.cityName }}
        </div>
      </div>
    </div>
    <div class="tag_group" v-if="districtNode.length > 0">
      <div class="tag_group_title">区/县：</div>
      <div class="tag_group_content">
        <div class="tag_item" v-for="item in districtNode" :key="item.districtCode" @click="selectItem(item.districtCode, 'districtCode', item)" :class="item.districtCode === searchData.districtCode ? 'active' : ''">
          {{ item.districtName }}
        </div>
      </div>
    </div>
    <div class="tag_group" v-if="searchList.userInfo.length > 1">
      <div class="tag_group_title">运维：</div>
      <div class="tag_group_content">
        <div class="tag_item" v-for="item in searchList.userInfo" :key="item.userId" @click="selectItem(item.userId, 'userId', item)" :class="item.userId === searchData.userId ? 'active' : ''">
          {{ item.nickname }}
        </div>
      </div>
    </div>
    <div class="tag_group" v-if="searchList.typeNode.length > 1">
      <div class="tag_group_title">状态：</div>
      <div class="tag_group_content">
        <div class="tag_item" v-for="item in searchList.typeNode" :key="item.type" @click="selectItem(item.type, 'type', item)" :class="item.type === searchData.type ? 'active' : ''">
          {{ item.typeName }}
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
export default {
  name: 'SearchMain',
}
</script>
<script lang="ts" setup>
import { onMounted, ref, nextTick } from 'vue'
import { getSearchList } from '@/api/school'
const searchData = ref({
  provinceCode: '',
  cityCode: '',
  districtCode: '',
  type: '',
  userId: '',
})

interface Province {
  provinceCode: string;
  provinceName: string;
  children?: City[]; // 如果有子级城市数据的话
}
interface City {
  cityCode:  string
  cityName:  string
  children?: District[]; // 如果有子级区县数据的话
}
interface District {
  districtCode:  string
  districtName:  string
}
const cityNode = ref<City[]>([])
const districtNode = ref<District[]>([])

interface TypeNode {
  type: string;
  typeName: string;
}

interface UserInfo {
  userId: string;
  nickname: string;
}

interface SearchList {
  examTypes: any[]; 
  provinceNode: Province[];
  cityNode: City[];
  districtNode: District[];
  typeNode: TypeNode[];
  userInfo: UserInfo[];
}
const searchList =  ref<SearchList>({
  examTypes: [],
  provinceNode: [],
  cityNode: [],
  districtNode: [],
  typeNode: [],
  userInfo: []
})
const emit = defineEmits(['FnSearch'])

type SearchDataKey = 'provinceCode' | 'cityCode' | 'districtCode' | 'type' | 'userId'
const selectItem = (value: string, str: SearchDataKey, item: any) => {
  searchData.value[str] = value
  let arr = []
  if(str == 'provinceCode') {
    if(value) {
      arr = [...item.children]
      arr.splice(0, 0, { cityCode: "", cityName: "全部" })
      cityNode.value = arr
      searchData.value.cityCode = ''
      districtNode.value = []
      searchData.value.districtCode = ''
    }else {
      cityNode.value = []
      searchData.value.cityCode = ''
      districtNode.value = []
      searchData.value.districtCode = ''
    }
  }
  if(str == 'cityCode') {
    if(value) {
      arr = [...item.children]
      arr.splice(0, 0, { districtCode: "", districtName: "全部" })
      districtNode.value = arr
      searchData.value.districtCode = ''
    }else {
      districtNode.value = []
      searchData.value.districtCode = ''
    }
  }
  emit('FnSearch', searchData);
}

onMounted(() => {
  GetSearchList()

    // HandleScroll()
    // window.addEventListener('scroll', function() {
    //   console.log('12123123')
    // });
})

const filterRef = ref(null)
const isSticky = ref(false)
let observer = null;
const HandleScroll = () => {
  observer = new IntersectionObserver(
    (entries) => {
      console.log(entries, '>>>>>>entries')
      entries.forEach(entry => {
        if (entry.boundingClientRect.top <= 65) {
          isSticky.value = true;
        } else {
          isSticky.value = false;
        }
      });
    },
    {
      root: null, // 使用视口作为根元素
      threshold: 0, // 当元素顶部接触视口顶部时触发
      rootMargin: `-${65}px 0px 0px 0px` // 相对于视口顶部的偏移
    }
  );
  
  if (filterRef.value) {
    observer.observe(filterRef.value);
  }
}
const GetSearchList = () => {
  getSearchList().then((res: any) => {
    console.log(res)
    if(res.code == 200) {
      const { data } = res
      for(let i in data) {
        if(i == 'provinceNode') {
          data[i].splice(0, 0, { provinceCode: "", provinceName: "全部" })
        }else if(i == 'cityNode') {
          data[i].splice(0, 0, { cityCode: "", cityName: "全部" })
        }else if(i == 'districtNode') {
          data[i].splice(0, 0, { districtCode: "", districtName: "全部" })
        }else if( i == 'typeNode') {
          data[i].splice(0, 0, { type: "", typeName: "全部" })
        }else if( i == 'userInfo') {
          data[i].splice(0, 0, { userId: "", nickname: "全部" })
        }
      }
      searchList.value = data
      console.log(searchList.value)
    }
  })
}
</script>

<style lang="scss" scoped>
.page_item {
  padding: 10px 20px !important;
}
.tag_group{
  padding:8px 0;
  display: flex;
  justify-content: flex-start;
  .tag_group_title {
    // width: auto;
    width: 50px;
    height: auto;
    margin-right: 8px;
    color: #303133;
    font-size: 14px;
    text-align: right;
    line-height: 30px;
    font-weight: 500;
    letter-spacing: 0em;
  }
  .tag_group_content {
    width: calc(100% - 80px);
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    .tag_item {
      padding: 0 8px;
      line-height: 30px;
      border-radius: 4px;
      font-size: 14px;
      background: #fff;
      color: #999999;
      cursor: pointer;
      &.active {
        background: rgba(71,113,203,0.1);
        color: #2E64FA;
      }
      &:hover {
        color: #2E64FA;
      }
    }
  }
}
</style>