<template>
  <view class="page-wraper" style="background-color: #fff">
    <view class="">
      <com-blank-view @goScan="openFg" v-if="!dataContent"></com-blank-view>
    </view>

    <view class="page-wraper" v-if="dataContent">
      <view class="" style="font-size: 35rpx; padding: 10rpx; padding-left: 15rpx"> 生产计划：{{ dataContent.number }} </view>
      <view class="split_line"></view>
      <view class="">
        <item-qty :dataContent="dataContent" :isShowBalanceQty="false"></item-qty>
      </view>
      <view class="split_line"></view>
      <view class="" style="margin-left: 20rpx">
        <batch :batch="dataContent.batch"></batch>
        <view class="card_view">
          <text style="color: #ffa500; padding: 5px; font-size: 35rpx">包装规格</text>
          <text class="card_content">{{ dataContent.packUnit }}</text>
        </view>
      </view>
      <view class="split_line"></view>
      <view class="page-main">
        <uni-swipe-action>
          <view class="scan_view" v-for="(item, index) in dataContent.subList" :key="index">
            <uni-swipe-action-item :right-options="options" @click="swipeClick($event, item, index)">
              <view class="" style="margin-left: 40rpx; padding-top: 10rpx; padding-bottom: 10rpx">
                <text style="font-size: 35rpx; color: #b66463">唯一码</text>
                <text style="font-size: 35rpx"> {{ item.code }}</text>
              </view>
            </uni-swipe-action-item>
          </view>
        </uni-swipe-action>
      </view>
      <view class="page-footer">
        <view class="uni-flex u-col-center space-between padding_10" style="background-color: ghostwhite; width: 100%">
          <view class="">
            <view class="uni-flex uni-row"> </view>
          </view>
          <view class="uni-flex uni-row">
            <button class="btn_single_clear" style="margin-right: 50rpx" hover-class="btn_clear_after" @click="clear">清空</button>
            <button class="btn_single_commit" hover-class="btn_commit_after" @click="commit">提交</button>
          </view>
        </view>
      </view>
    </view>
    <comReceiptPopup ref="comReceiptPopupRef" @confirm="requestConfirm"></comReceiptPopup>
    <win-scan-button v-if="dataContent" @goScan="openScanPopup"></win-scan-button>
    <win-scan-fg-label ref="scanPopup" @getResult="getScanResult" title="制品标签"></win-scan-fg-label>
    <com-message ref="comMessageRef" />
  </view>
</template>

<script setup lang="ts">
import { ref, getCurrentInstance, nextTick } from 'vue'
import { onShow, onLoad, onNavigationBarButtonTap } from '@dcloudio/uni-app'
import { isCheckMesCode, planReceiptSubmit } from '@/api/request2.js'
import { getRemoveOption } from '@/common/basic.js'
import winScanButton from '@/mycomponents/scan/winScanButton.vue'
import comBlankView from '@/mycomponents/common/comBlankView.vue'
import comReceiptPopup from '@/pages/fg/coms/comReceiptPopup.vue'
import itemQty from '@/mycomponents/item/itemQty.vue'
import batch from '@/mycomponents/balance/batch.vue'
import pack from '@/mycomponents/balance/pack.vue'
import winScanFgLabel from '@/mycomponents/scan/winScanFgLabel.vue'

const dataContent = ref(null)
const options = ref([])
const scanPopup = ref()

const comReceiptPopupRef = ref()
onLoad(() => {
  options.value = getRemoveOption()
  openFg()
})

const openFg = () => {
  setTimeout((res) => {
    if (comReceiptPopupRef.value) {
      comReceiptPopupRef.value.openRequestPopup()
    }
  }, 600)
}
const requestConfirm = (result) => {
  dataContent.value = {
    itemCode: result.itemCode,
    number: result.number, // 计划单号
    handleQty: 0,
    qty: result.planQty,
    batch: result.batch,
    packUnit: '3031',
    uom: result.uom,
    subList: [
      {
        code: '1234'
      },
      {
        code: '12345'
      }
    ]
  }
}
const swipeClick = (e, dataContent, index) => {
  if (e.content.text == '移除') {
    dataContent.value.subList.splice(index, 1)
  }
}
const commit = () => {
  planReceiptSubmit(params)
    .then((res) => {
      uni.hideLoading()
      if (res.data) {
        showCommitSuccessMessage(res.data)
      } else {
        showErrorMessage(`提交失败[${res.msg}]`)
      }
    })
    .catch((error) => {
      uni.hideLoading()
      showErrorMessage(error)
    })
}

const openScanPopup = () => {
  const { itemCode } = dataContent.value
  // itemCode = "015553147"
  console.log('物料', itemCode)
  scanPopup.value.openScanPopup(itemCode)
}
const getScanResult = (result) => {
  // TODO缺少零件号的对应关系
  // 查询mes码是否完工
  isCheckMesCode(result.content)
    .then((res) => {
      if (res.data) {
        if (dataContent.value) {
          const list = dataContent.value.subList // 应该按零件号查找

          const itemIndex = list.findIndex(
            (r) =>
              r.itemCode == result.itemCode &&
              // r.productDate == result.productDate &&
              r.order == result.order
          )
          // 不存在
          if (itemIndex == -1) {
            list.push(result)
            calcFgQty()
          } else {
            showErrorMessage(`标签【${result.content}】已经扫描，是否移除`, (confirm) => {
              list.splice(itemIndex, 1)
              calcFgQty()
            })
          }
          setTimeout((r) => {
            scanPopupGetFocus()
          }, 500)
        }
      }
    })
    .catch((error) => {
      showErrorMessage(error)
    })
}
const calcFgQty = () => {
  dataContent.value.handleQty = dataContent.value.subList.length
  if (dataContent.value.handleQty == dataContent.value.qty) {
    commit()
  }
}
const scanPopupGetFocus = () => {
  if (scanPopup.value) {
    scanPopup.value.getfocus()
  }
}
const scanPopupLoseFocus = () => {
  if (scanPopup.value) {
    scanPopup.value.losefocus()
  }
}
const showCommitSuccessMessage = (hint) => {
  comMessageRef.value.showSuccessMessage(hint, (res) => {
    dataContent.value = null
    openFg()
  })
}
</script>

<style></style>
