<!-- 收货任务详情 -->
<template>
	<page-meta root-font-size="18px"></page-meta>
	<view class="">
		<view class="top_card">
			<view class="device-detail" style="padding-bottom: 0 !important;">
				<view class="ljh_box nopad">
					<text class="state-style fl margin_xs_top" :class="detail.jobStatus | statusStyle">
						{{ detail.jobStatus | statusColor }}
					</text>
					<text class="tit_ljh">{{ detail.number }}</text>
				</view>
				<view class="cell_box uni-flex uni-row margin_top">
					<view class="cell_info">
						<view class="text_lightblue">总数量</view>
						<view class="text_black text_bold">{{ allCount}}</view>
					</view>
					<view class="cell_info">
						<view class="text_lightblue">已扫描</view>
						<view class="text_black text_bold">{{ scanCount}}</view>
					</view>
				</view>
			</view>
		</view>

		<scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-detail" @scrolltoupper="upper"
			@scrolltolower="lower" @scroll="scroll" style="margin-top: 20rpx;">
			<view class="detail-list" v-for="(item, index) in detail.details" :key="item.id" style="margin-top: 4rpx;">
				<view class="detail-content">
					<view class="choose_main">
						<view class="ljh_box">
							<view class="uni-flex uni-row space-between">
								<view class="text-itemcode">零件号:{{ item.itemCode }}</view>
							</view>
							<view class="label_order">
								<image class="icon_normal" src="@/static/icons_ui/icon_asn.svg"></image>
								<text>名称:{{ item.itemName }}</text>
							</view>
							<view class="uni-flex space-between">
								<view class="label_order" style="width: 50%;">
									<image class="icon_normal" src="@/static/icons_ui/icon_tm.svg"></image>
									<text>箱码:{{ item.packingCode }}</text>
								</view>

								<view class="label_order" style="width: 50%;">
									<image class="icon_normal" src="@/static/icons_ui/icon_avn.svg"></image>
									<text>数量: {{ item.handledQty}}{{ item.handledUom }}</text>
								</view>
							</view>
						</view>
					</view>
				</view>

			</view>
		</scroll-view>
		<view class="uni-flex uni-row new_btn_bot">
			<button class="new_clear_btn btn_double" @click="clear()">清空</button>
			<!-- <button class="new_clear_btn btn_double" @click="choseAll()">全选</button> -->
			<button class="new_save_btn btn_double" @click="submit()">提交</button>
		</view>

		<com-message ref="comMessage"></com-message>
		<win-scan-button @goScan='openScanPopup'></win-scan-button>
		<win-scan-by-pack ref="scanPopup" @getScanResult='getScanResult'></win-scan-by-pack>
	</view>
</template>

