<template>
<div style="float: left">
  <el-button style="margin-left: 20px" type="primary"  size="mini" :disabled="disabled" :readonly="readonly" @click="showUserSelect" plain>
    选择
  </el-button>
    <user-select ref="userSelect" @doSubmit="selectUsersToInput" :limit="limit" ></user-select>
</div>
</template>
<script>
import userSelect from './UserSelectDialog'
import userService from '@/api/sys/UserService'
export default {
  data () {
    return {
      name: '',
      labelValue: this.value,
      selectData: []
    }
  },
  props: {
    limit: Number,
    value: String,
    userName: String,
    size: {
      type: String,
      default: () => { return 'small' }
    },
    readonly: {
      type: Boolean,
      default: () => { return false }
    },
    disabled: {
      type: Boolean,
      default: () => { return false }
    }
  },
  components: {
    userSelect
  },
  beforeCreate () {
  },
  watch: {
    value: {
      handler (newVal) {
        this.selectData = []
        if (newVal) {
          newVal.split(',').forEach((id) => {
            userService.queryById(id).then(({data}) => {
              if (data && data.id !== '') {
                this.selectData.push(data)
              }
            })
          })
        }
      },
      immediate: true,
      deep: false
    },
    selectData: {
      handler (newVal) {
        this.name = newVal.map(user => { return user.name }).join(',')
      },
      immediate: false,
      deep: false
    },
    userName: {
      handler (newVal) {
        this.name = newVal
      },
      immediate: false,
      deep: false
    }
  },
  methods: {
    selectUsersToInput (users) {
      this.selectData = users
      this.labelValue = users.map(user => { return user.id }).join(',')
      this.name = users.map(user => { return user.name }).join(',')
      this.$emit('getValue', this.labelValue, this.name)
    },
    changeName () {
      this.$emit('getValue', null, this.name)
    },
    showUserSelect () {
      this.$refs.userSelect.init()
    }
  }
}
</script>
<style>
  .el-form-item__content .el-input-group {
      vertical-align: middle;
  }
 .el-tag + .el-tag {
    margin-left: 5px;
    margin-bottom: 5px;
  }
</style>


