<template>
  <view>
    <u-popup v-model="show" mode="bottom" :mask-close-able="false">
      <view class="popup_box">
        <view class="pop_title uni-flex space-between">
          <view class="" style="font-size: 35rpx"> 扫描{{ title }} </view>

          <view class="">
            <image class="fr icons_scan_close" src="/static/icons/icons_scan_close.svg" @click="closeScanPopup()"></image>
          </view>
        </view>
        <view class="uni-flex uni-row" style="align-items: center; background-color: #fff; margin-left: 20rpx; margin-right: 20rpx; border-radius: 8rpx; height: 30px">
          <view class="uni-center" style="width: 25%"> 来源库位 </view>
          <!-- style="width: 75%;padding: 8rpx" -->
          <view class="">
            <!-- <input v-model="fromLocationCode" placeholder="请扫描来源库位" :focus="locationOnFocus"
							placeholder-style="font-size:12px" style="padding: 5px;" @confirm="scanLocation" /> -->

            <view v-if="allowModifyLocation">
              <uni-combox :candidates="fromLocationList" v-model="fromLocationCode" placeholder="请扫描来源库位" style="height: 30rpx; border: 1px solid #fff"></uni-combox>
            </view>
            <view v-else>
              <text style="padding: 5px">
                {{ fromLocationCode }}
              </text>
            </view>
          </view>
        </view>
        <view class="">
          <view class="">
            <win-com-scan ref="comscan" :placeholder="title" @getResult="getScanResult" :isShowHistory="isShowHistory" :clearResult="true" :headerType="headerType"></win-com-scan>
          </view>
        </view>
      </view>
    </u-popup>
    <!-- <balance-select ref="balanceSelectRef" @onSelectItem='selectBalanceItem'></balance-select> -->
  </view>
  <com-message ref="comMessageRef" />
</template>

<script setup lang="ts">
import { ref, getCurrentInstance, onMounted, nextTick, watch } from 'vue'
import winComScan from '@/mycomponents/scan/winComScan.vue'
import balanceSelect from '@/mycomponents/balance/balanceSelect.vue'
import { getBalanceByManagementPrecision } from '@/common/balance.js'

import { getBasicLocationByCode } from '@/api/request2.js'

import { getListLocationTypeDesc, checkDirectoryItemExist, getDirectoryItemArray, getLocationTypeName, getInventoryStatusDesc } from '@/common/directory.js'

