<template>
  <el-select
    v-model="selected"
    @focus="fetchItems"
    :placeholder="placeholder"
    :filterable="filterable"
    :value-key="valueKey"
    :style="selectStyle"
    @change="onChange"
    :multiple="multiple"
  >
    <el-option
      v-for="item in items"
      :key="item.id"
      :label="item.code"
      :value="item"
    >
      <span>{{ item.code }}</span>
      <span>{{ item.area.name }}</span>
      <span>{{ item.itemCode }}</span>

    </el-option>

  </el-select>
</template>

<script>
import curdPoint, {queryPointList} from '@/api/point'

export default {
  name: 'PointSelect',
  props: {
    value: null, // v-model
    placeholder: {
      type: String,
      default: '请选择库位'
    },
    filterable: {
      type: Boolean,
      default: true
    },
    valueKey: {
      type: String,
      default: 'id'
    },
    isEnabled:{
      type: Boolean,
      default: true
    },
    point_type:{
      type: String,
      default: true
    },
    selectStyle: {
      type: String,
      default: 'width: 200px;'
    },
    immediate: {
      type: Boolean,
      default: false
    },
    // 是否支持多选
    multiple: {
      type: Boolean,
      default: false
    },
  },
  data() {
    return {
      items: [],
      selected: this.value
    }
  },
  watch: {
    value(val) {
      this.selected = val
    },
    selected(val) {
      this.$emit('input', val)
      this.$emit('change', val)
    }
  },
  created() {
    // 如果设置了immediate属性或已有值，则立即加载数据
    if (this.immediate || (this.value && this.value[this.valueKey])) {
      this.fetchItems();
    }
  },
  methods: {
    fetchItems() {
      if (this.items.length === 0) {
        curdPoint.queryPointList({ type: this.point_type }).then(res => {
          this.items = res
        })
      }
    },
    onChange(val) {
      this.$emit('change', val)
    }
  }
}
</script>
