<template>
  <el-select
    v-model="vModel"
    placeholder="请选择"
    filterable
    clearable
    v-bind="$attrs"
    @change="onChange"
  >
    <el-option
      v-for="item in options"
      :key="item.value"
      :label="item.label"
      :value="item.value"
    >
    </el-option>
  </el-select>
</template>

<script>
import { getList } from '@/api/scene';

export default {
  name: 'SceneSelect',

  props: {
    value: {
      type: String,
      default: ''
    }
  },

  data() {
    return {
      vModel: this.value,
      options: []
    };
  },

  watch: {
    value: {
      handler(val) {
        this.vModel = val;
      },
      immediate: true
    }
  },

  mounted() {
    this.loadData();
  },

  methods: {
    onChange(value) {
      console.log(value);
      this.$emit('input', value || null);
    },

    async loadData() {
      const { success, data, msg } = await getList();
      if (success) {
        this.options = data.map(x => ({
          value: x.id,
          label: x.name
        }));
      }
    }
  }
};
</script>

<style lang="scss" scoped></style>
