<template>
  <div>
    <el-select v-model="selectVal" style="width: 100%;" placeholder="请选择学校名称" :clearable="clearable" :filterable="filterable" @change="change">
      <el-option v-for="item in schoolList" :key="item.id" :label="item.name" :value="item.id" />
    </el-select>
  </div>
</template>

<script>
import schoolApi from '@/api/basics/schoolApi'

export default {
  name: 'SelectSchool',
  props: {
    selectGrade: {
      type: Boolean
    },
    value: {
      type: String,
      default: ''
    },
    clearable: {
      type: Boolean,
      default: false
    },
    filterable: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      schoolList: []
    }
  },
  computed: {
    selectVal: {
      get() {
        return this.value
      },
      set(newValue) {
        this.$emit('input', newValue)
      }
    }
  },
  watch: {
    selectGrade: {
      // todo add by 梁展鹏 20190829 在开课创建、编辑时需要监听【学校的年级字段】来展示该学校应该展示哪些年级数据。。。目前先绕一下实现
      handler() {
        if (this.value && this.selectGrade) {
          this.change(this.value)
        }
      }
    }
  },
  mounted() {
    this.list()
  },
  methods: {
    list(name) {
      /* add by 梁展鹏 20190807 该接口是管理后台页面支持分页操作的，暂时性HardCode最大数 */
      schoolApi.selectSchoolList(name).then(data => {
        this.schoolList = data.data
      })
    },
    change(newValue) {
      const school = this.schoolList.filter(school => {
        return school.id === newValue
      })[0]
      this.$emit('change', school)
    }
  }
}
</script>

<style scoped>
</style>
