<!-- 选择试验 -->

<template>
  <div>
    <el-input v-model="selected.name" :placeholder="$t('form.placeholderSelect')" :disabled="disabled"
      @click.native="showStudy" />
    <SelectStudyDialog ref="selectStudyDialog" @callback="handleChange" />
  </div>
</template>

<script>
import SelectStudyDialog from './SelectStudyDialog.vue'
export default {
  name: "SelectStudy",
  components: { SelectStudyDialog },
  props: {
    value: {
      type: [Number, String, Array],
      default: ''
    },
    selectkey: {
      type: String,
      default: ''
    },
    disabled: {
      type: Boolean,
      default: false
    },
    needPre: {
      type: Number,
      default: 0
    },
  },
  watch: {
    value: {
      immediate: true,
      handler(v) {
        this.selected.id = v ? ((v + '').indexOf('u_') > -1 ? v : ('u_' + v)) : ''
      }
    }
  },
  data() {
    return {
      selected: {
        id: '',
        name: ''
      },
    };
  },
  mounted() {
  },
  methods: {
    showStudy() {
      if (!this.disabled) {
        this.$refs.selectStudyDialog.show({ needPre: this.needPre })
      }
    },
    handleChange(obj) {
      this.selected.name = obj.name
      this.selected.id = obj.id
      this.$emit('change', _.merge({}, obj, { key: this.selectkey }))
      this.$emit('input', obj.id)
    },
  }
};
</script>