<template>
  <el-select
    v-model="selected"
    :loading="loading"
    :disabled="disabled"
    clearable
    @change="change"
    @visible-change="handleVisibleChange"
  >
    <el-option
      v-for="item in options"
      :key="item.badNo"
      :label="item.badName"
      :value="item.badNo"
    />
  </el-select>

</template>

<script>
import { getBadTemsMPage } from '@/api/wms/quality/badCode'

export default {
  props: {
    value: {
      type: [String, Number],
      default: ''
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      options: [],
      loading: false
    }
  },
  computed: {
    selected: {
      get() {
        return this.value
      },
      set(v) {
        this.$emit('input', v)
      }
    }
  },
  // created() {
  //   this.fetchBadcodeList()
  // },
  methods: {
    fetchBadcodeList() {
      this.loading = true
      const params = {
        pageNo: 1,
        pageSize: 100
      }
      getBadTemsMPage(params).then(res => {
        const { list = [] } = res.data || {}
        this.options = list
      }).finally(() => {
        this.loading = false
      })
    },
    change(v) {
      const index = this.options.find(item => item.badNo === v)
      this.$emit('change', index)
    },
    handleVisibleChange(visible) {
      if (visible) {
        this.fetchBadcodeList()
      }
    }
  }
}
</script>

  <style>

  </style>
