<template>
<!-- 垛位 选择器-->
  <div style="display: flex;align-items: center;justify-content: center;width: 100%">
<!--    {{query.qhId}}-->
<!--    <el-autocomplete-->
<!--        v-model="query.stackCd"-->
<!--        style="width: 100%;"-->
<!--        :fetch-suggestions="querySearStackCd"-->
<!--        placeholder="请输入垛位号"-->
<!--        clearable-->
<!--        @change="handleBlurDWHChange"-->
<!--        @select="handleBlurDWH"-->
<!--    ></el-autocomplete>-->
    <el-select
        clearable
        v-model="query.stackCd"
        filterable
        remote
        reserve-keyword
        @change="handleBlurDWH"
        placeholder="请选择垛位号"
        :remote-method="remoteMethod"
        :loading="loading"
    >
      <el-option
          v-for="item in options"
          :key="item.id"
          :label="item.stackCd"
          :value="item.id"
      />
    </el-select>
    <el-button v-if="buttonShow" plain @click="handleSelect()">选择</el-button>
  </div>
</template>

<script setup>

import {
  ref,
  onMounted,
  watch,
  getCurrentInstance
} from 'vue'

const {
  proxy
} = getCurrentInstance()

import tools from '@/tools'

const cardBodyStyle = ref(tools.style.card)

const dialogVisible = ref(false)
const open = () => {
  dialogVisible.value = true
}
const close = () => {
  dialogVisible.value = false
}

const props = defineProps({
  buttonShow: {
    type: Boolean,
    default: false
  },
  qhId: {
    type: String,
    default: ''
  },
  stackCd: {
    type: String,
    default: ''
  },
  stackCd_id: {
    type: String,
    default: ''
  }

})
const query = ref({})


const emit = defineEmits(['receiveFromChild'])
const handleSelect = (row) => {

  emit('receiveFromChild', query.value)
  close()
}


const querySearStackCd = (queryString, cb) => {
  var ss = []
  var url = 'sto/Config/queryAllDWH'
  var obj = {
    fId: query.value.qhId,
    stackCd: query.value.stackCd
  }
  var param = {
    params: JSON.stringify(obj),
  }
  proxy.$axios.get(url, param).then(response => {
    if (response.data.code == '0') {
      response.data.data.records.forEach((item) => {
        ss.push({value: item.stackCd, id: item.id})
      })
      cb(ss);
    } else {
      ElMessage.error('操作失败！' + response.data.msg)
      cb(ss);
    }
  })
};

const tableData = ref([])
const getTableData = (queryString, cb) => {
  loading.value = true
  var ss = []
  var url = 'sto/Config/queryAllDWH'
  var obj = {
    fId: query.value.qhId
  }
  var param = {
    params: JSON.stringify(obj),
  }

  proxy.$axios.get(url, param).then(response => {
    loading.value = false
    if (response.data.code == '0') {
      tableData.value = response.data.data.records
    } else {
      ElMessage.error('操作失败！' + response.data.msg)

    }
  })

};

const handleBlurDWH = (item) => {

  var op = options.value.find(option => option.id === item);
  var re = {
    id: '',
    stackCd: '',
    value: '',
  }
  if (op){
    re = {
      id: op.id,
      stackCd: op.stackCd,
      value: op.stackCd,
    }
  }

  emit('receiveFromChild', re)
};
const handleBlurDWHChange = (item) => {
  emit('receiveFromChild', item)
};
const options = ref([])
const loading = ref(false)
const remoteMethod = (val) => {
  loading.value = true
  setTimeout(() => {
    loading.value = false
    options.value = tableData.value.filter((item) => {
      if (!item){
        return item
      }
      return item.stackCd.includes(val)

    })
  }, 200)
}


watch(props, (newValue, oldValue) => {
  query.value = {
    qhId: props.qhId,
    stackCd: props.stackCd,
    stackCd_id: props.stackCd_id
  }
  getTableData()
}, {deep: true})

onMounted(() => {
  getTableData()

})

defineExpose({
  open,
  close
})
</script>

<style>
</style>
