<template>
  <el-select
    v-model="staffId"
    v-select-load-more="loadSalary"
    filterable
    :clearable="clearable"
    :placeholder="placeholder"
    default-first-option
    size="small"
    @change="handleSelect"
  >
    <el-option
      v-for="item in staffList"
      :key="item.id"
      :label="item.name"
      :value="item.id"
    >
      <div>
        <div class="inline-block item">
          <span class="option">{{ item.name }}({{ item.salary }})</span>
        </div>
      </div>
    </el-option>
  </el-select>
</template>
<script>
import { list } from '@/api/school/salary'
export default {
  props: {
    clearable: {
      type: Boolean,
      default: false
    },
    teacher: {
      type: String,
      default: undefined
    },
    placeholder: {
      type: String,
      default: '选择员工'
    },
    value: {
      type: String,
      default: undefined
    }
  },
  data() {
    return {
      staffId: this.value,
      staffList: [],
      pageNum: 1,
      hasMoreData: false
    }
  },
  watch: {
    value: {
      handler(newValue, oldValue) {
        this.staffId = newValue
      },
      immediate: true
    }
  },
  created() {
    this.loadSalary()
  },
  methods: {
    loadSalary: function() {
      if (this.pageNum === 1) {
        list({
          pageNum: this.pageNum,
          pageSize: 10,
          teacher: this.teacher
        }).then(response => {
          if (response.code === 200) {
            this.staffList = response.data.rows
            this.hasMoreData = response.data.rows.length > 0
            this.pageNum = this.pageNum + 1
          } else {
            this.msgError(response.msg)
          }
        })
      } else if (this.hasMoreData) {
        list({
          pageNum: this.pageNum,
          pageSize: 10,
        }).then(response => {
          if (response.code === 200) {
            this.staffList = this.staffList.concat(response.data.rows)
            this.hasMoreData = response.data.rows.length > 0
            this.pageNum = this.pageNum + 1
          } else {
            this.msgError(response.msg)
          }
        })
      }
    },
    handleSelect: function(val) {
      this.$emit('input', val)
      this.$emit('change', val)
    }
  }
}
</script>
<style ref="stylesheet/scss" lang="scss" scoped>
  .option{
    padding-right: 15px;
    color: #333;
    font-weight: normal;
  }
</style>
