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


    <el-autocomplete
        v-if="level2"
        v-model="query.areaCd"
        style="width: 100%;"
        :fetch-suggestions="querySearChareaCd"
        placeholder="请输入区号"
        clearable
        @change="handleBlurQhChange"
        @select="handleBlurQh"
    ></el-autocomplete>

    <el-autocomplete
        v-if="level3"
        v-model="query.stackCd"
        style="width: 100%;"
        :fetch-suggestions="querySearStackCd"
        placeholder="请输入垛位号"
        clearable
        @change="handleBlurDWHChange"
        @select="handleBlurDWH"
    ></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({
  // tableData: { // form查询数据
  //   type: Array,
  //   default: () => []
  // },
  url: { // form查询数据
    type: String,
    default: 'bas/Partner/query'
  },
  level1: {
    type: Boolean,
    default: false
  },
  level2: {
    type: Boolean,
    default: false
  },
  level3: {
    type: Boolean,
    default: false
  },
  buttonShow: {
    type: Boolean,
    default: false
  },
  querySearch: {
    type: Object,
    default: {
      id1: '',
      id2: '',
      id3: '',
      storeNm: '',
      storeNm_id: '',
      areaCd: '',
      areaCd_id: '',
      stackCd: '',
      stackCd_id: '',
    }
  }
})
const query = ref({})


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

  emit('receiveFromChild', query.value)
  close()
}
const currentPage = ref(1)
const pageSize = ref(10)
const pageSizes = ref([10, 20, 30, 40])
const tableData = ref([])
const totalRows = ref()

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)
};

const querySearChareaCd = (queryString, cb) => {
  var ss = []
  var url = 'sto/Config/queryAllQH'
  var obj = {
    fId: query.value.id1,
    areaCd : query.value.areaCd,
  }
  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.areaCd, id: item.id})
      })
      cb(ss);
    } else {
      ElMessage.error('操作失败！' + response.data.msg)
      cb(ss);
    }
  })
};
const querySearStackCd = (queryString, cb) => {
  var ss = []
  var url = 'sto/Config/queryAllDWH'
  var obj = {
    fId: query.value.id2,
    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 handleBlurQh = (item) => {
  console.log("区号选择···", item)
  query.value.id2 = item.id
  query.value.areaCd_id = item.id
  emit('receiveFromChild', item)
};
const handleBlurQhChange = (item) => {
  emit('receiveFromChild', item)
};
const handleBlurDWH = (item) => {

  console.log("区号选择···", item)
  query.value.id3 = item.id
  query.value.stackCd_id = item.id
  emit('receiveFromChild', item)
};
const handleBlurDWHChange = (item) => {
  emit('receiveFromChild', item)
};

const search = () => {
}
const handleSizeChange = (val) => {
  pageSize.value = val
}
const handleCurrentChange = (val) => {
  currentPage.value = val
}

onMounted(() => {
  // initTableData()

})
watch(dialogVisible, (newValue, oldValue) => {
  if (newValue) {

  } else {

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

<style>
</style>
