<template>
  <a-select
      :getPopupContainer="
                          (triggerNode) => {
                            return triggerNode.parentNode || document.body;
                          }
                        "
      v-model:value="managerArray"
      :placeholder="placeholder"
      :filterOption="false"
      :options="isOpen?options:selectOptions"
      allowClear
      showSearch
      :mode="mode"
      :disabled="disabled"
      @popupScroll="memberScroll"
      @search="searchMember"
      :open="isOpen"
      @change="change"
      @blur="blur"
      :show-arrow="false"
      style="width: 60%"
  >
    <template v-if="loading" #notFoundContent>
      <a-spin size="small"/>
    </template>
  </a-select>
</template>
<script setup>
import {computed, defineEmits, defineProps, onMounted, ref, watch} from "vue";
import {useThrottlePage} from "@/api/request";
import {USER_LIST} from "@/api/apis";

const props = defineProps({
  value: {
    type: String,
    default: ''
  },
  name: {
    type: String,
    default: ''
  },
  disabled: Boolean,
  placeholder: {
    type: String,
    default: "请输入搜索关键字",
  },
  mode: String
})

const selectOptions = ref([])

const managerArray = computed(() => props.mode === 'select' ? props.value : (props.value ? props.value.split(',') : []))

const emit = defineEmits({})

const isOpen = ref(false)

const memberParam = ref({keyword: '', pageNo:1, pageSize: 20})

const {data: userList, loading} = useThrottlePage(USER_LIST, memberParam.value, false)

const options = computed(() => userList.value.filter(e => !(props.value + '').includes(e.id)).map(e => ({
  label: e.realName + e.userNo,
  value: e.id // ,
  // ...e,
  // audienceList: null
})))

watch(props, init)

function init() {
  //第一次进来 编辑赋值
  if (props.value && (props.value + '') !== selectOptions.value.map(e => e.value).join(',')) {
    selectOptions.value = (props.value + '').split(',').map((e, i) => ({label: props.name.split(',')[i], value: e}))
  }
}

onMounted(() => {
  console.log('onMounted')
  init()
})


const memberScroll = ({target: {scrollHeight, scrollTop, clientHeight}}) => {
  scrollHeight === (clientHeight + scrollTop) && memberParam.value.pageNo++
};

//搜索学员
const searchMember = (keyword) => {
  console.log('searchMember', keyword)
  loading.value = true
  isOpen.value = true
  userList.value = []
  memberParam.value.pageNo = 1
  memberParam.value.keyword = keyword
  console.log('searchMember', memberParam.value)
};

function blur() {
  isOpen.value = false
  memberParam.value.keyword = ''
  memberParam.value.pageNo = 1
}

function change(e, l) {
  memberParam.value.keyword = ''
  memberParam.value.pageNo = 1
  isOpen.value = false
  Array.isArray(l) && (selectOptions.value = l)
  Array.isArray(selectOptions.value) && emit('onChange', e, l, selectOptions.value.find(e => e.departId)?.departId, selectOptions.value.find(e => e.departId)?.departName, selectOptions.value.find(e => e.departId)?.orgName)
  if (Array.isArray(l)) {
    emit('update:name', l.map(t => t.label).join(','))
    emit('update:value', l.map(t => t.value).join(','))
  } else {
    emit('update:name', l?.label)
    emit('update:value', l?.value)
  }
}

</script>
