<template>
	<view class="">
		<com-blank-view @goScan='openScanPopup' v-if="itemCode==''"></com-blank-view>
		<view class="" v-if="itemCode!=''">
			<item-info :itemdetail='itemDetail'></item-info>
		</view>
		<view class="tabs" v-if="itemCode!=''">
			<scroll-view id="tab-bar" class="scroll-h" :scroll-x="true" :show-scrollbar="false">
				<view class="title_tab">
					<view v-for="(tab, index) in tabBars" :key="tab.id" class="uni-tab-item" :id="tab.id"
						:data-current="index" @click="ontabtap">
						<text class="uni-tab-item-title"
							:class="tabIndex == index ? 'uni-tab-item-title-active' : ''">{{ tab.name }}</text>
					</view>
				</view>
			</scroll-view>
			<scroll-view :scroll-top="scrollTop" scroll-y="true" @scrolltoupper="upper" @scrolltolower="lower"
				@scroll="scroll">
				<!-- 汇总 -->
				<comItemDetailCard v-if="tabIndex == 0" :itemList="summarys" :isShowPack="false"
					style='margin: 10rpx;'></comItemDetailCard>
			</scroll-view>
			<!-- 明细 -->
			<scroll-view :scroll-top="scrollTop" scroll-y="true" @scrolltoupper="upper" @scrolltolower="lower"
				@scroll="scroll">
				<comItemDetailCard v-if="tabIndex == 1" :itemList="balances"  style='margin: 10rpx;'>
				</comItemDetailCard>
			</scroll-view>
			<!-- 预计入 -->
			<scroll-view :scroll-top="scrollTop" scroll-y="true" @scrolltoupper="upper" @scrolltolower="lower"
				@scroll="scroll">
				<comItemDetailCard v-if="tabIndex == 2" :itemList="dataIn" 
				:isShowLocation="false"
				:isShowBusiness="true"></comItemDetailCard>
			</scroll-view>
			<!-- 预计出 -->
			<scroll-view :scroll-top="scrollTop" scroll-y="true" @scrolltoupper="upper" @scrolltolower="lower"
				@scroll="scroll">
				<comItemDetailCard v-if="tabIndex == 3" :itemList="dataOut" :isShowLocation="false" :isShowBusiness="true">
				</comItemDetailCard>
			</scroll-view>
		</view>

		<win-scan-button @goScan='openScanPopup' v-if="itemCode!=''"></win-scan-button>
		<win-scan-item ref="scanPopup" title='物料代码' @getScanCode='getScanCode'>
		</win-scan-item>
		<comMessage ref="comMessage"></comMessage>
	</view>
</template>

