<template>
  <view>
    <u-popup mode="bottom" v-model="show" :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>
    <com-message ref="comMessageRef" @afterClose="getfocus" />
  </view>
</template>

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

import { getLabelInfo } from '@/common/label.js'

const props = defineProps({
  title: {
    type: String,
    default: '箱标签'
  },
  isShowHistory: {
    type: Boolean,
    default: true
  },
  headerType: {
    type: String,
    default: 'HPQ'
  }
})
const show = ref(false)
const comMessageRef = ref()
const comscan = ref()
const comscansimulate = ref()
// 模拟扫描功能
const simulateScan = (scanMessage) => {
  getLabelInfo(scanMessage, props.headerType, (callback) => {
    if (callback.success) {
      getScanResult(callback)
    } else {
      showMessage(callback.message)
    }
  })
}
const openScanPopup = () => {
  show.value = true
  setTimeout((res) => {
    getfocus()
  }, 200)
}

const closeScanPopup = () => {
  losefocus()
  show.value = false
  emit('close', '')
}

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

const getScanResult = (result) => {
  if (result.success) {
    emit('getResult', result)
  } else {
    showMessage(result.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(['close', 'getResult'])
defineExpose({
  openScanPopup,
  closeScanPopup,
  getfocus,
  losefocus,
  simulateScan
})
</script>

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