<template>
	<page-meta root-font-size="16px"></page-meta>
	<view class="">
		<view class="view-noData" v-if="putawaydetailList.length==0">
			<image class="default_nodata" src="@/static/icons_ui/default_data.png"></image>
		</view>

		<view hover-class="uni-list-cell-hover" v-for="(item, index) in putawaydetailList" :key="item.id"
			@click="openDetail(item)">
			<com-putaway :datacontent="item"></com-putaway>
		</view>
		<uni-load-more v-if="reload" :status="status" :icon-size="16" :content-text="contentText" />
		<win-scan-button @goScan='openScanPopup'></win-scan-button>
		<win-scan-by-pack ref="scanPopup" @getScanResult='getScanResult'></win-scan-by-pack>
		
		<!-- com-message必须放在最下层 -->
		<com-message ref="comMessage"></com-message>
	</view>
</template>

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

	import comPutaway from '@/mycomponents/coms/task/comPutaway.vue';
	import comMessage from '@/mycomponents/common/comMessage.vue'
	import winScanButton from '@/mycomponents/wincom/winScanButton.vue'
	import winScanByPack from '@/mycomponents/wincom/winScanByPack.vue'

	export default {
		name: 'putaway',
		components: {
			comPutaway,
			comMessage,
			winScanButton,
			winScanByPack
		},
		data() {
			return {
				//popup
				type: '',
				putawaydetailList: [],
				reload: false,
				status: '',
				contentText: {
					contentdown: '上拉加载更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				},
				pageSize: 1000,
				pageIndex: 1
			};
		},
		props: {
			datacontent: {
				type: Object,
				value: null
			},
			isByLocation: {
				type: Boolean,
				value: false
			}
		},
		mounted() {
			this.getList();
		},
		onPullDownRefresh() {
			console.log('refresh');
			this.pageIndex = 1;
			this.putawaydetailList = []
			this.getList();
		},
		//返回首页
		onNavigationBarButtonTap(e) {
			if (e.index === 0) {
				goHome();
			}else if(e.index === 1){
				window.location.reload();
			}
		},
		methods: {
			openScanPopup() {
				this.$refs.scanPopup.openScanPopup();
			},
			getList() {
				let that = this;
				let params = {
					pageSize: that.pageSize,
					pageIndex: that.pageIndex,
				};

				uni.showLoading({
					title: "加载中....",
					mask: true
				});
				getPutawayJobList(params)
					.then(res => {
						uni.stopPullDownRefresh();
						console.log('list', res);
						let details = [];
						res.items?.forEach(r => {
							r.details?.forEach(detail => {
								//子表里没有任务状态,增加一个任务状态的属性
								detail.jobStatus = r.jobStatus;
								details.push(detail);
							});
						});

						that.putawaydetailList = details;
						if (details.length == 0) {
							this.showMessage
						}
						uni.hideLoading();
					})
					.catch(err => {
						uni.stopPullDownRefresh();
						console.log('list err', err);
						uni.hideLoading();
					});
			},

			getScanResult(result) {
				let that = this;
				let code = '';
				let item = {};

				//不管是按托标签上架还是按照箱标签上架都对应一个上架任务
				if (result.data.isPack === true) { //箱码
					item = this.putawaydetailList.find(r => {
						//零件号，箱码,批次
						return r.itemCode === result.data.itemCode &&
							r.recommendPackingCode === result.data.packingCode &&
							r.recommendLot === result.data.lot
					})

				} else if (result.data.isPack === false) {
					item = this.putawaydetailList.find(r => {
						return r.recommendContainerCode === result.data.containerCode
					})
				} else {
					this.showMessage('标签格式不正确');
					return;
				}
				if (item == null) {
					this.showMessage('未查找到任务');
				} else {
					// item.masterID:任务主表id
					let isPack = result.data.isPack;
					uni.navigateTo({
						url: './putaway_detail?id=' + item.masterID + '&jobStatus=' + item.jobStatus + '&isPack=' +
							isPack + '&byLocation=' + this.isByLocation
					})
				}
			},

			openDetail(item) {
				uni.navigateTo({
					url: './putaway_detail?id=' + item.masterID + '&jobStatus=' + item.jobStatus + '&isPack=' +
						true + '&byLocation=' + this.isByLocation
				})
			},
			showMessage(message) {
				this.$refs.comMessage.showMessage(message);
			}
		}
	};
</script>

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