<template>
  <a-select
      :getPopupContainer="
                          (triggerNode) => {
                            return triggerNode.parentNode || document.body;
                          }
                        "
      v-model:value="managerArray"
      :placeholder="placeholder"
      :filterOption="false"
      style="width: 100%"
      :options="isOpen?options:selectOptions"
      allowClear
      showSearch
      :mode="mode"
      :disabled="disabled"
      @popupScroll="memberScroll"
      @search="searchMember"
      :open="isOpen"
      @change="change"
      @blur="blur"
  >
    <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 {scrollLoad, throttle} from "@/api/method";
import * as api1 from "@/api/index1";

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

const options = ref([])

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: 10})
const loading = ref(false)


watch(() => memberParam.value, throttle(getMemberData, 1000))
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()
})


function getMemberData() {
  console.log(memberParam)
  if (!memberParam.value.keyWord) {
    return
  }
  if (memberParam.value.pageNo !== 1) {
    options.value && options.value.length && (options.value = [])
  }
  isOpen.value = true
  loading.value = true
  api1.getMemberInfo(memberParam.value).then((res) => {
    if (!res.data.data.rows || !res.data.data.rows.length) {
      isOpen.value = false
      return;
    }
    const list = res.data.data.rows.filter(e => !(props.value + '').includes(e.id)).map(e => ({
      label: e.realName,
      value: e.id,
      deptId: e.departId
    }));
    memberParam.value.pageNo === 1 && props.value ? (options.value = list) : options.value.push(...list)
    loading.value = false
  });
}

const memberScroll = (e) => {
  let num = scrollLoad(e);
  if (num === 2) {
    memberParam.value.pageNo++;
  }
};

//搜索学员
const searchMember = (keyWord) => {
  console.log('searchMember', keyWord)
  loading.value = true
  options.value = []
  isOpen.value = true
  keyWord && (memberParam.value = {keyWord, pageNo: 1, pageSize: 10});
};

function blur() {
  isOpen.value = false
}

function change(e, l) {
  isOpen.value = false
  Array.isArray(l) && (selectOptions.value = l)
  Array.isArray(selectOptions.value) && emit('onChange', e, l, selectOptions.value.find(e => e.deptId)?.deptId)
  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>
