<template>
<!--todo 库房模糊选择 -->
  <div style="display: flex;align-items: center;justify-content: center;width: 100%">
    <el-autocomplete
        style="width: 100%;"
        v-model="query.storeNm"
        :fetch-suggestions="querySearchSignReqNm"
        placeholder="请输入库房名称"
        clearable
        @change="handleBlurChange"
        @select="handleBlur"
    ></el-autocomplete>


    <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
  },
  storeNm: {
    type: String,
    default: false
  },
  storeNm_id: {
    type: String,
    default: false
  },
  querySearch: {
    type: Object,
    default: {
      storeNm: '',
      storeNm_id: '',
    }
  }
})


const query = ref({
  storeNm:''
})



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

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


const querySearchSignReqNm = (queryString, cb) => {
  var ss = []
  var url = 'sto/Config/queryAllKF'
  var obj = {
    activeStatus: '0',
    storeNm: query.value.storeNm
  }
  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.storeNm, id: item.id, storeCd: item.storeCd})
      })
      cb(ss);
    } else {
      ElMessage.error('操作失败！' + response.data.msg)
      cb(ss);
    }
  })
};
const handleBlur = (item) => {
  query.value.id1 = item.id
  query.value.storeNm_id = item.id
  console.log("库房选择···", item)
  emit('receiveFromChild', item)
};

const handleBlurChange = (item) => {

  emit('receiveFromChild', item)
};


onMounted(() => {


})
watch(props, (newValue, oldValue) => {
  query.value = {

    storeNm: props.storeNm,
    storeNm_id: props.storeNm_id
  }

} , {deep: true})



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

<style>
</style>
