<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.customerName }}</el-col>
      </el-row>
    </el-option>
  </el-select>

</template>

<script>
import { getSaleOrderPage } from '@/api/wms/orders/sale'

export default {
  props: {
    value: {
      type: [String, Number],
      default: ''
    },
    customerCode: {
      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.fetchSalesList()
  // },
  methods: {
    fetchSalesList(erpOrderNo) {
      this.loading = true
      const params = {
        pageNo: 1,
        pageSize: 200,
        erpOrderNo,
        customerCode: this.customerCode,
        materialNo: this.materialNo
      }
      getSaleOrderPage(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.fetchSalesList(v)
    },
    // 下拉框出现时调接口
    handleCaseVisibleChange(visible) {
      if (visible) {
        this.fetchSalesList()
      }
    }
  }
}
</script>

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

