<template>
  <div class="google-developer-selector">
    <el-select
      v-model="selectedValue"
      :placeholder="placeholder"
      filterable
      clearable
      @change="handleChange"
      :disabled="disabled"
      class="selector"
    >
      <el-option
        v-for="item in options"
        :key="item.id"
        :label="item.email"
        :value="item.id"
      >
      </el-option>
    </el-select>
  </div>
</template>

<script>
import { getAvailableDevelopers } from '@/api/googleDeveloper'

export default {
  name: 'GoogleDeveloperSelector',
  props: {
    value: {
      type: [Number, String],
      default: ''
    },
    placeholder: {
      type: String,
      default: '请选择开发者账号'
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      options: [],
      loading: false
    }
  },
  computed: {
    selectedValue: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('input', val)
      }
    }
  },
  created() {
    this.fetchOptions()
  },
  methods: {
    async fetchOptions() {
      this.loading = true
      try {
        const res = await getAvailableDevelopers()
        if (res.status === 200 && res.result && res.result.data) {
          this.options = res.result.data
        } else {
          this.$message.error('获取Google开发者账号列表失败')
        }
      } catch (error) {
        console.error('获取Google开发者账号列表失败:', error)
        this.$message.error('获取Google开发者账号列表失败')
      } finally {
        this.loading = false
      }
    },
    handleChange(value) {
      this.$emit('change', value)
    }
  }
}
</script>

<style scoped>
.google-developer-selector {
  display: inline-block;
}

.selector {
  width: 100%;
}
</style> 