<template>
  <view>
    <u-popup mode="bottom" v-model="show">
      <view class="">
        <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="uni-flex uni-row" style="align-items: center; background-color: #fff; margin-left: 20rpx; margin-right: 20rpx; margin-top: 8rpx; border-radius: 8rpx">
            <view class="uni-center" style="width: 25%"> 来源库位 </view>
            <view class="" style="width: 75%; padding: 8rpx">
              <uni-combox :candidates="fromLocationList" v-model="fromLocationCode" placeholder="请选择库位" @confirm="fromLocationUpdate"></uni-combox>
            </view>
          </view>

          <view class="">
            <view class="">
              <win-com-scan ref="comscan" placeholder="箱标签" @getResult="onScan" :clearResult="true" headerType="HPQ,HMQ" :isShowHistory="false"> </win-com-scan>

              <view style="width: 100%">
                <view style="width: 100%" v-if="issueRecord.length > 0">
                  <view class="uni-flex uni-row space-between u-col-center">
                    <view class="" style="padding: 10rpx"> 历史记录 </view>
                    <view class="" style="padding-right: 10rpx">
                      <u-icon :name="expendIcon" size="35rpx" @click="expands()"></u-icon>
                    </view>
                  </view>
                  <u-line class="line_color" style="padding-top: 10rpx; padding-bottom: 20rpx"> </u-line>
                  <scroll-view scroll-y="true" class="scroll-view" v-if="expand && issueRecord.length > 0">
                    <u-swipe-action :show="item.show" :index="index" v-for="(item, index) in issueRecord" :key="index" :options="scanOptions" bg-color="rgba(255,255,255,0)" @click="(...event) => swipeClick($event, item)">
                      <view style="padding: 0px 10px">
                        <balance :dataContent="item" :isShowFromLocation="false" :isShowStdPack="false"></balance>
                      </view>
                    </u-swipe-action>
                  </scroll-view>
                </view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </u-popup>
    <balance-select ref="balanceSelectRef" @onSelectItem="selectBalanceItem"></balance-select>
    <com-message ref="comMessageRef" />
    <balance-qty-edit ref="balanceQtyEditRef" @confirm="confirm" :isShowStatus="true"></balance-qty-edit>
  </view>
</template>

<script setup lang="ts">
import { ref, getCurrentInstance, nextTick, onMounted } from 'vue'
import winComScan from '@/mycomponents/scan/winComScan.vue'
import balance from '@/mycomponents/balance/balance.vue'
import balanceQtyEdit from '@/mycomponents/qty/balanceQtyEdit.vue'
import balanceSelect from '@/mycomponents/balance/balanceSelect.vue'

import { getDetailOption, getDetailEditRemoveOption } from '@/common/array.js'
import { getWorkShopLineStation } from '@/api/request2.js'

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

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

const { proxy } = getCurrentInstance()
const props = defineProps({
  title: {
    type: String,
    default: ''
  }
})
const dataContent = ref({})
const jobContent = ref({})
const expendIcon = ref('arrow-down')
const show = ref(false)
const scanList = ref([])
const toLocation = ref(null)
const toLocationCode = ref('')
const fromLocationList = ref([])
const fromLocationCode = ref('')
const fromLocation = ref(null)
const issueRecord = ref([]) // 发料历史
const expand = ref(true)
const scanOptions = ref({})
const editItem = ref({})
const positionInfo = ref('请选择位置')
const positionList = ref([])
const defaultValueList = ref([])
const fromInventoryStatuses = ref('')
const packageInfo = ref({})
const label = ref({})
const toLocationCombox = ref()
const comMessageRef = ref()
const detailOptions = ref([])
const comscan = ref()
const balanceSelectRef = ref()
const balanceQtyEditRef = ref()
onMounted(() => {
  detailOptions.value = getDetailOption()
  scanOptions.value = getDetailEditRemoveOption()
})
const openScanPopup = (content, jobcontent) => {
  issueRecord.value = []
  dataContent.value = content
  jobContent.value = jobcontent
  initData()
  // this.positionInfo = this.jobContent.workShopCode + "-" + this.jobContent.subList[0].productionLineCode +
  // 	"-" + this.jobContent.subList[0].workStationCode
  setTimeout((res) => {
    show.value = true
  }, 500)
}
const closeScanPopup = () => {
  show.value = false
  emit('closeScan')
  // 清除数据，恢复默认值
  // Object.assign(this.$data, this.$options.data());
}
const initData = () => {
  fromLocationList.value = []
  if (dataContent.value != null) {
    fromInventoryStatuses.value = jobContent.value.outInventoryStatuses
    toLocation.value = dataContent.value[0]
    toLocationCode.value = dataContent.value[0].toLocationCode
    fromLocationList.value = getFromLocationList()
  }
}