const { proxy } = getCurrentInstance()
const props = defineProps({
  title: {
    type: String,
    default: '箱标签'
  },
  isShowHistory: {
    type: Boolean,
    default: true
  },
  allowNullBalance: {
    type: Boolean,
    default: false
  },
  // 不提示库存
  noShowBalanceMessage: {
    type: Boolean,
    default: false
  },
  // 是否允许修改库位
  allowModifyLocation: {
    type: Boolean,
    default: true
  },
  headerType: {
    type: String,
    default: 'HPQ,HMQ'
  }
})
const scanResult = ref({})
const show = ref(false)
const scanList = ref([])
const expand = ref(false)
const showScanResult = ref({})
const expendIcon = ref('arrow-down')
const fromLocationCode = ref('')
const fromLocation = ref('')
const fromLocationList = ref([])
const fromLocationTypeArray = ref([])
const locationOnFocus = ref(false)
const businessType = ref({})
const inventoryStatus = ref([])
const managementPrecision = ref('')
const fromInventoryStatuses = ref('')
const isCheck = ref(false)
const balanceSelectRef = ref()
const comscan = ref()
const comMessageRef = ref()
// 直接扫描
const openScanPopupForType = (fromLocationCodeParms, businessTypeParms) => {
  businessType.value = businessTypeParms
  fromLocationCode.value = fromLocationCodeParms
  fromInventoryStatuses.value = getDirectoryItemArray(businessType.value.outInventoryStatuses)
  inventoryStatus.value = getDirectoryItemArray(businessType.value.outInventoryStatuses) // 出库库存状态
  fromLocationTypeArray.value = getDirectoryItemArray(businessType.value.outLocationTypes) // 出库库存状态
  show.value = true
  setTimeout((res) => {
    if (fromLocationCodeParms != '') {
      packGetFocus()
    } else {
      locationGetFocus()
    }
  }, 500)
}
// 在任务中扫描
const openScanPopupForJob = (fromLocationCodeParms, fromLocationListParms, jobContent) => {
  fromLocationCode.value = fromLocationCodeParms
  fromLocationList.value = fromLocationListParms
  fromInventoryStatuses.value = getDirectoryItemArray(jobContent.outInventoryStatuses)
  inventoryStatus.value = getDirectoryItemArray(jobContent.outInventoryStatuses) // 出库库存状态; //出库库存状态
  fromLocationTypeArray.value = getDirectoryItemArray(jobContent.fromLocationTypes) // 出库库存状态
  show.value = true
  setTimeout((res) => {
    if (fromLocationListParms != '') {
      packGetFocus()
    } else if (fromLocationList.value.length == 0) {
      locationGetFocus()
    } else {
      fromLocationCode.value = fromLocationList.value[0]
      packGetFocus()
    }
  }, 500)

}
const closeScanPopup = (content) => {
  packLoseFocus()
  show.value = false
  emit('close', '')
}
const getScanResult = (scanResult) => {
  scanResult.value = scanResultParams
  const isCheck = false
  if (fromLocationCode.value == '') {
    showErrorMessage('来源库位不能为空', (callback) => {
      locationGetFocus()
    })
    return
  }

  if (fromLocationList.value.length > 0) {
    if (!isInLocationList(fromLocationCode.value)) {
      showErrorMessage(`扫描库位【${fromLocationCode.value}】不在任务来源库位中`, (callback) => {
        locationGetFocus()
      })
      return
    }
  }

  uni.showLoading({
    title: '扫描中...',
    mask: true
  })

  getBasicLocationByCode(fromLocationCode.value)
    .then((res) => {
      if (res.data.total > 0) {
        const result = res.data.list[0]
        if (result.code != fromLocationCode.value) {
          showErrorMessage(`未查询到库位[${fromLocationCode.value}]`)
          return
        }
        const { type } = result
        const { available } = result
        if (available == 'TRUE') {
          if (checkDirectoryItemExist(fromLocationTypeArray.value, type)) {
            location.value = result
            packCallBack()
          } else {
            uni.hideLoading()
            const hint = getListLocationTypeDesc(fromLocationTypeArray.value)
            showErrorMessage(`库位[${fromLocationCode.value}]是${getLocationTypeName(type)},\n需要的库位类型是[${hint}]`, (callback) => {
              locationGetFocus()
            })
          }
        } else {
          showErrorMessage(`扫描库位[${fromLocationCode.value}]不可用`, (res) => {
            locationGetFocus()
          })
        }
        uni.hideLoading()
      } else {
        uni.hideLoading()
        showErrorMessage(`未查询到库位[${fromLocationCode.value}]`, (res) => {
          locationGetFocus()
        })
      }
    })
    .catch((error) => {
      uni.hideLoading()
      showErrorMessage(error, (res) => {
        locationGetFocus()
      })
    })
}

const packCallBack = (item) => {
  comscan.value.clear()
  // 返回标签信息、库存信息和管理模式
  const data = {
    label: scanResult.value.label,
    package: scanResult.value.package,
    balance: null,
    fromLocationCode: fromLocationCode.value
  }
  packGetFocus()
  emit('getResult', data)
}

const packGetFocus = () => {
  if (comscan.value != undefined) {
    comscan.value.getfocus()
  }
}
const packLoseFocus = () => {
  if (comscan.value != undefined) {
    comscan.value.getfocus()
  }
}
const locationGetFocus = () => {
  fromLocationCode.value = ''
  locationOnFocus.value = true
}
const showMessage = (message, callback) => {
  setTimeout((r) => {
    packLoseFocus()
    comMessageRef.value.showMessage(message, callback)
  })
}

const showErrorMessage = (message, callback) => {
  setTimeout((r) => {
    packLoseFocus()
    comMessageRef.value.showErrorMessage(message, callback)
  })
}

const isInLocationList = (location) => {
  let item = fromLocationList.value.find((res) => res == location)

  if ((item = undefined)) {
    return false
  }
  return true
}
const addLocationCode = (code) => {
  if (!isInLocationList(code)) {
    fromLocationList.value.push(code)
  }
}
// 传递给父类
const emit = defineEmits(['close', 'getCountScanResult', 'getResult'])
defineExpose({ packGetFocus, packLoseFocus, openScanPopupForJob })
</script>

<style lang="scss">
.scroll-view {
  overflow-y: scroll;
  height: auto;
  max-height: 300rpx;
}
</style>
