<!-- 收货任务详情 -->
<template>

	<view class="" style="width: 100%;">
		<page-meta root-font-size="18px"></page-meta>
		<view class="top_card">
			<win-job-scan-count :dataContent="receiptJob" :scanCount='scanCount' :allCount="allCount">
			</win-job-scan-count>
		</view>
		<!-- 	<view class="">
			<view v-for="(item, index) in swipeList">
				---------------------------------
				<receiptDetal :dataContent="item" :index ='index'>
					==========================================
					<view v-for="(item2, index2) in item.shijiList">
						<receiptDetalA :dataContent="item2" :index ='index2'></receiptDetalA>
					</view>
				</receiptDetal>
				---------------------------------
			</view>
		</view> -->



		<scroll-view scroll-y="true" class="scroll-detail" style="margin-top: 20rpx;">
			<view class="detail-list" v-for="(item, index) in receiptJob.details" :key="item.id"
				style="margin-top: 4rpx;">
				<view class="">
					<receipt-recommend :dataContent="item"></receipt-recommend>
					==================================
					<receipt-handle v-if="item.handledQty" :dataContent="item" :isEdit="true"></receipt-handle>
				</view>

			</view>
		</scroll-view>

		<view class="pop_btn ">
			<view class="new_btn_bot uni-flex uni-row space-between btn_unusual">
				<button class="new_clear_btn btn_triple" @click="clear()">重置</button>
				<button class="new_save_btn btn_triple" @click="commit()">提交</button>
			</view>
		</view>

		<win-scan-button @goScan='openScanPopup'></win-scan-button>
		<win-scan-pack ref="scanPopup" @getScanResult='getScanResult'></win-scan-pack>
		<com-message ref="comMessage" @afterCloseCommitMessage='closeCommitMessage()'
			@afterCloseScanMessage='closeScanMessage'></com-message>
	</view>
</template>