const showBalanceSelect = (items) => {
  balanceSelectRef.value.openPopup(items)
}

const getFromLocationList = () => {
  const list = []
  dataContent.value.forEach((location) => {
    location.Items.forEach((item) => {
      item.Locations.forEach((f) => {
        const item = list.find((l) => l == f.fromLocationCode)
        if (item == undefined) {
          list.push(f.fromLocationCode)
        }
      })
    })
  })

  fromLocationCode.value = list[0]
  return list
}

const fromLocationUpdate = (fromlocation) => {
  const location = fromLocationList.value.find((r) => r == fromlocation)
  if (location == undefined) {
    fromLocationCode.value = ''
    showErrorMessage(`发料库位【${fromlocation}】不存在`)
  }
}

const onScan = (result) => {
  if (toLocation.value && result.package.packUnit) {
    const item = toLocation.value.Items.find((r) => r.itemCode == result.package.itemCode)
    if (result.package.packUnit !== item.packUnit) {
      comMessageRef.value.showQuestionMessage(`扫描物料包装【${result.package.packUnit}】与任务推荐包装规格【${item.packUnit}】不一致.是否要继续发料？`, (res) => {
        if (res) {
          onScanResult(result)
        }
      })
    } else {
      onScanResult(result)
    }
  }
}
const onScanResult = (result) => {
  try {
    if (fromLocationCode.value == '') {
      showErrorMessage('请选择来源库位', (res) => {
        toLocationCombox.value.onFocus()
      })
      return
    }

    label.value = result.package
    // let packageInfo = result.package;
    const { itemCode } = result.label
    const packingCode = result.label.packingNumber
    const lot = result.label.batch
    const item = toLocation.value.Items.find((r) => r.itemCode == itemCode)
    if (item == undefined) {
      showErrorMessage(`未查找到物料【${itemCode}】的发料明细`, (res) => {
        getfocus()
      })
      return
    }
    // 查找库存信息
    proxy.$modal.loading('加载中')
    getBalanceByManagementPrecision(result.label, fromLocationCode.value, fromInventoryStatuses.value, (balanceRes) => {
      if (balanceRes.success) {
        if (balanceRes.data.list.length == 0) {
          showErrorMessage(`在来源库位[${fromLocationCode.value}],未查找到该包装的库存记录`, (res) => {
            packGetFocus()
          })
        } else if (balanceRes.data.list.length == 1) {
          const balance = balanceRes.data.list[0]
          afterGetBalance(result.label, balance, packageInfo.value)
        } else {
          showBalanceSelect(balanceRes.data.list)
        }
      } else {
        showErrorMessage(balanceRes.message)
      }
      uni.hideLoading()
    })
  } catch (e) {
    showErrorMessage(e.stack)
    uni.hideLoading()
  }
}

const selectBalanceItem = (balance) => {
  afterGetBalance(label.value, balance, packageInfo.value)
}

const afterGetBalance = (label, balance, packageInfo) => {
  try {
    const { itemCode } = label
    const packingCode = label.packingNumber
    const lot = label.batch
    const item = toLocation.value.Items.find((r) => r.itemCode == itemCode)
    const fromLocation = item.Locations.find((l) => l.fromLocationCode == fromLocationCode.value)
    if (fromLocation != undefined) {
      const batch = fromLocation.Batchs.find((r) => r.batch == lot)
      if (batch != undefined) {
        if (batch.Records == undefined) {
          batch.Records = []
        }

        const record = batch.Records.find((r) => r.packingNumber == packingCode)
        if (record == undefined) {
          // 如果有推荐箱码
          if (batch.Recommends.length > 0) {
            const recommend = batch.Recommends.find((r) => r.packingNumber == packingCode)
            if (recommend != undefined) {
              addRecord(batch, label, balance, packageInfo)
            } else {
              // 允许修改箱码
              if (jobContent.value.allowModifyPackingNumber == 'TRUE') {
                addRecord(batch, label, balance, packageInfo)
              } else {
                showErrorMessage(`未查找到该箱码【${packingCode}】的明细`, (res) => {
                  getfocus()
                })
              }
            }
          } else {
            addRecord(batch, label, balance, packageInfo)
          }
          emit('afterScan')
        } else {
          showErrorMessage(`箱码【${packingCode}】已经扫描,请继续扫描下一箱`, (res) => {
            getfocus()
          })
        }
      } else if (jobContent.value.allowModifyBatch == 'TRUE') {
        showQuestionMessage(`在【${fromLocationCode.value}】库位下,未查找到批次【${lot}】的发料明细,是否要继续发料?`, (res) => {
          if (res) {
            const batch = createBatchInfo(label, balance, packageInfo)
            // 新增加的批次赋值details
            if (fromLocation.Batchs.length > 0) {
              batch.detail = fromLocation.Batchs[0].detail
            }
            fromLocation.Batchs.unshift(batch)
          }
        })
      } else {
        showErrorMessage(`未查找到批次【${lot}】的发料明细`, (res) => {
          getfocus()
        })
      }
    } else {
      showErrorMessage(`未查找到推荐库位【${fromLocationCode.value}】的发料明细`, (res) => {
        getfocus()
      })
    }
  } catch (e) {
    showErrorMessage(e.stack, (res) => {
      getfocus()
    })
  }
}

