<template>
  <div class="select-plat__wrapper" style="width:100%">
    <el-select
      v-model="selectValue"
      style="width:100%"
      filterable
      remote
      reserve-keyword
      placeholder="请选择平台"
      :remote-method="remoteMethod"
      :loading="loading"
      @change="updateCacheValue"
      @visible-change="handleInit"
    >
      <el-option
        v-for="item in options"
        :key="item.id"
        :style="{color:`${proxyValue.includes(item.id) ? '#6954f0' : '#606266'}`}"
        :label="item.platform_name"
        :value="item.id"
      >
        <p @click="toggle(item)">{{ item.platform_name }}</p>
      </el-option>
    </el-select>

    <div class="tags" style="display:flex;gap:5px;margin-top:10px">
      <el-tag v-for="item in cacheValues" :key="item.id" size="mini" closable @close="handleClose(item)">{{ item.platform_name }}</el-tag>
    </div>
  </div>
</template>

<script>
import asyncRequest from '@/apis/service/interest/account'
export default {
  props: ['value', 'plat_options', 'allowInit'],
  data() {
    return {
      selectValue: '',
      cacheValues: [],
      options: [],
      initOptions: [],
      list: [],
      loading: false,
      states: []
    }
  },
  computed: {
    proxyValue: {
      set(newVal) {
        this.$emit('update:value', newVal)
      },
      get() {
        return this.value
      }
    }
  },
  watch: {
    plat_options: {
      handler() {
        this.cacheValues = this.plat_options || []
      },
      deep: true,
      immediate: true
    },
    allowInit: {
      handler(isAllow) { isAllow && this.remoteMethod() },
      deep: true,
      immediate: true
    }
  },
  async mounted() {
    const list = await this.remoteMethod()
    if (list) this.initOptions = list
  },
  methods: {
    handleInit(visible) {
      if (visible && this.initOptions) {
        this.options = [...this.initOptions]
      }
    },
    handleClose(tag) {
      const { id } = tag
      const _cacheIndex = this.cacheValues.findIndex(({ id: _id }) => _id === id)
      const _sourceIndex = this.proxyValue.findIndex(v => v === id)
      this.cacheValues.splice(_cacheIndex, 1)
      this.proxyValue.splice(_sourceIndex, 1)
    },
    updateCacheValue() {
      this.selectValue = ''
    },
    toggle(item) {
      const { id } = item
      const _cacheIndex = this.cacheValues.findIndex(({ id: _id }) => _id === id)
      const _sourceIndex = this.proxyValue.findIndex(v => v === id)

      if (_cacheIndex >= 0 && _sourceIndex >= 0) {
        if (_cacheIndex >= 0) this.cacheValues.splice(_cacheIndex, 1)
        if (_sourceIndex >= 0) this.proxyValue.splice(_sourceIndex, 1)
        return
      }

      if (_cacheIndex < 0 && _sourceIndex < 0) {
        this.cacheValues.push(item)
        this.proxyValue.push(item.id)
      }
    },
    async remoteMethod(platform_name) {
      this.selectLoading = true
      this.options = []
      const { code, data, message } = await asyncRequest.platlist({ platform_name, page: 1, size: 1000, noRelation: true })
      if (code === 0) {
        const { list } = data
        console.log(list)
        this.options = list
        return list
      } else if (code >= 100 && code <= 104) {
        await this.logout()
      } else {
        this.$message.warning(message)
      }
      this.selectLoading = false
    }
  }
}
</script>

<style lang="scss" scoped>
.select-plat__wrapper{
  ::v-deep(.el-tag){
    display: none;
  }

  .tags{
    gap:5px;
    display:flex;
    margin-top:10px;
    flex-wrap: wrap;

    ::v-deep(.el-tag){
      display: flex !important;
      align-items: center;
    }
  }
}
</style>
