<template>
  <div class="page_item">
    <div class="tag_group">
      <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">
      <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">
      <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 } from 'vue'
import { getSearchList } from '@/api/school'
const searchData = ref({
  provinceCode: '',
  cityCode: '',
  type: '',
  userId: '',
})
interface City {
  cityCode:  string
  cityName:  string
}
const cityNode = ref<City[]>([])
interface Province {
  provinceCode: string;
  provinceName: string;
  children?: City[]; // 如果有子级城市数据的话
}

interface City {
  cityCode: string;
  cityName: string;
}

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

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

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

type SearchDataKey = 'provinceCode' | 'cityCode' | '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 = ''
    }else {
      cityNode.value = []
      searchData.value.cityCode = ''
    }
  }
  emit('FnSearch', searchData);
}
onMounted(() => {
  GetSearchList()
})
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 == '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>
.tag_group{
  padding:8px 0;
  display: flex;
  justify-content: flex-start;
  .tag_group_title {
    width: auto;
    height: auto;
    margin-right: 8px;
    color: #303133;
    font-size: 14px;
    text-align: left;
    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>