<template>
	<page-meta root-font-size="16px"></page-meta>
	<view class="">
		<view class="view-noData" v-if="inspectList.length==0">
			<image class="default_nodata" src="@/static/icons_ui/default_data.png"></image>
		</view>
		<scroll-view class="uni-scroll-view" scroll-y="true">
			<view v-for="(item, index) in inspectList" :key="item.id" @click="openSummary(item)"
				style="margin-top:20rpx;">
				<com-inspect :datacontent="item"></com-inspect>
			</view>
		</scroll-view>
		<comMessage ref="comMessage"></comMessage>
		<win-scan-button @goScan='openScanPopup'></win-scan-button>
		<win-mulit-scan ref="scanPopup" :titleArray="titleArray" @getScanResult='getScanResult'>
		</win-mulit-scan>
	</view>
</template>

<script>
	import {
		getInspectList
	} from '@/api/index.js';
	import {
		getJobStatuStyle,
		getJobStatuDesc,
		goHome,
	} from '@/common/basic.js';

	import comInspect from '@/mycomponents/coms/task/comInspect.vue';
	import comMessage from '@/mycomponents/common/comMessage.vue'
	import winMulitScan from '@/mycomponents/wincom/winMulitScan.vue'
	import winScanButton from '@/mycomponents/wincom/winScanButton.vue'

	export default {
		name: 'inspect',
		components: {
			comInspect,
			comMessage,
			winMulitScan,
			winScanButton
		},
		data() {
			return {
				inspectList: [],
				contentText: {
					contentdown: '上拉加载更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				},
				pageSize: 1000,
				pageIndex: 1,
				scrollHeight: 0,
				titleArray: ['ASN', '到货单', 'ERP料号'],
			};
		},
		props: {
			datacontent: {
				type: Object,
				value: null
			}
		},
		onReady() {
			// let that = this;
			// uni.getSystemInfo({ //调用uni-app接口获取屏幕高度
			// 	success(resu) { //成功回调函数
			// 		let wHeight = resu.windowHeight; //windoHeight为窗口高度，主要使用的是这个
			// 		const query = uni.createSelectorQuery()
			// 		query.select('.scrollView').boundingClientRect()
			// 		query.exec(function(res) {
			// 			if (res[0] != null) {
			// 				that.scrollHeight = wHeight - res[0].top - 40;
			// 			}
			// 		})
			// 	}
			// })
		},
		activated() {},
		onShow() {
			this.getList();
		},
		onPullDownRefresh() {
			console.log('refresh');
			this.pageIndex = 1;
			this.inspectList = []
			this.getList();
		},
		onNavigationBarButtonTap(e) {
			if (e.index === 0) {
				uni.reLaunch({
					url: '../index/index'
				})
			}else if(e.index === 1){
				window.location.reload();
			}
		},
		methods: {
			openScanPopup() {
				this.$refs.scanPopup.openScanPopup();
			},
			getScanResult(type, result) {
				let code = result.data.code;
				if (code == "") {
					this.showMessage('扫描内容不能为空')
					return;
				}
				this.getScanItems(type, code);
			},
			getList() {
				let that = this;
				let params = {
					pageSize: that.pageSize,
					pageIndex: that.pageIndex,
				};

				uni.showLoading({
					title: "加载中....",
					mask: true
				});
				getInspectList(params)
					.then(res => {
						uni.stopPullDownRefresh();
						console.log('list', res);
						if (res.items != null && res.items.length > 0) {
							res.items.forEach(r => {
								if (r.summaryDetails != null && r.summaryDetails[0] != null) {

									r.item = r.summaryDetails[0].item;
									r.itemCode = r.summaryDetails[0].itemCode; //ERP料号
									r.lot = r.summaryDetails[0].recommendLot; //批次
									if (r.summaryDetails[0].receiveQty === undefined) {
										r.qty = {
											qty: 0,
											uom: ''
										};
									} else {
										r.qty = r.summaryDetails[0].receiveQty; //收货数量
									}

								}
							});
							that.inspectList = res.items;
						}
						uni.hideLoading();
					})
					.catch(err => {
						this.showMessage(err.message);
						uni.stopPullDownRefresh();
						uni.hideLoading();
					});
			},

			getScanItems(type, code) {
				if (code != "") {
					let that = this;
					let itemDetail = {};
					switch (type) {
						case 'ASN':
							itemDetail = that.inspectList.find(r => {
								return r.asnNumber === code
							});
							break;
						case '到货单':
							itemDetail = that.inspectList.find(r => {
								return r.arriveNoticeNumber === code
							});
							break;
						case 'ERP料号':
							itemDetail = that.inspectList.find(r => {
								return r.itemCode === code
							});
							break;
					}
					if (itemDetail != undefined) {
						that.openSummary(itemDetail);
					} else {
						this.showMessage("未找到" + type + ": " + code);
					}
				} else {
					this.showMessage('请先扫描标签')
				}
			},

			openSummary(item) {
				uni.navigateTo({
					url: './Inspect_summary?id=' + item.id + '&jobStatus=' + item.jobStatus
				})
			},

			openDetail(item) {
				uni.navigateTo({
					url: './Inspect_detail?id=' + item.id + '&jobStatus=' + item.jobStatus
				})
			},

			showMessage(message) {
				this.$refs.comMessage.showMessage(message);
			},
		}
	};
</script>

<style scoped lang="scss">
	@import '../../common/pdabasic.css';
</style>