<script>
	import {
		getReturnJobDetail,
		takeReturnJob,
		cancelTakeReturnJob,
		finishReturnJob
	} from '@/api/index.js';
	import {
		getJobStatuStyle,
		getJobStatuDesc,
		showConfirmMsg,
		compareDesc,
		goHome
	} from '@/common/basic.js';
	import {
		getReturnReasonArray
	} from '@/common/array.js'
	import comMessage from '@/mycomponents/common/comMessage.vue'
	import winScanButton from '@/mycomponents/wincom/winScanButton.vue'
	import winScanByPack from '@/mycomponents/wincom/winScanByPack.vue'
	import dyItemInfo from '@/mycomponents/dycom/dyItemInfo.vue'
	export default {
		name: 'transferReceipt_detail',
		components: {
			comMessage,
			winScanButton,
			winScanByPack,
			dyItemInfo
		},
		data() {
			return {
				scrollTop: 0,
				old: {
					scrollTop: 0
				},
				currentItem: {},
				id: '',
				detail: {},
				ispending: false,
				inputQty: "",
				listfocus: false,
				finshed: false,
				received: false,
				allCount: 0,
				scanCount: 0,
				isCheckAll: false,
				selectedItem: {},
				returnReason: "",
				returnReasonArray: [],
			};
		},
		mounted: function() {

		},
		onLoad(option) {
			if (option.id != undefined) {
				this.id = option.id;
				//新建的任务自动接收
				if (option.jobStatus == 1) {
					this.receive((callback => {
						this.received = true;
						this.getDetail();
					}));
				} else {
					this.getDetail();
				}
			}
		},
		//返回首页
		onNavigationBarButtonTap(e) {
			if (e.index === 0) {
				goHome();
			}else if(e.index === 1){
				window.location.reload();
			}
		},
		//拦截返回按钮事件
		onBackPress(e) {
			//已经接收但是没提交任务
			if (this.received) {
				//取消承接任务	
				cancelTakeReturnJob(this.id)
					.then(res => {})
					.catch(err => {
						this.showMessage(err.message);
					});
			}
		},
		filters: {
			statusStyle: function(val) {
				return getJobStatuStyle(val);
			},
			statusColor: function(val) {
				return getJobStatuDesc(val);
			},
			inspectDesc: function(val) {
				if (val == 'none') return ''
				else if (val == 'pass') return '合格'
				else if (val == 'unPass') return '不合格'
			},
		},
		mounted() {
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: "#5A7CF3 !important"
			})
		},
		methods: {
			openScanPopup() {
				this.$refs.scanPopup.openScanPopup();
			},

			//接收
			receive(callback) {
				if (this.id != null) {
					takeReturnJob(this.id)
						.then(res => {
							callback();
						})
						.catch(err => {
							this.showMessage(err.message);
						});
				}
			},

			getDetail() {
				let that = this;
				uni.showLoading({
					title: '加载中...',
					mask: true
				})
				getReturnJobDetail(that.id)
					.then(res => {
						that.detail = res;
						that.ispending = res.jobStatus === 2;
						that.allCount = res.details.length;
						that.initDetails();
						this.$nextTick();
						uni.hideLoading();
					})
					.catch(err => {
						uni.hideLoading();
						this.showMessage(err.message);
					});
			},

			initDetails() {
				this.detail.details.forEach(v => {
					v.scaned = false;
					v.scanDate = new Date();
				});
				this.$forceUpdate();
			},

			//处理扫描解析的返回值
			getScanResult(result) {
				let code = result.data.code;
				if (code == '') return;
				let datas = this.detail.details.filter(function(r) {
					if (
						r.recommendPackingCode === code) {
						return r;
					}
				});

				if (datas.length === 0) {
					this.showMessage('在退货任务详情中,未找到箱码:[' + code + ']');
				} else {
					if (datas.length === 1) {
						let data = datas[0];
						if (data.scaned) {
							this.showMessage('箱码[' + data.recommendPackingCode + ']已经扫描,请扫描下一箱');
						} else {
							this.scanItem(data);
						}
					} else {
						datas.forEach(r => {
							this.scanItem(r);
						});
					}
					this.scanCount = this.detail.details.filter(r => r.scaned).length;
					this.detail.details.sort(compareDesc('scanDate')); //按扫描信息排序
				}
			},

			//收货
			scanItem(data) {
				data.scaned = true;
				data.scanDate = new Date()
			},

			submit() {
				if (this.returnReason == '') {
					this.showMessage('请选择退货原因')
					return;
				}
				let that = this;
				let uncheckitems = that.detail.details.filter(r => {
					return r.scaned === false;
				});

				if (uncheckitems.length > 0) {
					let msg = '还有未扫描的零件,是否要提交?';
					showConfirmMsg(msg, confirm => {
						if (confirm) {
							uncheckitems.forEach(r => {
								r.handledQty //将实际数量置为0
							});
							that.finsh();
						}
					})
				} else {
					that.finsh();
				}
			},

			finsh() {
				uni.showLoading({
					title: "提交中...",
					mask: true
				});
				//赋值
				this.detail.details.forEach(r => {
					r.handledContainerCode = r.recommendContainerCode;
					r.handledPackingCode = r.recommendPackingCode;
					r.handledBatch = r.recommendBatch;
					r.handledLot = r.recommendLot;
					r.handledLocationCode = r.recommendLocationCode;
					r.worker = localStorage.userName_CN ==""?localStorage.userName:localStorage.userName_CN
				});

				let params = JSON.stringify(this.detail);
				finishReturnJob(this.id, params)
					.then(res => {
						setTimeout(() => {
							uni.navigateTo({
								url: './purchaseReturn'
							})
						}, 1000)
						uni.hideLoading();
						this.showMessage('提交成功');
					})
					.catch(err => {
						this.showMessage(err.message);
						uni.hideLoading();
					});
			},

			choseAll() {
				this.isCheckAll = !this.isCheckAll;
				this.detail.details.forEach((v, i) => {
					v.scaned = this.isCheckAll;
					v.scanDate = new Date();
				});
				this.scanCount = this.allCount;
				this.$forceUpdate();
			},

			clear() {
				showConfirmMsg('是否要清空已扫描的数据?', confir => {
					if (confirm) {
						this.scanCount = 0;
						this.isCheckAll = false;
						this.initDetails();
					}
				});
			},

			showMessage(message) {
				this.$refs.comMessage.showMessage(message);
			},
			upper: function(e) {
				// console.log(e)
			},

			lower: function(e) {
				// console.log(e)
			},

			scroll: function(e) {
				// console.log(e)
				this.old.scrollTop = e.detail.scrollTop;
			}
		}
	}
</script>

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