<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>
		<comMessage ref="comMessage"></comMessage>

	</view>
</template>

<script>
	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'


	export default {
		name: 'location',
		components: {
			locationInfo,
			comBlankView,
			winScanButton,
			comLocationDetailCard,
			winScanLocation
		},
		data() {
			return {
				locationDetail: {},
				locationCode: '',
				summarys: [],
				balances: [],
				dataIn: [],
				dataOut: [],
				pageNo: 1,
				// 每页数据量
				pageSize: 10,
				// 当前页
				pageCurrent: 1,
				// 数据总量

				dataList: [],
				tabList: ['汇总', '明细', '预计入', '预计出'],
				tabIndex: 0,
				loadingType: "nomore",
				totalCount:0
			};
		},
		onLoad() {},
		onNavigationBarButtonTap(e) {
			if (e.index === 0) {
				goHome();
			}
		},
		filters: {

		},
		mounted() {
			this.openScanPopup()
		},
		
		onPullDownRefresh() {
			this.getContentByTab(this.tabIndex, this.pageNo, this.pageSize, "refresh");
		},

		onReachBottom() {
			console.log("底部")
			//避免多次触发
			if (this.loadingType == 'loading' || this.loadingType == 'nomore') {
				return;
			}
			this.getContentByTab(this.tabIndex, this.pageNo, this.pageSize, "more");

		},

		methods: {
			openScanPopup() {
				this.$refs.scanPopup.openScanPopup();
			},
			closeScanPopup() {
				this.$refs.scanPopup.closeScanPopup();
			},
			getScanCode(location, code) {
				this.locationCode = '';
				this.summarys = [];
				this.balances = [];
				this.locationCode = code;
				this.locationDetail = location
				this.tabChange(0)
				// this.getContentByTab(this.tabIndex);
			},

			getSummary(pageNo, pageSize, type) {
				let that = this;
				uni.showLoading({
					title: "加载中...",
					mask: true
				});
				this.loadingType = "loading";
				if (type === "refresh") {
					this.pageNo = 1;
					this.dataList = [];
				}

				var params = {
					locationCode: this.locationCode,
					pageNo: this.pageNo,
					pageSize: pageSize
				}

				getBalanceByLocationcodeGroup(params).then(res => {
					uni.hideLoading();
					if (type === "refresh") {
						uni.stopPullDownRefresh();
					}
					var list = res.data.list;
					this.totalCount = res.data.total
					this.loadingType = "loadmore";
					if (list == null || list.length == 0) {
						this.loadingType = "nomore";
						return;
					}
					this.dataList = type === "refresh" ? list : this.dataList.concat(list);
					this.pageNo++;

				}).catch(error => {
					uni.hideLoading();
					this.showMessage(error);
				})

			},

			getDetailList(pageNo, pageSize, type) {
				let that = this;
				uni.showLoading({
					title: "加载中...",
					mask: true
				});

				this.loadingType = "loading";
				if (type === "refresh") {
					this.pageNo = 1;
					this.dataList = [];
				}

				var params = {
					locationCode: this.locationCode,
					pageNo: this.pageNo,
					pageSize: pageSize
				}
				getBalanceByLocationcode(params).then(res => {
					uni.hideLoading();
					if (type === "refresh") {
						uni.stopPullDownRefresh();
					}
					var list = res.data.list;
					this.totalCount = res.data.total
					this.loadingType = "loadmore";
					if (list == null || list.length == 0) {
						this.loadingType = "nomore";
						return;
					}
					this.dataList = type === "refresh" ? list : this.dataList.concat(list);
					this.pageNo++;

				}).catch(error => {
					uni.hideLoading();
					this.showMessage(error);
				})
			},

			getExpectin(pageNo, pageSize, type) {
				let that = this;
				uni.showLoading({
					title: "加载中...",
					mask: true
				});

				if (type === "refresh") {
					this.pageNo = 1;
					this.dataList = [];
				}

				var params = {
					pageNo: this.pageNo,
					pageSize: pageSize,
					filters: [{
						column: "locationCode",
						action: "==",
						value: this.locationCode

					}]
				}

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

			},

			getExpectOut(pageNo, pageSize, type) {
				let that = this;
				uni.showLoading({
					title: "加载中...",
					mask: true
				});

				if (type === "refresh") {
					this.pageNo = 1;
					this.dataList = [];
				}

				var params = {
					pageNo: this.pageNo,
					pageSize: pageSize,
					filters: [{
						column: "locationCode",
						action: "==",
						value: this.locationCode

					}]
				}

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

			},

			getContentByTab(index, pageNo, pageSize, type) {
				if (index === 0) {
					this.getSummary(pageNo, pageSize, type);
				} else if (index === 1) this.getDetailList(pageNo, pageSize, type)
				else if (index === 2) {
					this.getExpectin(pageNo, pageSize, type)
				} else if (index == 3) {
					this.getExpectOut(pageNo, pageSize, type)
				}
			},

			showMessage(message) {
				this.$refs.comMessage.showWarningMessage(message, res => {
					if (res) {
						// this.afterCloseMessage()
					}
				});
			},
			afterCloseMessage() {
				if (this.$refs.scanPopup != undefined) {
					this.$refs.scanPopup.getfocus();
				}
			},
			tabChange(index) {
				this.tabIndex = index;
				this.getContentByTab(index, this.pageNo, this.pageSize,"refresh")
			},
		}
	};
</script>

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