<template>
  <!-- <page-meta root-font-size="18px"></page-meta> -->
  <view class="uni-flex" style="flex-direction: column">
    <view class="" style="width: 100%; position: fixed; top: 0; right: 0">
      <com-blank-view @goScan="openScanPopup" v-if="locationCode == ''"></com-blank-view>
      <location-info :locationDetail="locationDetail" v-if="locationCode"></location-info>
      <z-tabs v-if="locationCode" :list="tabList" @change="tabChange" />
    </view>
    <view style="padding-top: 200rpx; width: 100%">
      <view v-if="totalCount > 0" style="margin: 10rpx; font-size: 35rpx; font-weight: bold">总数 : {{ totalCount }} </view>
      <view v-for="(item, index) in dataList" style="width: 100%">
        <view class="uni-flex uni-row" style="align-items: center; background-color: #fff; border-radius: 10rpx; margin: 10rpx">
          <view class="" style="font-size: 35rpx"> ({{ index + 1 }}) </view>
          <comLocationDetailCard :isShowPack="false" :dataContent="item" style="margin: 10rpx"> </comLocationDetailCard>
        </view>
      </view>
      <uni-load-more :status="loadingType" v-if="dataList.length > 0" />
    </view>

    <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, getExpectInByLocationCode, getExpectOutByLocationCode } 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 pageNo = ref(1)
// 每页数据量
const pageSize = ref(10)
// 当前页
const pageCurrent = ref(1)
// 数据总量
const dataList = ref([])
const tabList = ref(['汇总', '明细', '预计入', '预计出'])
const tabIndex = ref(0)
const loadingType = ref('nomore')
const totalCount = 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()
})
onPullDownRefresh(() => {
  getContentByTab(tabIndex.value, pageNo.value, pageSize.value, 'refresh')
})

onReachBottom(() => {
  console.log('底部')
  // 避免多次触发
  if (loadingType.value == 'loading' || loadingType.value == 'nomore') {
    return
  }
  getContentByTab(tabIndex.value, pageNo.value, pageSize.value, 'more')
})
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, type) => {
  proxy.$modal.loading('加载中­....')
  loadingType.value = 'loading'
  if (type === 'refresh') {
    pageNo.value = 1
    dataList.value = []
  }

  const params = {
    locationCode: locationCode.value,
    pageNo: pageNo.value,
    pageSize
  }

  getBalanceByLocationcodeGroup(params)
    .then((res) => {
      uni.hideLoading()
      if (type === 'refresh') {
        uni.stopPullDownRefresh()
      }
      const { list } = res.data
      totalCount.value = res.data.total
      loadingType.value = 'loadmore'
      if (list == null || list.length == 0) {
        loadingType.value = 'nomore'
        return
      }
      dataList.value = type === 'refresh' ? list : dataList.value.concat(list)
      pageNo.value++
    })
    .catch((error) => {
      paging.value.complete(false)
      uni.hideLoading()
      showMessage(error)
    })
}

const getDetailList = (pageNo, pageSize, type) => {
  proxy.$modal.loading('加载中­....')
  loadingType.value = 'loading'
  if (type === 'refresh') {
    pageNo.value = 1
    dataList.value = []
  }

  const params = {
    locationCode: locationCode.value,
    pageNo: pageNo.value,
    pageSize
  }
  getBalanceByLocationcode(params)
    .then((res) => {
      uni.hideLoading()
      if (type === 'refresh') {
        uni.stopPullDownRefresh()
      }
      const { list } = res.data
      totalCount.value = res.data.total
      loadingType.value = 'loadmore'
      if (list == null || list.length == 0) {
        loadingType.value = 'nomore'
        return
      }
      dataList.value = type === 'refresh' ? list : dataList.value.concat(list)
      pageNo.value++
    })
    .catch((error) => {
      paging.value.complete(false)
      uni.hideLoading()
      showMessage(error)
    })
}
const getExpectin = (pageNo, pageSize, type) => {
  uni.showLoading({
    title: '加载中...',
    mask: true
  })

  if (type === 'refresh') {
    pageNo.value = 1
    dataList.value = []
  }

  const params = {
    pageNo: pageNo.value,
    pageSize,
    filters: [
      {
        column: 'locationCode',
        action: '==',
        value: locationCode.value
      }
    ]
  }

  getExpectInByLocationCode(params)
    .then((res) => {
      uni.hideLoading()
      if (type === 'refresh') {
        uni.stopPullDownRefresh()
      }
      const { list } = res.data
      totalCount.value = res.data.total
      loadingType.value = 'loadmore'
      if (list == null || list.length == 0) {
        loadingType.value = 'nomore'
        return
      }
      dataList.value = type === 'refresh' ? list : dataList.value.concat(list)
      pageNo.value++
    })
    .catch((error) => {
      uni.hideLoading()
      showMessage(error)
    })
}
const getExpectOut = (pageNo, pageSize, type) => {
  uni.showLoading({
    title: '加载中...',
    mask: true
  })

  if (type === 'refresh') {
    pageNo.value = 1
    dataList.value = []
  }

  const params = {
    pageNo: pageNo.value,
    pageSize,
    filters: [
      {
        column: 'locationCode',
        action: '==',
        value: locationCode.value
      }
    ]
  }

  getExpectOutByLocationCode(params)
    .then((res) => {
      uni.hideLoading()
      if (type === 'refresh') {
        uni.stopPullDownRefresh()
      }
      const { list } = res.data
      totalCount.value = res.data.total
      loadingType.value = 'loadmore'
      if (list == null || list.length == 0) {
        loadingType.value = 'nomore'
        return
      }
      dataList.value = type === 'refresh' ? list : dataList.value.concat(list)
      pageNo.value++
    })
    .catch((error) => {
      uni.hideLoading()
      showMessage(error)
    })
}

const getContentByTab = (index, pageNo, pageSize, type) => {
  if (index === 0) {
    getSummary(pageNo, pageSize, type)
  } else if (index === 1) {
    getDetailList(pageNo, pageSize, type)
  } else if (index === 2) {
    getExpectin(pageNo, pageSize, type)
  } else if (index == 3) {
    getExpectin(pageNo, pageSize, type)
  }
}

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
  getContentByTab(index, pageNo.value, pageSize.value, 'refresh')
}
</script>

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