<template>
  <el-select 
    v-model="selectedValue" 
    placeholder="请选择" 
    clearable 
    filterable
    :multiple="multiple"
    :loading="loading"
    @change="handleChange"
  >
    <div class="select-footer">
      <div style="text-align: center; color: #8492a6; font-size: 13px; line-height: 28px; ">
        共{{ total }}条数据
      </div>
      <el-button v-if="multiple && !isEmpty(options)" style="margin-left: 10px;" size="mini" type="text" @click.stop="handleSelectAll">
        {{ isAllSelected ? '取消全选' : '全选' }}
      </el-button>
    </div>
    <el-option 
      v-for="item in options" 
      :key="item.userId" 
      :value="item.userId"
      :label="item.nickName"
    >
      <avatar :size="24" :src="item.avatar" :name="item.nickName" :id="item.userId" />
      <span class="user-name">{{ item.nickName }}</span>
    </el-option>
    <el-option v-if="isEmpty(value) && isEmpty(options)" style="display:none" disabled :value="null"></el-option>
  </el-select>
</template>

<script>
import { listUser } from '@/api/system/user';
import Avatar from '@/components/Avatar';
import {isEmpty} from '@/utils'

export default {
  name: 'UserRemoteSelect',
  components: {
    Avatar
  },
  props: {
    // 用户id
    value: {
      type: [String, Array],
      default: null
    },
    // 自定义查询参数
    query: {
      type: Object,
      default: () => ({})
    },
    // 是否多选
    multiple: {
      type: Boolean,
      default: false
    },
  },
  data() {
    return {
      options: [],
      total: 0,
      loading: false,
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        keyword: null
      }
    }
  },
  computed: {
    selectedValue: {
      get() {
        return this.value
      },
      set(value) {
        this.$emit('input', value)
      }
    },
    // 是否全选
    isAllSelected() {
      return this.multiple && this.options.length > 0 && Array.isArray(this.value) && this.value.length === this.options.length;
    }
  },
  watch: {
    query: {
      handler(nv) {
        this.queryParams = {
          ...this.queryParams,
          ...nv
        }
        this.getOptions();
      },
      immediate: true
    }
  },
  created() {
    this.getOptions();
  },
  methods: {
    isEmpty,
    // 全选
    handleSelectAll() {
      if (this.isAllSelected) {
        this.handleChange([]);
      } else {
        const allUserIds = this.options.map(item => item.userId);
        this.handleChange(allUserIds);
      }
    },
    handleChange(value) {
      let list = this.options.filter(item => value.includes(item.userId));
      this.$emit('change', list);
    },
    getOptions() {
      this.loading = true;
      this.queryParams = {
        ...this.queryParams,
        ...this.query
      }
      listUser(this.queryParams).then(res => {
        this.options = res.rows;
        this.total = res.total;
      }).finally(() => {
        this.loading = false;
      });
    }
  }
}
</script>

<style lang="scss" scoped>
.select-footer {
  padding: 5px 12px;
  border-bottom: 1px solid #EBEEF5;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
.user-name {
  padding-left: 6px;
}
</style>