<template>
  <commonStyle :btnTitle="title" :close-on-content-click="false" v-if="show">
    <v-list>
      <v-list-item
        v-for="item in items" :key="item.id" :value="item.value"
        :active="selectedItems.includes(item.value)"
        color="primary"
        @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-JobType'})
const emits = defineEmits(['inputChange'])
const query = inject('routeQuery')
const props = defineProps({
  positionIndexPage: { // 职位检索
    type: Boolean,
    default: false
  }
})

const title = ref('求职类型')
let show = ref(false)
let items = ref()
const selectedItems = ref([])

getDict('menduner_job_type').then(({ data }) => {
  data = data?.length && data || []
  items.value = data
  // 刷新回显
  if (query && query.jobType) {
    const str = query.jobType.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) => {
  if (selectedItems.value.includes(val)) {
    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>
