<template>
	<page-meta root-font-size="18px"></page-meta>
	<view class="content">
		<win-empty-view v-if="countList.length==0"></win-empty-view>
		<view hover-class="uni-list-cell-hover" v-for="(item, index) in countList" @click="openDetail(item)">
			<com-count :datacontent="item"></com-count>
		</view>
	</view>

	<win-scan-button @goScan='openScanPopup'></win-scan-button>
	<win-mulit-scan ref="scanPopup" :titleArray="titleArray" @getScanResult='getScanResult'></win-mulit-scan>
	<com-count-items ref="popupCountItems" @selectedItem='getSelectedItem'></com-count-items>

	<uni-load-more :status="loadingType" v-if="countList.length>0"></uni-load-more>
	<com-message ref="comMessage"></com-message>
</template>

<script>
	import {
		getCountJobList,
		getCountJobByLocationAsync,
		getCountJobByNumber,
		locationsAsync
	} from '@/api/index.js';
	import {
		getJobStatuStyle,
		getJobStatuDesc,
		maxPageSize,
		goHome
	} from '@/common/basic.js';
	import winEmptyView from '@/mycomponents/wincom/winEmptyView.vue'
	import comCount from '@/mycomponents/coms/task/comCount.vue';
	import comCountItems from '@/mycomponents/coms/task/comCountItems.vue'
	import comMessage from '@/mycomponents/common/comMessage.vue'
	import winScanButton from '@/mycomponents/wincom/winScanButton.vue'
	import winMulitScan from '@/mycomponents/wincom/winMulitScan.vue'

	export default {
		name: 'count',
		components: {
			winEmptyView,
			comCount,
			comCountItems,
			comMessage,
			winScanButton,
			winMulitScan
		},
		//后退按钮
		onBackPress(options) {
			if (options.from === 'navigateBack') {
				return false;
			}
			goHome();
			return true;
		},
		onNavigationBarButtonTap(e) {
			if (e.index === 0) {
				goHome();
			}else if(e.index === 1){
				window.location.reload();
			}
		},
		data() {
			return {
				allCountList: [],
				countList: [], //当前页面显示的列表
				reload: false,
				status: '',
				loadMore: {
					contentdown: '上拉加载更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				},
				pageSize: this.modelConfig, //每次向服务器请求的分页数量
				pageIndex: 1,
				filterSize: 20, //静态分页
				filterIndex: 1, //静态分页
				allPageCount: 0,
				maxIndex: 1,
				titleArray: ['任务编号', '库位', ],
				loadingType: "nomore"
			};
		},
		props: {
			datacontent: {
				type: Object,
				value: null
			}
		},
		onShow() {
			this.getList('refresh');
		},
		onPullDownRefresh() {
			this.getList('refresh');
		},
		onReachBottom() {
			// this.filterIndex++;
			// //已经显示了静态分页数据的最后一页
			// if (this.filterIndex > this.allPageCount) {
			// 	//重新向服务器请求
			// 	this.filterIndex--;
			// 	this.pageIndex++;
			// 	this.reload = true;
			// 	this.status = 'more';
			// 	this.getList();
			// } else {
			// 	this.reload = true;
			// 	this.status = 'more';
			// 	this.getPageList();
			// }
			//避免多次触发
			if (this.loadingType == 'loading' || this.loadingType == 'nomore') {
				return;
			}
			this.getList("more");
		},
		methods: {
			openScanPopup() {
				this.$refs.scanPopup.openScanPopup();
			},
			getList(type) {
				let that = this;
				uni.showLoading({
					title: "加载中....",
					mask: true
				});

				this.loadingType = "loading";
				if (type === "refresh") {
					this.pageIndex = 1;
					this.countList = [];
				}
				let params = {
					pageSize: that.pageSize,
					pageIndex: that.pageIndex
				};
				getCountJobList(params)
					.then(res => {
						console.log('list', res);
						uni.hideLoading();
						if (type === "refresh") {
							uni.stopPullDownRefresh();
						}
						var list = res.items;
						this.loadingType = "loadmore";
						if (list == null || list.length == 0) {
							//没数据了
							this.loadingType = "nomore";
							return;
						}
						that.countList = type === "refresh" ? list : this.countList.concat(list);
						that.pageIndex++;
					})
					.catch(err => {
						this.loadingType = "";
						this.showMessage(err.message);
						uni.hideLoading();
						if (type === "refresh") {
							uni.stopPullDownRefresh();
						}
					});
			},

			getPageList() {
				let minIndex = (this.filterIndex - 1) * this.filterSize; //0
				let maxIndex = this.filterIndex * this.filterSize; //100
				let items = this.allCountList.filter(r => r.index > minIndex && r.index <= maxIndex);
				this.countList = items;
			},

			getAllPageCount() {
				let totalCount = this.allCountList.length;
				let count = totalCount > 0 ? ((totalCount < this.filterSize) ? 1 : ((totalCount % this.filterSize) ? (
					parseInt(
						totalCount / this.filterSize) + 1) : (
					totalCount / this.filterSize))) : 0;
				return count;
			},

			getScanResult(type, result) {
				if (type == '任务编号') {
					this.getByNumber(type, result.data.code);
				} else if (type == '库位') {
					this.getlocationScanResult(type, result.data.code);
				} else if (type == '零件号') {
					this.getItemScanResult(result);
				}
			},

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

				getCountJobByNumber(code).then(res => {
					uni.hideLoading();
					if (res != null) {
						that.openDetail(res);
					} else {
						that.showMessage('未查找到' + type + '为【' + code + '】的盘点任务');
					}
				}).catch(err => {
					that.showMessage(err.message);
					uni.hideLoading();
				});
			},

			async getlocationScanResult(type, code) {
				let that = this;
				uni.showLoading({
					title: '加载中...',
					mask: true
				})
				let locationRes = await locationsAsync(code);
				if (locationRes != '' || locationRes != null) {
					let jobRes = await getCountJobByLocationAsync(code);
					if (jobRes.error == undefined) {
						if (jobRes.totalCount == 0) {
							that.showMessage('未查找到' + type + '为【' + code + '】的盘点任务');
						} else if (jobRes.totalCount == 1) {
							this.openDetail(jobRes.items[0]);
						} else {
							this.openCountItems(jobRes.items);
						}						
					} else {
						that.showMessage('未查找到' + type + '为【' + code + '】的盘点任务');
					}
				} else {
					this.showMessage('未查找到库位【' + code + '】');
				}
				uni.hideLoading();
			},

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

			openCountItems(items) {
				this.$refs['popupCountItems'].openPopup(items);
			},

			getSelectedItem(item) {
				this.openDetail(item);
			},
			showMessage(message) {
				this.$refs.comMessage.showMessage(message);
			},

			getNumberScanResult(result) {
				let that = this;
				let number = result.data.code;
				let items = that.countList.filter(r => {
					return r.number === number
				});

				if (items.length == 0) {
					this.showMessage('未查找到对应的盘点任务');
				} else {
					if (items.length == 1) {
						that.openDetail(items[0]);
					} else {
						that.openCountItems(items);
					}
				}

			},
		}
	};
</script>

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