<template>
  <!--todo 库房模糊选择,但库房为必选项 -->
<!-- 库房选择器，  设置库房为必选项 ，依旧可以模糊查询 -->
  <div style="display: flex;align-items: center;justify-content: center;width: 100%">

    <el-select
        ref="selectRef"
        v-model="query.storeNm"
        filterable
        remote
        reserve-keyword
        @change="handleBlur"
        placeholder="请输入或选择库房名称"
        :remote-method="remoteMethod"
        :loading="loading"
        clearable
    >
      <el-option
          v-for="item in options"
          :key="item.id"
          :label="item.storeNm"
          :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
  },
  storeNm: {
    type: String,
    default: ''
  },
  storeNm_id: {
    type: String,
    default: ''
  },
  ifItemKf: {
    type: Boolean,
    default: false
  },
  itemCd: {
    type: String,
    default: ''
  },
  storeType: {
    type: String,
    default: ''
  },
  querySearch: {
    type: Object,
    default: {
      storeNm: '',
      storeNm_id: '',
    }
  }
})


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



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

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


const handleBlur = (item) => {

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

  emit('receiveFromChild', re)
};

const handleBlurChange = (item) => {

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

    })
  }, 200)
}
const tableData = ref([])
const getTableData = (queryString, cb) => {
  loading.value = true
  var url = 'sto/Config/queryAllKF'
  var obj = {
    fId: query.value.kfId,
    areaCd: query.value.areaCd,
    itemCd: props.itemCd,
    storeType: props.storeType,
    ifItemKf: props.ifItemKf,
  }
  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)

    }
  })

};
onMounted(() => {
  getTableData()

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

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

} , {deep: true})



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

<style>
</style>
