<template>
  <view class="page-wraper">
    <view class="page-main">
      <scroll-view scroll-y="true" class="page-main-scroll">
        <view class="" style="padding: 10rpx 30rpx; font-size: 32rpx">
          <!-- padding-left: 20rpx;padding-right: 20rpx; padding-top: 10rpx;padding-bottom: 10rpx -->
          <view style="padding: 5rpx"> 发货单号:{{ dataContent.asnNumber }} </view>
          <view style="padding: 5rpx"> 供应商代码:{{ dataContent.supplierCode }} </view>
        </view>
        <u-line />
        <view class="detail-list" v-for="(item, index) in detailSource" :key="item.id">
          <view class="">
            <com-record-detail-card :dataContent="item" :index="index" @remove="updateData" @updateData="updateData" @openDetail="openDetail"> </com-record-detail-card>
          </view>
        </view>
      </scroll-view>
    </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>
			</view>
		</view> -->

    <!-- <win-scan-pack ref="scanPopup" @getResult='getScanResult'></win-scan-pack> -->
    <recordDetailInfoPopup ref="recordDetailPopup"></recordDetailInfoPopup>
    <com-message ref="comMessageRef" />
  </view>
</template>

<script setup lang="ts">
import { ref, getCurrentInstance, onMounted, nextTick, watch } from 'vue'
import { onLoad, onNavigationBarButtonTap, onReady, onReachBottom, onPullDownRefresh, onShow, onBackPress } from '@dcloudio/uni-app'
import { getSupplierDeliverRecordDetail } from '@/api/request2.js'

import { goHome, getCurrDateTime, getPackingNumberAndBatch, getDirectoryItemArray } from '@/common/basic.js'

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

import { getDataSource, createRecordInfo, calcHandleQty } from '@/common/detail.js'

import winScanPack from '@/mycomponents/scan/winScanPack.vue'
import comRecordDetailCard from '@/pages/supplierDeliver/coms/comRecordDetailCard.vue'
import recordDetailInfoPopup from '@/pages/supplierDeliver/coms/recordDetailInfoPopup.vue'

const { proxy } = getCurrentInstance()
const id = ref('')
const receiptJob = ref({})
const received = ref(false)
const scanCount = ref(0)
const dataContent = ref({}) // 任务内容
const subList = ref([]) // 接口返回的任务subList
const detailSource = ref([]) // 绑定在页面上的数据源
const toLocationCode = ref('')
const toLocationInfo = ref({})
const businessTypeInfo = ref({})
const managementList = ref([])
const recordDetailPopup = ref()
onLoad((option) => {
  uni.setNavigationBarTitle({
    title: `${option.title}详情`
  })
  id.value = option.id
  getDetail()
})
onNavigationBarButtonTap((e) => {
  if (e.index === 0) {
    goHome()
  }
})
const getDetail = () => {
  proxy.$modal.loading('加载中­....')
  getSupplierDeliverRecordDetail(id.value)
    .then((res) => {
      uni.hideLoading()
      if (res.data == null) {
        showMessage('未获取到详情')
      } else if (res.data.subList.length > 0) {
        dataContent.value = res.data
        subList.value = res.data.subList
        subList.value.forEach((res) => {
          res.packingNumber = res.toPackingNumber
          res.batch = res.fromBatch
        })
        detailSource.value = getDataSource(subList.value)
      } else {
        showMessage('列表数据为0')
      }
    })
    .catch((error) => {
      uni.hideLoading()
      showErrorMessage(error)
    })
}

const getScanResult = (result) => {
  try {
    const { packingNumber } = result.label
    const { batch } = result.label
    const { qty } = result.label
    const { itemCode } = result.label
    const detail = detailSource.value.find((r) => r.itemCode == itemCode)
    if (detail == undefined) {
      showMessage(`物料号【${itemCode}】不在列表中`)
    } else {
      const itemDetail = detail.subList.find((r) => r.packingNumber == packingNumber && r.batch == batch)
      if (itemDetail == undefined) {
        showMessage(`箱码【${packingNumber}】、批次【${batch}】不在列表中`)
      } else if (itemDetail.scaned) {
        showMessage(`箱码【${packingNumber}】已经扫描`)
      } else {
        itemDetail.scaned = true
        calcHandleQty(detailSource.value)
      }
    }
  } catch (e) {
    showErrorMessage(e.message)
  }
}

const getScanCount = (closeScan) => {
  const items = subList.value.filter((r) => {
    if (r.scaned) {
      return r
    }
  })
  const scanCount = items != null ? items.length : 0
  return scanCount
}

const updateData = () => {
  calcHandleQty(detailSource.value)
}

const showMessage = (message) => {
  comMessageRef.value.showMessage(message, (res) => {
    if (res) {
    }
  })
}

const showErrorMessage = (message) => {
  setTimeout((r) => {
    comMessageRef.value.showErrorMessage(message, (res) => {
      if (res) {
      }
    })
  })
}

const openScanPopup = () => {
  scanPopup.value.openScanPopup()
}

const closeScanPopup = () => {
  scanPopup.value.closeScanPopup()
}

const openDetail = (item) => {
  recordDetailPopup.value.openPopup(item)
}
</script>

<style scoped lang="scss">
page {
  width: 100%;
  height: 100%;
  background-color: #fff;
}

.page-wraper {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
}

.page-main {
  flex: 1;
  position: relative;
}

.page-main-scroll {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
}

.page-main-list {
  /*  height: 80rpx;
	    line-height: 80rpx; */
  text-align: center;
  background: #e0e0e0;
}
</style>
