<template>
  <view>
    <u-popup v-model="show" mode="bottom">
      <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-fg ref="comscan" :placeholder="title" @getResult="getScanResult" :isShowHistory="isShowHistory" :clearResult="true"></win-com-scan-fg>
          </view>
        </view>
      </view>
    </u-popup>
    <com-message ref="comMessageRef" @afterClose="getfocus" />
  </view>
</template>

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

const props = defineProps({
  title: {
    type: String,
    default: '成品标签'
  },
  isShowHistory: {
    type: Boolean,
    default: true
  }
})
const show = ref(false)
const comscan = ref()
const openScanPopup = (itemCode) => {
  setTimeout((res) => {
    show.value = true
    setTimeout((re) => {
      comscan.value.setItemCode(itemCode)
    }, 500)
  }, 200)
}

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

const scanClick = () => {
  comscan.value.clickScanMsg()
}
const cancelClick = () => {
  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(['getResult', 'close'])
defineExpose({
  openScanPopup,
  closeScanPopup,
  losefocus,
  getfocus
})
</script>

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