<template>
  <el-select
    v-model="departId"
    :placeholder="placeholder"
    clearable
    :size="size"
    filterable
    default-first-option
    :disabled="disabled"
    :loading="loadingSelect"
    @change="handleSelect"
  >
    <el-option
      v-for="item in campusOptions"
      :key="item.id"
      :label="item.label"
      :value="item.id"
    />
  </el-select>
</template>
<script>
import { campusList } from '@/api/school/system/dept'

export default {
  props: {
    size: {
      type: String,
      default: undefined
    },
    value: {
      type: String,
      default: undefined
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 开课校区
      campusOptions: [],
      departId: this.value,
      loadingSelect: false,
      placeholder: '请选择上课校区'
    }
  },
  watch: {
    value: {
      handler(newValue, oldValue) {
        this.departId = newValue
      },
      immediate: true
    }
  },
  created() {
    this.campusList()
  },
  activated() {
    // 当组件被 keep-alive 缓存后激活时，重新获取数据
    this.campusList()
  },
  methods: {
    campusList() {
      campusList().then(response => {
        response.data.forEach(item=>{
          item.id=item.id+''
        })
        this.campusOptions = response.data
      })
    },
    handleSelect: function(val) {
      this.$emit('input', val)
      this.$emit('change', val)
    }
  }
}
</script>
