<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/activity';

export default {
  name: 'ActivitySelect',

  props: {
    value: {
      type: String,
      default: ''
    },
    kindergartenId: {
      type: String,
      default: ''
    },
    firstLoad: {
      type: Boolean,
      default: true
    }
  },

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

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

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

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

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

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