<script>
	import {
		purchaseReceipts,
		takeReceiptJob,
		cancelTakeReceiptJob,
		finishReceiptJob,
		getFileByCode,
		unInspectToQualified,
		saveReceiptResult,
		resetReceiptResult
	} from '@/api/index.js';
	import {
		getJobStatuStyle,
		getJobStatuDesc,
		showConfirmMsg,
		compareDesc,
		goHome
	} from '@/common/basic.js';

	import comMessage from '@/mycomponents/common/comMessage.vue'
	import winScanButton from '@/mycomponents/wincom/scanCom/winScanButton.vue'
	import winScanPack from '@/mycomponents/wincom/scanCom/winScanPack.vue'
	import receiptCheck from '/views/task/receipt_check.vue'
	import comJobScanDetail from '@/mycomponents/comjob/comJobScanDetail.vue'
	import winRecommendSimple from '@/mycomponents/wincom/unitCom/winRecommendSimple.vue'
	import winJobScanCount from '@/mycomponents/wincom/basicCom/winJobScanCount.vue'
	import winQty from '@/mycomponents/wincom/unitCom/winQty.vue'

	import receiptDetal from '@/mycomponents/receipt/receipt_detal.vue'
	import receiptRecommend from '@/mycomponents/wincom/modelCom/receiptRecommend.vue'
	import receiptHandle from '@/mycomponents/wincom/modelCom/receiptHandle.vue'


	export default {
		name: 'receipt_detail',
		components: {
			comMessage,
			winScanButton,
			winScanPack,
			receiptCheck,
			comJobScanDetail,
			winRecommendSimple,
			winJobScanCount,
			winQty,
			receiptDetal,
			receiptRecommend,
			receiptHandle
		},
		data() {
			return {
				scrollTop: 0,
				old: {
					scrollTop: 0
				},
				currentItem: {},
				id: '',
				receiptJob: {},
				ispending: false,
				inputQty: "",
				listfocus: false,
				finshed: false,
				received: false,
				allCount: 0,
				scanCount: 0,
				isCheckAll: false,
				selectedItem: {},
				loadingType: "",
				isShowQty_receipt: this.$isShowQty_receipt,
				isEditShowQty_receipt: this.$isEditShowQty_receipt,
			};
		},
		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();
			}
		},
		//拦截返回按钮事件
		onBackPress(e) {
			//已经接收但是没提交任务
			if (this.received) {
				//取消承接任务	
				cancelTakeReceiptJob(this.id)
					.then(res => {})
					.catch(err => {
						this.showMessage(err.message);
					});
			}
		},

		onPullDownRefresh() {
			this.getDetail();
			uni.stopPullDownRefresh();
		},

		mounted() {
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: "#5A7CF3 !important"
			})
		},
		methods: {
			//接收
			receive(callback) {
				if (this.id != null) {
					takeReceiptJob(this.id)
						.then(res => {
							callback();
						})
						.catch(err => {
							this.showMessage(err);
						});
				}
			},

			getDetail() {
				let that = this;
				that.getJoDetail(callBack => {

					that.ispending = that.receiptJob.jobStatus === 2;
					if (that.receiptJob.details != null) {
						that.allCount = that.receiptJob.details.length;
						that.calcScanCount();
						this.$nextTick();
					}
				});
			},

			calcScanCount() {
				let items = this.receiptJob.details.filter(r => {
					if (r.scaned) {
						return r;
					}
				})
				this.scanCount = items != null ? items.length : 0;

				this.closeScanPopup();
			},

			closeScanPopup() {
				if (this.allCount == this.scanCount) {
					this.$refs.scanPopup.closeScanPopup();
				}
			},

			getJoDetail(callBack) {
				let that = this;
				purchaseReceipts(that.id)
					.then(res => {
						console.log("详细", res)
						res.details.sort(compareDesc('purchaseReceiptInspectStatus')); //按扫描信息排序
						that.receiptJob = res;
						callBack();
					})
					.catch(err => {
						this.showMessage(err);
					});
			},

			afterPurchase() {
				let that = this;
				//查询任务明细
				that.getJoDetail(() => {
					this.calcScanCount();
				})
			},

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

				if (datas.length === 0) {
					this.showScanMessage('在收货任务详情中,未找到箱码【' + code + '】');
				} else {
					if (datas.length === 1) {
						let data = datas[0];
						if (data.scaned) {
							this.showScanMessage('箱码【' + data.recommendPackingCode + '】已经扫描,请扫描下一箱零件');
						} else {
							datas[0].scaned = true;
							datas[0].handledLocationCode = result.data.recommendLocationCode;
							datas[0].handledQty = result.data.qty;
							datas[0].handledUom = result.data.uom;
							// datas[0].stdPackQty = result.data.stdPackQty;
							datas[0].stdPackQty = 100;
						}
					}
					this.calcScanCount();
					// this.receiptJob.details = datas;
				}
			},

			getPhotoResult(files) {
				this.afterPurchase();
			},

			// 生产检验结果
			inspectResult() {
				let that = this;
				let uncheckitems = that.receiptJob.details.filter(r => {
					return (r.purchaseReceiptInspectStatus <= 1)
				});
				if (uncheckitems.length > 0) {
					let msg = '还有未扫描的零件,是否要生成检验结果?';
					showConfirmMsg(msg, confirm => {
						if (confirm) {
							that.gotoInspectResult();
						}
					})
				} else {
					that.gotoInspectResult();
				}
			},

			gotoInspectResult() {
				let param = JSON.stringify(this.receiptJob);
				uni.navigateTo({
					url: './receipt_result?id=' + this.id
				});
			},

			//待检转合格

			clear() {
				showConfirmMsg('是否要清空已扫描及已目检的信息?', confirm => {
					if (confirm) {
						let that = this;
						let details = [];
						this.receiptJob.details.forEach(r => {
							details.push(r.id);
						})
						let param = JSON.stringify(details);
						resetReceiptResult(that.id, param).then(res => {
							if (res != null) {
								that.getJoDetail(() => {
									that.scanCount = 0;
								});
							}
						}).catch(err => {
							that.showMessage(err.message)
							uni.hideLoading();
						});
					}
				});
			},

			openScanPopup() {
				this.$refs.scanPopup.openScanPopup();
			},

			scanPopupGetFocus() {
				this.$refs.scanPopup.getfocus();
			},

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

			showScanMessage(message) {
				this.$refs.comMessage.showScanMessage(message);
			},

			showAutoCloseMessge(message) {
				this.$refs.comMessage.showAutoCloseMessge(message);
			},

			closeScanMessage() {
				this.$refs.scanPopup.getfocus();
			},
			upper: function(e) {
				// console.log(e)
			},

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

			scroll: function(e) {
				// console.log(e)
				this.old.scrollTop = e.receiptJob.scrollTop;
			},

			/**
			 * BlobUrl转blob数据  
			 * @param {Object} url blob URL
			 * @param {Object} callback 回调函数
			 */
			objectURLToBlob(url, callback) {
				const http = new XMLHttpRequest();
				http.open("GET", url, true);
				http.responseType = "blob";
				http.onload = function(e) {
					if (this.status == 200 || this.status === 0) {
						callback(this.response);
					}
				};
				http.send();
			},
			commit() {

			},
		}
	}
</script>

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