<template>
  <view>
    <u-popup v-model="show" mode="bottom" :mask-close-able="false">
      <!-- <uni-popup ref="popup" @change="change" @maskClick="closeScanPopup()"> -->
      <view class="popup_box">
        <view class="pop_title uni-flex space-between">
          <view class="" style="font-size: 35rpx"> 校验客户标签 </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="" style="position: relative">
            <win-com-scan ref="comscanFgLabel" placeholder="WMS制品标签" @getResult="getScanFgResult" :isShowHistory="false" :clearResult="false" :headerType="headerType"></win-com-scan>
            <view class="uni-flex" style="position: absolute; color: #3c9cff; font-weight: bold; font-size: 15px; left: 10px; bottom: 10px; z-index: 999">
              {{ itemCode }}
            </view>
          </view>
          <view class="">
            <win-com-scan-customer-label ref="comscanCustomerLabel" placeholder="客户标签" @getResult="getScanCustomerResult" :isShowHistory="false" :clearResult="false" :headerType="headerType"></win-com-scan-customer-label>
          </view>
        </view>
      </view>
    </u-popup>
  </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 winComScanCustomerLabel from '@/mycomponents/scan/winComScanCustomerLabel.vue'

import balanceSelect from '@/mycomponents/balance/balanceSelect.vue'
import { getBalanceByManagementPrecision } from '@/common/balance.js'

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

import { getListLocationAreaTypeDesc, checkDirectoryItemExist, getDirectoryItemArray, getLocationAreaTypeName, getInventoryStatusDesc } from '@/common/directory.js'

const props = defineProps({
  title: {
    type: String,
    default: '箱标签'
  },
  headerType: {
    type: String,
    default: 'HPQ,HMQ'
  }
})
const show = ref(false)
const scanResult = ref({})
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 fromLocationAreaTypeList = ref([])
const locationOnFocus = ref(false)
const businessType = ref({})
const inventoryStatus = ref([])
const managementPrecision = ref('')
const fromInventoryStatuses = ref('')
const isCheck = ref(false)
const itemCode = ref('')
const customerItem = ref(null)
const customerCode = ref('') // 客户代码
const packingNumber = ref('') // 箱码
const comscanFgLabel = ref()
const comscanCustomerLabel = ref()
const comMessageRef = ref()
const openScanPopupForType = (customerCodeParams) => {
  customerCode.value = customerCodeParams
  if (fromLocationCode.value != '') {
    packGetFocus()
  } else {
    locationGetFocus()
  }
  fromInventoryStatuses.value = getDirectoryItemArray(businessType.value.outInventoryStatuses)
  inventoryStatus.value = getDirectoryItemArray(businessType.value.outInventoryStatuses) // 出库库存状态
  fromLocationAreaTypeList.value = getDirectoryItemArray(businessType.value.fromLocationAreaTypeList) // 出库库区
  setTimeout((res) => {
    show.value = true
  }, 500)
}

// 在任务中扫描
const openScanPopupForJob = (fromLocationCode, fromLocationList, jobContent) => {
  fromLocationCode.value = fromLocationCode
  fromLocationList.value = fromLocationList
  if (fromLocationCode != '') {
    packGetFocus()
  } else if (fromLocationList.value.length == 0) {
    locationGetFocus()
  } else {
    fromLocationCode.value = fromLocationList.value[0]
  }
  setTimeout((res) => {
    show.value = true
  }, 500)
  fromInventoryStatuses.value = getDirectoryItemArray(jobContent.outInventoryStatuses)
  inventoryStatus.value = getDirectoryItemArray(jobContent.outInventoryStatuses) // 出库库存状态; //出库库存状态
  fromLocationAreaTypeList.value = getDirectoryItemArray(jobContent.fromAreaTypes) // 出库库存状态
}

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

const getScanFgResult = (result) => {
  console.log('getScanFgResult', result)
  itemCode.value = result.label.itemCode
  packingNumber.value = result.package.number // 箱码

  const filters = []
  filters.push({
    column: 'customerCode',
    action: '==',
    value: customerCode.value
  })
  filters.push({
    column: 'itemCode',
    action: '==',
    value: itemCode.value
  })

  const params = {
    filters,
    pageNo: 1,
    pageSize: 10
  }

  getCustomerItemList(params)
    .then((res) => {
      console.log('getCustomerItemList', res)

      if (res.data != null && res.data.list.length > 0) {
        customerItem.value = res.data.list[0]
      } else {
        showErrorMessage('未查找到客户物料代码')
      }
    })
    .catch((error) => {
      showErrorMessage(error)
    })

  customerGetFocus()
}

const getScanCustomerResult = (result) => {
  console.log('getScanCustomerResult', result)
  if (customerItem.value != null) {
    // 客户物料代码
    if (customerItem.value.customerItemCode == result) {
      // 通过
      emit('checkResult', {
        packingNumber: packingNumber.value, // 箱码
        customerItemCode: customerItem.value.customerItemCode, // 客户物料代码
        customerItemCode_reality: result, // 实际校验物料
        itemCode: itemCode.value, // 物料代码
        pass: true
      })
    } else {
      // 不通过
      emit('checkResult', {
        packingNumber: packingNumber.value, // 箱码
        customerItemCode: customerItem.value.customerItemCode, // 客户物料代码
        customerItemCode_reality: result, // 实际校验物料
        itemCode: itemCode.value, // 物料代码
        pass: false
      })
    }
  }
}

const packGetFocus = () => {
  if (comscanFgLabel.value != undefined) {
    comscanFgLabel.value.getfocus()
  }
}

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

const customerGetFocus = () => {
  if (comscanCustomerLabel.value != undefined) {
    comscanCustomerLabel.value.getfocus()
  }
}

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

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 emit = defineEmits(['close', 'checkResult'])
defineExpose({
  closeScanPopup,
  openScanPopupForType,
  packGetFocus
})
</script>

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