<template>
  <view class="page-wraper">
    <view class="">
      <com-blank-view @goScan="goScan()"></com-blank-view>
    </view>
  </view>
  <win-scan-pack ref="scanPopup" @getResult="getScanResult" headerType="HCMQ" title="叫料标签" :isShowHistory="false"></win-scan-pack>
  <com-message ref="comMessageRef" />
</template>

<script setup lang="ts">
import { ref, getCurrentInstance, nextTick } from 'vue'
import { onLoad, onNavigationBarButtonTap, onPullDownRefresh } from '@dcloudio/uni-app'
import { issueRequestSubmit } from '@/api/request2.js'

import { calc } from '@/common/calc.js'

import { getBusinessType } from '@/common/record.js'

import { goHome, updateTitle, getRemoveOption, navigateBack, getCurrDateOneMonthsTimes } from '@/common/basic.js'

import { getDataSource } from '@/pages/issue/js/issue.js'

import comBlankView from '@/mycomponents/common/comBlankView.vue'
import jobDetailPopup from '@/mycomponents/job/jobDetailPopup.vue'
import comScanIssuePack from '@/pages/issue/coms/comScanIssuePack.vue'
import comIssueRequestCreator from '@/pages/issue/coms/comIssueRequestCreator.vue'
import winScanPack from '@/mycomponents/scan/winScanPack.vue'

const { proxy } = getCurrentInstance()
const jobContent = ref({}) // 任务内容
const subList = ref([]) // 接口返回的任务subList
const detailSource = ref({
  subList: []
}) // 绑定在页面上的数据源
const detailOptions = ref([])
const scanOptions = ref([])
const requestList = ref([]) // 需求信息
const itemCodeTypeList = ref([])
const useOnTheWay = ref('FALSE')
const comIssueRequestPopupRef = ref()
const comMessageRef = ref()
const fromInventoryStatuses = ref('')
const toInventoryStatuses = ref('')
const scanPopup = ref()
onLoad((option) => {
  const typeCode = 'Issue'
  getBusinessType(typeCode, (res) => {
    nextTick(() => {
      if (res.success) {
        itemCodeTypeList.value = res.itemCodeTypeList
        useOnTheWay.value = res.useOnTheWay
        fromInventoryStatuses.value = res.fromInventoryStatuses
        toInventoryStatuses.value = res.toInventoryStatuses
        goScan(true)
      } else {
        showErrorMessage(res.message)
      }
    })
  })
})
const goScan = () => {
  scanPopup.value.openScanPopup()
}
const getScanResult = (data) => {
  detailSource.value = {
    subList: []
  }
  const labelInfo = data.label

  var data = {
    productionLineCode: labelInfo.productionLineCode,
    workStationCode: labelInfo.workStationCode,
    itemCode: labelInfo.itemCode,
    location: labelInfo.location,
    qty: labelInfo.qty,
    uom: labelInfo.uom,
    packUnit: labelInfo.packUnit
  }
  detailSource.value.subList.push(data)
  detailSource.value.warehouseCode = labelInfo.warehouseCode
  detailSource.value.workshopCode = labelInfo.workshopCode
  submit()
}

const setParams = () => {
  detailSource.value.dueTime = getCurrDateOneMonthsTimes()
  return detailSource.value
}
const submit = () => {
  proxy.$modal.loading('提交中....')
  const params = setParams()
  issueRequestSubmit(params)
    .then((res) => {
      uni.hideLoading()
      if (res.data) {
        showCommitSuccessMessage(`提交成功<br>生成发料申请[${res.data}]`)
      } else {
        showErrorMessage(`提交失败[${res.msg}]`)
      }
    })
    .catch((error) => {
      uni.hideLoading()
      showErrorMessage(error)
    })
}
const showCommitSuccessMessage = (hint) => {
  comMessageRef.value.showSuccessMessage(hint, (res) => {
    closeScanPopup()
    goScan()
  })
}
const closeScanPopup = () => {
  scanPopup.value.closeScanPopup()
}
const showErrorMessage = (message) => {
  comMessageRef.value.showErrorMessage(message, (res) => {
    if (res) {
      scanPopupGetFocus()
    }
  })
}
const scanPopupGetFocus = () => {
  if (scanPopup.value != undefined) {
    scanPopup.value.getfocus()
  }
}
</script>

<style></style>
