<template>
  <view class="">
    <u-popup mode="bottom" v-model="isShow">
      <view class="popup_box">
        <view class="pop_title uni-flex space-between">
          <view class=""> 扫描{{ title }} </view>
          <view class="">
            <image class="icons_scan_close" src="@/static/icons/icons_scan_close.svg" @click="closeScanPopup()"></image>
          </view>
        </view>
        <view class="">
          <view class="">
            <win-com-scan ref="scan" @getResult="getScanResult" :placeholder="title" :clearResult="false" headerType="HMQ,HPQ"> </win-com-scan>
          </view>
        </view>
      </view>
    </u-popup>
    <com-message ref="comMessageRef" />
  </view>
</template>

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

import { checkDirectoryItemExist, getListItemTypeDesc, getItemTypeInfo } from '@/common/directory.js'

import winComScan from '@/mycomponents/scan/winComScan.vue'

const { proxy } = getCurrentInstance()
const props = defineProps({
  title: {
    type: String,
    default: ''
  },
  isShowRecord: {
    type: Boolean,
    default: true
  },
  itemCodeTypeList: {
    type: Array,
    default: []
  }
})
const code = ref('')
const result = ref({})
const isShow = ref(false)
const expand = ref(false)
const scanList = ref([])
const scan = ref()
const expendIcon = ref('arrow-down')
const comMessageRef = ref()
const openScanPopup = () => {
  setTimeout((res) => {
    isShow.value = true
  }, 500)
}
const closeScanPopup = () => {
  isShow.value = false
}
const getfocus = () => {
  if (isShow.value) {
    scan.value.getfocus()
  }
}
const scanClick = () => {
  scan.value.clickScanMsg()
}
const cancelClick = () => {
  scan.value.clearScanValue()
}
const getScanResult = (resultParams) => {
  proxy.$modal.loading('扫描中...')

  const scanResult = resultParams
  if (scanResult.label.barType == 'QRCode') {
    code.value = scanResult.label.itemCode
  } else if (scanResult.label.barType == 'BarCode') {
    code.value = scanResult.label.code
  }
  if (code.value == undefined) {
    uni.hideLoading()
    showErrorMessage(`扫描物料[${code.value}]为空,请输入正确的物料`)
    return
  }
  getBasicItemByCode(code.value)
    .then((res) => {
      uni.hideLoading()
      if (res.data != null && res.data.list.length > 0) {
        const result1 = res.data.list[0]
        const { status } = result1
        const { type } = result1
        const itemCode = result1.code
        const itemName = result1.name
        const { uom } = result1
        const std = result1.uom
        result1.package = scanResult.package
        if (status == 'ENABLE') {
          if (checkDirectoryItemExist(props.itemCodeTypeList, type)) {
            result.value = result1
            callBack()
          } else {
            const hint = getListItemTypeDesc(props.itemCodeTypeList)
            showErrorMessage(`扫描物料[${code.value}]是[${getItemTypeInfo(type).label}],需要的物料类型是[${hint}]`)
          }
        } else {
          showErrorMessage(`物料【${code.value}】不可用`)
        }
      } else {
        showErrorMessage(`未查找到物料【${code.value}】`)
      }
    })
    .catch((error) => {
      uni.hideLoading()
      showErrorMessage(error)
    })
}
const callBack = () => {
  closeScanPopup()
  scan.value.clear()
  emit('getScanResult', code.value, result.value)
}
const change = (e) => {
  isShow.value = e.show
}
const showErrorMessage = (message) => {
  losefocus()
  comMessageRef.value.showErrorMessage(message, (res) => {
    code.value = ''
    getfocus()
  })
}
const losefocus = () => {
  scan.value.losefocus()
}
// 传递给父类
const emit = defineEmits(['getScanResult'])
defineExpose({
  openScanPopup,
  closeScanPopup,
  getfocus
})
</script>

<style></style>
