<!--
  iqc检验项下拉选择组件
 -->
<template>
  <el-select
    v-model="selected"
    v-bind="$attrs"
    :loading="loading"
    @change="change"
  >
    <el-option
      v-for="item in options"
      :key="item.id"
      :label="item.name"
      :value="item.id"
    />
  </el-select>
</template>
<script>
import { getMaterialItemMasterPage } from '@/api/wms/quality/inspectionCase'

export default {
  props: {
    value: {
      type: [String, Number],
      default: ''
    },
    type: {
      type: [String, Number],
      default: ''
    },
    auditStatus: {
      type: [String, Number],
      default: ''
    }
  },
  data() {
    return {
      options: [],
      loading: false
    }
  },
  computed: {
    selected: {
      get() {
        return this.value
      },
      set(v) {
        this.$emit('input', v)
      }
    }
  },
  created() {
    this.fetchInspectionList()
  },
  methods: {
    fetchInspectionList() {
      this.loading = true
      const params = {
        pageNo: 1,
        pageSize: 100,
        status: 1,
        type: this.type,
        auditStatus: 1
      }
      getMaterialItemMasterPage(params)
        .then((res) => {
          const { list = [] } = res.data || {}
          this.options = list
        })
        .finally(() => {
          this.loading = false
        })
    },
    change(v) {
      const itemName = this.options.find(item => item.id === v)
      if (itemName) {
        this.$emit('change', itemName)
      }
    }
  }
}
</script>