<script>
	import {
		getExpectoutByItemcode,
		getExpectinByItemcode,
		getBalanceByItemCode,
		getBasicItemByCode,
		getBalanceByCodeGroup
	} from '@/api/request2.js';
	import {
		goHome
	} from '@/common/basic.js';
	import itemInfo from '@/mycomponents/item/itemInfo.vue'
	import comBlankView from '@/mycomponents/common/comBlankView.vue'
	import winScanItem from '@/mycomponents/scan/winScanItem.vue'
	import winScanButton from '@/mycomponents/scan/winScanButton.vue'
	import comItemDetailCard from '@/pages/query/coms/comItemDetailCard.vue'
	import comMessage from '@/mycomponents/common/comMessage.vue'

	export default {
		name: 'parts',
		components: {
			itemInfo,
			comBlankView,
			winScanItem,
			winScanButton,
			comItemDetailCard,
			comMessage
		},
		data() {
			return {
				// 每页数据量
				pageSize: 1000,
				// 当前页
				pageCurrent: 1,
				tabIndex: 0,
				tabBars: [{
						name: '汇总',
						id: 'huizong'
					},
					{
						name: '明细',
						id: 'mingxi'
					},
					{
						name: '预计入',
						id: 'in'
					},
					{
						name: '预计出',
						id: 'out'
					}
				],
				itemDetail: {},
				itemCode: '',
				summarys: [],
				balances: [],
				dataIn: [],
				dataOut: [],
				occupieds: [],
				scrollTop: 0,
				old: {
					scrollTop: 0
				},
			};
		},
		onLoad() {

		},
		//返回首页
		onNavigationBarButtonTap(e) {
			if (e.index === 0) {
				goHome();
			}
		},
		mounted() {
			this.openScanPopup();
		},
		methods: {
			openScanPopup() {
				this.$refs.scanPopup.openScanPopup();
			},
			closeScanPopup() {
				this.$refs.scanPopup.closeScanPopup();
			},
			getScanCode(code) {
				if (code == "") {
					this.showMessage('零件号不能为空')
					return;
				}
				this.itemCode = '';
				this.summarys = [];
				this.balances = [];
				this.occupieds = [];
				this.getItemInfo(code);
			},
			getItemInfo(code) {
				uni.showLoading({
					title: "正在查询零件信息...",
					mask: true
				});

				getBasicItemByCode(code).then(res => {
					uni.hideLoading();
					if (res.data.list.length > 0) {
						this.closeScanPopup();
						this.itemCode = res.data.list[0].code;
						this.itemDetail = res.data.list[0];
						this.getContentByTab(this.tabIndex);
					} else {
						this.showMessage('未查找到零件【' + code + '】');
					}

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

			// 汇总
			getSummary() {
				uni.showLoading({
					title: "加载中...",
					mask: true
				});

				getBalanceByCodeGroup(this.itemCode).then(res => {
					uni.hideLoading();
					if (res.data.list.length > 0) {
						this.summarys = res.data.list;
					} else {
						this.showMessage('未查找到零件【' + this.itemCode + '】');
					}
				}).catch(error => {
					uni.hideLoading();
					this.summarys = "";
					this.showMessage(error);
				})
			},
			//明细
			getDetailList() {
				uni.showLoading({
					title: "加载中...",
					mask: true
				});
				getBalanceByItemCode(this.itemCode).then(res => {
					uni.hideLoading();
					if (res.data.list.length > 0) {
						this.balances = res.data.list;
					} else {
						this.showMessage('未查找到零件【' + this.itemCode + '】');
					}
				}).catch(error => {
					uni.hideLoading();
					this.balances = "";
					this.showMessage(error);
					
				})

			},
			//占用
			getoccupied() {
				let that = this;
				uni.showLoading({
					title: "加载中...",
					mask: true
				});
				let params = {
					pageSize: that.pageSize,
					pageIndex: that.pageCurrent,
					itemCode: that.itemCode, //按零件号查询库存列表
				};
				expectIn(params).then(res => {
					this.occupieds = res.items;
					uni.hideLoading();
				}).catch(error => {
					uni.hideLoading();
					this.dataIn = "";
					this.showMessage(error);
				});
			},
			ontabtap(e) {
				let index = e.target.dataset.current || e.currentTarget.dataset.current;
				this.tabIndex = index;
				this.getContentByTab(index);
			},

			getContentByTab(index) {
				if (index === 0) this.getSummary();
				else if (index === 1) this.getDetailList();
				else if (index === 2) {
					this.getExpectin();
				} else if (index == 3) {
					this.getExpectout();
				}
			},
			//预计入
			getExpectin() {
				uni.showLoading({
					title: "加载中...",
					mask: true
				});
				getExpectinByItemcode(this.itemCode).then(res => {
					uni.hideLoading();
					if (res.data.total > 0) {
						this.dataIn = res.data.list;
					} else {
						this.showMessage('未查找到零件【' + this.itemCode + '】');
					}
				}).catch(error => {
					uni.hideLoading();
					this.dataIn = "";
					this.showMessage(error);
				})
			},

			//预计出
			getExpectout() {
				uni.showLoading({
					title: "加载中...",
					mask: true
				});
				getExpectoutByItemcode(this.itemCode).then(res => {
					uni.hideLoading();
					if (res.data.total > 0) {
						this.dataOut = res.data.list;
					} else {
						this.showMessage('未查找到零件【' + this.itemCode + '】');
					}
				}).catch(error => {
					this.dataOut = "";
					uni.hideLoading();
					this.showMessage(error);
				})
			},


			upper: function(e) {
				// console.log(e)
			},
			lower: function(e) {
				// console.log(e)
			},
			scroll: function(e) {
				// console.log(e)
				this.old.scrollTop = e.detail.scrollTop;
			},
			showMessage(message) {
				this.$refs.comMessage.showErrorMessage(message, res => {
					if (res) {
						this.afterCloseMessage()
					}
				});
			},
			afterCloseMessage() {
				this.$refs.scanPopup.getfocus();
			}
		}
	};
</script>

<style scoped lang="scss">
</style>
