<template>
  <!-- <page-meta root-font-size="18px"></page-meta> -->
  <view class="">
    <com-blank-view @goScan="openScanPopup" v-if="locationCode == ''"></com-blank-view>

    <my-paging v-show="locationCode != ''" ref="paging" v-model="dataList" @query="queryList">
      <!-- 需要固定在顶部不滚动的view放在slot="top"的view中，如果需要跟着滚动，则不要设置slot="top" -->
      <template #top>
        <view v-if="locationCode != ''">
          <location-info :locationDetail="locationDetail" v-if="locationCode"></location-info>
          <z-tabs :list="tabList" @change="tabChange" />
        </view>
      </template>
      <view class="" style="padding-bottom: 50rpx">
        <view v-if="tabIndex == 0" v-for="(item, index) in dataList">
          <comLocationDetailCard :isShowPack="false" :dataContent="item" style="margin: 20rpx"> </comLocationDetailCard>
        </view>
        <view v-if="tabIndex == 1" v-for="(item, index) in dataList">
          <comLocationDetailCard :dataContent="item" style="margin: 20rpx"></comLocationDetailCard>
        </view>
      </view>
    </my-paging>

    <win-scan-button @goScan="openScanPopup" v-if="locationCode != ''"></win-scan-button>
    <winScanLocation ref="scanPopup" title="库位代码" @getLocation="getScanCode"></winScanLocation>

    <show-modal ref="modal"></show-modal>
    <com-message ref="comMessageRef" />
  </view>
</template>

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

import { maxPageSize, goHome } from '@/common/basic.js'

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

import locationInfo from '@/mycomponents/location/locationInfo.vue'
import comBlankView from '@/mycomponents/common/comBlankView.vue'
import winScanButton from '@/mycomponents/scan/winScanButton.vue'
import comLocationDetailCard from '@/pages/query/coms/comLocationDetailCard.vue'
import winScanLocation from '@/mycomponents/scan/winScanLocation.vue'

const { proxy } = getCurrentInstance()
const locationDetail = ref({})
const locationCode = ref('')
const summarys = ref([])
const balances = ref([])
const dataIn = ref([])
const dataOut = ref([])
// 每页数据量
const pageSize = ref(1000)
// 当前页
const pageCurrent = ref(1)
// 数据总量
const dataList = ref([])
const tabList = ref(['汇总', '明细'])
const tabIndex = ref(0)
const scanPopup = ref()
const modal = ref()
const paging = ref()
const comMessageRef = ref()
const itemCode = ref()
const summarysList = ref()
const detailList = ref()
onNavigationBarButtonTap((e) => {
  if (e.index === 0) {
    goHome()
  }
})
onMounted(() => {
  openScanPopup()
})
const openScanPopup = (val) => {
  scanPopup.value.openScanPopup()
}
const closeScanPopup = () => {
  scanPopup.value.closeScanPopup()
}

const getScanCode = (location, code) => {
  // locationCode.value = ''
  summarys.value = []
  balances.value = []
  locationCode.value = code
  locationDetail.value = location
  console.log(22, locationDetail.value)
  tabChange(0)
  // this.getContentByTab(this.tabIndex);
}

const getSummary = (pageNo, pageSize) => {
  proxy.$modal.loading('加载中­....')
  const params = {
    itemCode: itemCode.value,
    pageNo,
    pageSize
  }
  getBalanceByLocationcodeGroup(params)
    .then((res) => {
      uni.hideLoading()
      if (res.data.list.length > 0) {
        summarysList.value = setShowList(res.data.list)
        paging.value.complete(summarysList.value)
      } else {
        paging.value.complete(false)
        showMessage(`未查找到库位【${locationCode.value}】`)
      }
    })
    .catch((error) => {
      paging.value.complete(false)
      uni.hideLoading()
      showMessage(error)
    })
}

const getDetailList = (pageNo, pageSize) => {
  proxy.$modal.loading('加载中­....')

  const params = {
    locationCode: locationCode.value,
    pageNo,
    pageSize
  }
  getBalanceByLocationcode(params)
    .then((res) => {
      uni.hideLoading()
      if (res.data.list.length > 0) {
        detailList.value = setShowList(res.data.list)
        paging.value.complete(detailList.value)
      } else {
        paging.value.complete(false)
        showMessage(`未查找到库位w【${locationCode.value}】`)
      }
    })
    .catch((error) => {
      paging.value.complete(false)
      uni.hideLoading()
      showMessage(error)
    })
}

const setShowList = (list) => {
  const resultlist = []
  list.forEach((res) => {
    const temp = resultlist.find((res1) => res1.itemCode == res.itemCode)
    if (temp == undefined) {
      const data = {
        itemCode: res.itemCode,
        qty: Number(res.qty),
        uom: res.uom,
        list: []
      }
      var item = {
        packingNumber: res.packingNumber,
        batch: res.batch,
        uom: res.uom,
        qty: Number(res.qty),
        inventoryStatus: res.inventoryStatus
      }
      data.list.push(item)
      resultlist.push(data)
    } else {
      temp.qty = calc.add(temp.qty, res.qty)
      var item = {
        packingNumber: res.packingNumber,
        batch: res.batch,
        uom: res.uom,
        qty: Number(res.qty),
        inventoryStatus: res.inventoryStatus
      }
      temp.list.push(item)
    }
  })
  return resultlist
}
const ontabtap = (e) => {
  const index = e.target.dataset.current || e.currentTarget.dataset.current
  tabIndex.value = index
  getContentByTab(tabIndex.value)
}

const getContentByTab = (index, pageNo, pageSize) => {
  if (index === 0) {
    getSummary(pageNo, pageSize)
  } else if (index === 1) {
    getDetailList(pageNo, pageSize)
  } else if (index === 2) {
    dataIn.value = balances.value
  } else if (index == 3) {
    dataOut.value = balances.value
  }
}

const showMessage = (message) => {
  comMessageRef.value.showErrorMessage(message, (res) => {
    if (res) {
      // afterCloseMessage()
    }
  })
}
const afterCloseMessage = () => {
  if (scanPopup.value != undefined) {
    scanPopup.value.getfocus()
  }
}
const tabChange = (index) => {
  tabIndex.value = index
  paging.value.reload(true)
}
const queryList = (pageNo, pageSize) => {
  if (locationCode.value != '') {
    getContentByTab(tabIndex.value, pageNo, pageSize)
  }
}
</script>

<style scoped lang="scss">
page {
  height: 100%;
}
</style>
