<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: 10rpx;'>
					</comLocationDetailCard>
				</view>
				<view v-if="tabIndex == 1" v-for="(item, index) in dataList">
					<comLocationDetailCard :dataContent="item" style='margin: 10rpx;'></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>
		<comMessage ref="comMessage"></comMessage>

	</view>
</template>

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


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

				dataList: [],
				tabList: ['汇总', '明细'],
				tabIndex: 0,
			};
		},
		onLoad() {},
		onNavigationBarButtonTap(e) {
			if (e.index === 0) {
				goHome();
			}
		},
		filters: {

		},
		mounted() {
			this.openScanPopup()
		},
		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) {
				let that = this;
				uni.showLoading({
					title: "加载中...",
					mask: true
				});
				var params = {
					itemCode: this.itemCode,
					pageNo: pageNo,
					pageSize: pageSize
				}

				getBalanceByLocationcodeGroup(params).then(res => {
					uni.hideLoading();
					if (res.data.list.length > 0) {
						this.summarysList = this.setShowList(res.data.list)
						this.$refs.paging.complete(this.summarysList);

					} else {
						this.$refs.paging.complete(false);
						this.showMessage('未查找到库位【' + this.locationCode + '】');
					}
				}).catch(error => {
					this.$refs.paging.complete(false);
					uni.hideLoading();
					this.showMessage(error);
				})

			},

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

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

			setShowList(list) {
				var resultlist = [];
				list.forEach(res => {
					var temp = resultlist.find(res1 =>
						res1.itemCode == res.itemCode)

					if (temp == undefined) {
						var 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;
			},
			ontabtap(e) {
				let index = e.target.dataset.current || e.currentTarget.dataset.current;
				this.tabIndex = index;
				this.getContentByTab(this.tabIndex);
			},

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

			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.$refs.paging.reload(true);
			},
			queryList(pageNo, pageSize) {
				console.log("加载", pageNo)
				if (this.locationCode != "") {
					this.getContentByTab(this.tabIndex, pageNo, pageSize)
				}

			},
		}
	};
</script>

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