<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="">
          <view class="">
            <win-com-scan ref="comscan" :placeholder="title" @getResult="getScanResult" :headerType="headerType" :isShowHistory="isShowHistory" :clearResult="true"></win-com-scan>
          </view>
        </view>
      </view>
    </u-popup>
    <!-- 模拟扫描功能 -->
    <win-com-scan v-show="false" ref="comscansimulate" @getResult="getScanResult" :headerType="headerType" :isShowHistory="false" :clearResult="true"></win-com-scan>
    <com-message ref="comMessageRef" />
  </view>
</template>

<script setup lang="ts">
import { ref, getCurrentInstance, onMounted, nextTick, watch } from 'vue'
import winComScan from '@/mycomponents/scan/winComScan.vue'
import { getBalanceByFilter } from '@/api/request2.js'

const props = defineProps({
  title: {
    type: String,
    default: '箱标签'
  },
  isShowHistory: {
    type: Boolean,
    default: true
  },
  headerType: {
    type: String,
    default: 'HPQ,HMQ'
  }
})
const show = ref(false)
const comscansimulate = ref()
const comscan = ref()
const comMessageRef = ref()
const businessType = ref(null)
const simulateScan = (item) => {
  comscansimulate.value.setItemCodeSimulate(item.copyContent)
  comscansimulate.value.clickScanMsg()
}
const openScanPopup = () => {
  setTimeout((res) => {
    show.value = true
  }, 200)
}

const closeScanPopup = () => {
  show.value = false
  emit('close', '')
}
const openScanPopupByBusinessType = (businessTypeParams) => {
  setTimeout((res) => {
    show.value = false
    businessType.value = businessTypeParams
  }, 200)
}
const scanClick = () => {
  if (comscan.value) {
    comscan.value.clickScanMsg()
  }
}

const cancelClick = () => {
  if (comscan.value) {
    comscan.value.clearScanValue()
  }
}

const getScanResult = (result) => {
  if (result.success) {
    getBalance(result, (res) => {
      result.balance = res
      emit('getResult', result)
    })
  } else {
    showMessage(result.message)
  }
}
const getBalance = (result, callback) => {
  const filters = []
  const { label } = result
  const packageInfo = result.package

  if (packageInfo.parentNumber) {
    const packingNumber = `${packageInfo.parentNumber},${label.packingNumber}`
    filters.push({
      column: 'packingNumber',
      action: 'in',
      value: packingNumber
    })
  } else {
    filters.push({
      column: 'packingNumber',
      action: '==',
      value: label.packingNumber
    })
  }

  filters.push({
    column: 'itemCode',
    action: '==',
    value: label.itemCode
  })
  filters.push({
    column: 'batch',
    action: '==',
    value: label.batch
  })

  if (businessType.value != null) {
    filters.push({
      column: 'inventoryStatus',
      action: 'in',
      value: businessType.value.inInventoryStatuses
    })
  }

  const params = {
    filters,
    pageNo: 1,
    pageSize: 100
  }
  getBalanceByFilter(params)
    .then((res) => {
      if (res.length == 0) {
        showErrorMessage('未查找到该包装的库存余额', (res) => {
          packGetFocus()
        })
      } else {
        callback(res.data)
      }
    })
    .catch((err) => {
      showErrorMessage(err.message)
    })
}
const getfocus = () => {
  if (comscan.value != undefined) {
    comscan.value.getfocus()
  }
}

const losefocus = () => {
  if (comscan.value != undefined) {
    comscan.value.losefocus()
  }
}

const showMessage = (message) => {
  comMessageRef.value.showMessage(message)
}
// 传递给父类
const emit = defineEmits(['getResult'])
</script>

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