<template>
  <commonStyle v-if="show" :btnTitle="title" :closeOnContentClick="props.isSingle">
    <v-list>
      <v-list-item
        color="primary"
        :active="selectedItems.includes(item.value)"
        v-for="item in items" :key="item.id" :value="item.value"
        @click="handle(item.value)"
      >
        <template v-if="selectedItems.includes(item.value)" v-slot:append>
          <v-icon icon="mdi-check"></v-icon>
        </template>
        <v-list-item-title>{{ item.label }}</v-list-item-title>
      </v-list-item>
    </v-list>
  </commonStyle>
</template>
<script setup>
import commonStyle from './commonStyle.vue'
import { getDict } from '@/hooks/web/useDictionaries'
import { ref, inject } from 'vue';
defineOptions({name: 'conditionFilter-educationType'})
const emits = defineEmits(['inputChange'])
const query = inject('routeQuery')

const props = defineProps({
  isSingle: {
    type: Boolean,
    default: false
  },
  positionIndexPage: { // 职位检索
    type: Boolean,
    default: false
  }
})

const title = ref('学历要求')
const show = ref(false)
let items = ref()
const selectedItems = ref([])

getDict('menduner_education_type').then(({ data }) => {
  data = data?.length && data || []
  items.value = data
  if (query && query.eduType) {
    const str = query.eduType.split(',')[0]
    const arr = str.split('_')
    if (arr?.length) {
      selectedItems.value = arr
      title.value = selectedItems.value.length ? `学历要求(${selectedItems.value.length})` : '学历要求'
      if (!props.positionIndexPage) emits('inputChange', { values: selectedItems.value, isEmit: true })
    }
  }
  show.value = true
})
const handle = (val) => {
  const obj = selectedItems.value.find(e => e === val)
  if (props.isSingle) {
    // 单选
    selectedItems.value = obj ? [] : [val]
  } else {
    // 多选 
    if (obj) {
      selectedItems.value = selectedItems.value.filter(i => i !== val)
    } else selectedItems.value.push(val)
  }
  title.value = selectedItems.value.length ? `学历要求(${selectedItems.value.length})` : '学历要求'
  if (props.positionIndexPage) emits('inputChange', selectedItems.value)
  else emits('inputChange', { values: selectedItems.value, isEmit: true })
}
</script>
<style lang="scss" scoped>
</style>
