<template>
  <div class="d-flex">
    <div class="label-title">行业类型</div>
    <div class="label-content">
      <span 
        v-for="k in items" 
        :key="k.id"
        :class="['label-color', {'actives': k.active}]"
        @click="handleItemClick(k)"
      >{{ k.nameCn }}</span>
    </div>
  </div>
</template>

<script setup>
defineOptions({ name: 'search-industry-type'})
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { getIndustryTreeData } from '@/api/common/index'

const emits = defineEmits(['handleClick', 'clear'])
const props = defineProps({
  isClear: {
    type: Boolean,
    default: false
  }
})
watch(
  () => props.isClear, 
  (newVal) => {
    if (!newVal) return
    // 清空筛选条件时默认选中不限
    items.value.map(e => {
      e.active = false
      if (e.id === -1) e.active = true
      return e
    })
    emits('clear')
  }
)

const route = useRoute()
const routeQuery = route.query

const items = ref([])
const getList = async () => {
  const data = await getIndustryTreeData({ level: 1 })
  const list = data.map(e => {
    e.active = false
    return e
  })
  items.value = [{ id: -1, nameCn: '不限', active: true }, ...list]
  // 刷新回显
  if (routeQuery.industryIds) {
    const obj = items.value.find(e => e.id === routeQuery.industryIds)
    items.value.map(e => e.active = false)
    if (obj) obj.active = true
  }
}
getList()


const handleItemClick = (k) => {
  items.value.map(e => e.active = false)
  k.active = true
  emits('handleClick', [k.id], 'industryIds')
}
</script>

<style scoped lang="scss">
@import '@/styles/recruit/company.scss';
</style>
