<template>
  <el-select
    v-model="selected"
    v-bind="$attrs"
    :loading="loading"
    filterable
    :filter-method="filterValue"
    @visible-change="handleCaseVisibleChange"
    @change="change"
  >
    <el-option
      v-for="item in options"
      :key="item.id"
      :label="item.erpOrderNo"
      :value="item.erpOrderNo"
    >
      <el-row :gutter="10">
        <el-col :span="6" class="long-text" :title="item.erpOrderNo">{{ item.erpOrderNo }}</el-col>
        <el-col :span="6" class="long-text" :title="item.materialNo">{{ item.materialNo }}</el-col>
        <el-col :span="12" class="text-right long-text">{{ item.materialName }}</el-col>
      </el-row>
    </el-option>
  </el-select>

</template>

<script>
import { getProductionOrderPage } from '@/api/wms/output/shipmentDetail'

export default {
  props: {
    value: {
      type: [String, Number],
      default: ''
    },
    materialNo: {
      type: [String, Number],
      default: ''
    }
  },
  data() {
    return {
      options: [],
      loading: false
    }
  },
  computed: {
    selected: {
      get() {
        return this.value
      },
      set(v) {
        this.$emit('input', v)
      }
    }
  },
  // created() {
  //   this.fetchProductsList()
  // },
  methods: {
    fetchProductsList(erpOrderNo) {
      this.loading = true
      const params = {
        pageNo: 1,
        pageSize: 200,
        erpOrderNo,
        materialNo: this.materialNo
      }
      getProductionOrderPage(params).then(res => {
        const { list = [] } = res.data || {}
        this.options = list
      }).finally(() => {
        this.loading = false
      })
    },
    change(v) {
      const item = this.options.find(item => item.erpOrderNo === v)
      this.$emit('change', item)
    },
    filterValue(v) {
      this.fetchProductsList(v)
    },
    // 下拉框出现时调接口
    handleCaseVisibleChange(visible) {
      if (visible) {
        this.fetchProductsList()
      }
    }
  }
}
</script>

  <style scoped>
/* 在您的全局或组件CSS文件中 */
.long-text {
  overflow: hidden; /* 隐藏超出部分的内容 */
  text-overflow: ellipsis; /* 当内容溢出时显示省略号 */
  white-space: nowrap; /* 禁止文本换行 */
}
  </style>

