<template>
  <a-select
      :getPopupContainer="
                          (triggerNode) => {
                            return triggerNode.parentNode || document.body;
                          }
                        "
      v-model:value="managerArray"
      :placeholder="placeholder"
      :filterOption="true"
      style="width: 100%"
      :options="options"
      allowClear
      showSearch
      :mode="mode"
      :disabled="disabled"
      @popupScroll="memberScroll"
      @search="searchMember"
      @change="change"
  >
    <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";
import {useStore} from "vuex";

const props = defineProps({
  value: {
    type: String,
    default: ''
  },
  name: {
    type: String,
    default: ''
  },
  disabled: Boolean,
  placeholder: String,
  mode: String
})
const store = useStore();

const managerArray = computed(() => {
  return props.mode === 'select' ? props.value : props.value.split(',')
})

const emit = defineEmits({})

const options = ref([])

const memberParam = ref({keyWord: '', pageNo: 1, pageSize: 10})
const loading = ref(false)


watch(() => memberParam.value.keyWord, throttle(getSearchMember, 500))
watch(() => memberParam.value.pageNo, throttle(getPageMember, 500))

onMounted(() => {
  console.log('onMounted')
  if (props.value) {
    options.value = [...(props.value + '').split(',').map((value, i) => ({
      label: (props.name + '').split(',')[i],
      value
    })), ...store.state.memberInitInfo]
  } else options.value = store.state.memberInitInfo
})

function getSearchMember() {
  loading.value = true
  options.value = []
  getMemberData()
}

function getPageMember() {
  loading.value = true
  getMemberData()
}


function getMemberData() {
  api1.getMemberInfo(memberParam.value).then((res) => {
    const list = res.data.data.rows.filter(e => !(props.value + '').includes(e.id)).map(e => ({
      label: e.realName,
      value: e.id
    }));
    if (memberParam.value.pageNo === 1 && props.value) {
      const arrManagerId = (props.value + '').split(',')
      const arrManager = props.name.split(',')
      options.value = [...arrManager.map((e, i) => ({label: e, value: arrManagerId[i]})), ...list]
    } else options.value.push(...list)
    loading.value = false
  });
}

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

//搜索学员
const searchMember = (keyWord) => {
  keyWord && (memberParam.value = {keyWord, pageNo: 1, pageSize: 10});
};

function change(e, l) {
  console.log('change', l)
  if (Array.isArray(l)) {
    emit('update:value', l.map(t => t.value).join(','))
    emit('update:name', l.map(t => t.label).join(','))
  } else {
    emit('update:value', l.value)
    emit('update:name', l.label)
  }
}

</script>
