<template>
  <el-select
    class="resource-select"
    :class="{select__block: !inline}"
    :placeholder="placeholder"
    :clearable="clearable"
    :disabled="disabled"
    :multiple="multiple"
    @change="$emit('change', $event)"
    @input="$emit('input', $event)"
  >
    <el-option
      v-for="data in list"
      :key="data.id"
      :value="data[valueKey]"
      :label="data.name"
    />
  </el-select>
</template>

<script>
import { fetchAll } from '@/api/cms/resource.group'

export default {
  name: 'ResourceGroupSelect',
  props: {
    disabled: {
      default: false
    },
    valueKey: {
      default: 'id'
    },
    placeholder: {
      default: '请选择资源组'
    },
    inline: {
      default: true
    },
    multiple: {
      default: false
    },
    clearable: {
      default: true
    }
  },
  data () {
    return {
      loading: false,
      list: []
    }
  },
  methods: {
    // 查询数据
    fetchList () {
      if (this.loading) {
        return
      }
      this.loading = true
      fetchAll()
        .then(data => {
          this.list = data
        })
        .catch(e => {
          this.$tip.apiFailed(e)
        })
        .finally(() => {
          this.loading = false
        })
    }
  },
  created () {
    this.fetchList()
  }
}
</script>
<style lang="scss" scoped>
.select__block {
  display: block;
}
</style>