const createBatchInfo = (data, balance, packageInfo) => {
  const batch = {
    batch: data.batch,
    qty: 0,
    uom: data.uom,
    handleQty: Number(data.qty),
    Records: []
  }
  const record = creatRecord(data, balance, packageInfo)
  batch.Records.push(record)
  issueRecord.value.unshift(record)
  return batch
}
const creatRecord = (label, balance, packageInfo) => {
  balance.packQty = packageInfo.packQty
  balance.packUnit = packageInfo.packUnit
  const record = {
    scaned: true,
    itemCode: label.itemCode,
    packingNumber: label.packingNumber,
    batch: label.batch,
    qty: Number(balance.qty),
    // qty: Number(label.qty) > Number(balance.qty) ? Number(balance.qty) : Number(label.qty),
    uom: balance.uom,
    inventoryStatus: balance.inventoryStatus,
    balance,
    toLocationCode: toLocationCode.value,
    supplierCode: label.supplierCode,
    packUnit: packageInfo.packUnit,
    packQty: packageInfo.packQty
  }
  return record
}
const calcBatchHandleQty = (batch) => {
  let handleQty = 0
  batch.Records.forEach((res) => {
    handleQty = calc.add(handleQty, res.qty)
  })
  batch.handleQty = handleQty
}
const addRecord = (batch, label, balance, packageInfo) => {
  const record = creatRecord(label, balance, packageInfo)
  batch.Records.push(record)
  issueRecord.value.unshift(record)
  calcBatchHandleQty(batch)
  getfocus()
}

const getfocus = () => {
  if (!comscan.value) {
    comscan.value.getfocus()
  }
}

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

const expands = () => {
  expand.value = !expand.value
  expendIcon.value = expand.value == true ? 'arrow-down' : 'arrow-up'
}

// const swipeClick = (e, item, index) => {
//   if (e.content.text == '详情') {
//     this.detail(item)
//   } else if (e.content.text == '编辑') {
//     this.edit(item)
//   } else if (e.content.text == '移除') {
//     this.remove(item, index)
//   }
// }
const swipeClick = (params, item) => {
  const { text } = scanOptions.value[params[1]]
  if (text == '详情') {
    detail(item)
  } else if (text == '编辑') {
    edit(item)
  } else if (text == '库位') {
    showLocation(item)
  } else if (text == '移除') {
    remove(item)
  }
}
const edit = (item) => {
  editItem.value = item
  // item.balance.balanceQty = item.balance.qty;
  item.balance.balanceQty = item.balance.qty
  balanceQtyEditRef.value.openEditPopup(item.balance, item.qty)
}

const detail = (item) => {
  showItem.value = item
  receiptHint.value.openScanPopup()
}
const remove = (record, index) => {
  showQuestionMessage('确定移除扫描信息?', (res) => {
    if (res) {
      record.qty = 0
      issueRecord.value.splice(index, 1)

      const item = toLocation.value.Items.find((r) => r.itemCode == record.itemCode)

      if (item != undefined) {
        item.Locations.forEach((l) => {
          const batch = l.Batchs.find((b) => b.packingNumber == record.packingNumber && b.batch == record.batch)

          if (batch && batch.Records && batch.Records.length > 0) {
            const rIndex = batch.Records.findIndex((r) => r.packingNumber == record.packingNumber && r.batch == record.batch)
            batch.Records.splice(rIndex, 1)
          }
        })
      }
      emit('updateData', item)
    }
  })
}

const packGetFocus = () => {
  comscan.value.getfocus()
}
const packLoseFocus = () => {
  comscan.value.losefocus()
}
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 showQuestionMessage = (message, callback) => {
  setTimeout((r) => {
    packLoseFocus()
    comMessageRef.value.showQuestionMessage(message, callback)
  })
}
const confirm = (val) => {
  editItem.value.qty = Number(val)
  emit('updateData', editItem.value)
}
const cancle = () => {
  closeScanPopup()
}
// 传递给父类
const emit = defineEmits(['updateData', 'closeScan', 'afterScan'])
defineExpose({
  openScanPopup,
  closeScanPopup
})
</script>

<style lang="scss">
button {
  border: none;
}

button::after {
  border: none;
}

.scroll-view {
  overflow-y: scroll;
  height: auto;
  max-height: 300rpx;
  padding: 10rpx;
}
</style>
