<template>
  <el-select 
    v-model="selectedValue" 
    placeholder="请选择" 
    filterable
    :multiple="multiple"
    :loading="loading"
    @change="handleChange"
    @visible-change="handleVisibleChange"
    remote
    :remote-method="remoteMethod"
  >
    <div class="select-footer">
      <div style="text-align: center; color: #8492a6; font-size: 13px; ">
        共{{ 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.id" 
      :value="item.id"
      :label="item.name"
    />
    <el-option v-if="isEmpty(value) && isEmpty(options)" style="display:none" disabled :value="null"></el-option>
  </el-select>
</template>

<script>
import { listModel } from '@/api/bst/model';
import Avatar from '@/components/Avatar';
import {isEmpty} from '@/utils'

export default {
  name: 'AreaRemoteSelect',
  components: {
    Avatar
  },
  props: {
    // 区域id
    value: {
      type: [String, Array],
      default: null
    },
    // 自定义查询参数
    query: {
      type: Object,
      default: () => ({})
    },
    // 是否多选
    multiple: {
      type: Boolean,
      default: false
    },
    // 初始化选项
    initOptions: {
      type: Array,
      default: () => []
    }
  },
  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;
    }
  },
  created() {
    if (!isEmpty(this.initOptions)) {
      this.options = this.initOptions;
    }
  },
  methods: {
    isEmpty,
    // 远程搜索
    remoteMethod(val) {
      this.queryParams.keyword = val;
      this.getOptions();
    },
    // 下拉框可见性变化
    handleVisibleChange(visible) {
      if (visible) {
        this.getOptions();
      }
    },
    // 全选
    handleSelectAll() {
      if (this.isAllSelected) {
        this.handleChange([]);
      } else {
        const allids = this.options.map(item => item.id);
        this.handleChange(allids);
      }
    },
    handleChange(value) {
      if (this.multiple) {
        let list = this.options.filter(item => value.includes(item.id));
        this.$emit('change', list);
      } else {
        let item = this.options.find(item => value.includes(item.id));
        this.$emit('change', item);
      }
    },
    // 获取选项
    getOptions() {
      this.loading = true;
      this.queryParams = {
        ...this.queryParams,
        ...this.query
      }
      listModel(this.queryParams).then(res => {
        this.options = res.rows;
        this.total = res.total;
      }).finally(() => {
        this.loading = false;
      });
    }
  }
}
</script>

<style lang="scss" scoped>
.select-footer {
  padding: 2px 12px;
  border-bottom: 1px solid #EBEEF5;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  line-height: 1em;
}
</style>