<template>
	<view class="" v-if="jobContent!=null">

		<view class="pda-list">
			<view class="pda-list-cell">
				<view class="pda-list-cell-left">
					<view class="uni-label">类型</view>
				</view>
				<view class="pda-list-cell-db">
					{{getInspectName(jobContent.inspectType) }}
				</view>
			</view>
			<view class="pda-list-cell">
				<view class="pda-list-cell-left">
					<view class="uni-label">零件号</view>
				</view>
				<view class="pda-list-cell-db">
					<view class="uni-flex space-between">
						<input class="display-input" type="text" :disabled="true" v-model="jobContent.itemCode" />
					</view>
				</view>
			</view>
			<view class="pda-list-cell">
				<view class="pda-list-cell-left">
					<view class="uni-label">批次</view>
				</view>
				<view class="pda-list-cell-db">
					<input class="display-input" type="text" :disabled="true" v-model="jobContent.batch" />
				</view>
			</view>
			<view class="pda-list-cell" style="margin-top: 10rpx;margin-bottom: 10rpx;">
				<view class="pda-list-cell-left">
					<view class="uni-label">收货数量</view>
				</view>
				<view class="pda-list-cell-db">
					<text>{{jobContent.receiveQty}} {{getUnitInfo(jobContent.uom)}}</text>
				</view>

			</view>
			<view class="pda-list-cell">
				<view class="pda-list-cell-left">
					<view class="uni-label">检验数量</view>
				</view>
				<view class="pda-list-cell-db">
					<text>{{jobContent.sampleQty}} {{getUnitInfo(jobContent.uom)}}</text>
				</view>
			</view>

			<view class="pda-list-cell">
				<view class="pda-list-cell-left">
					<view class="uni-label">不合格数量</view>
				</view>
				<view class="pda-list-cell-db">
					<uni-easyinput :inputBorder="true" type="number" placeholder="请输入数字" v-model="jobContent.failedQty" @clear="clearFailedQty"
						@blur="failedQtyConfirm" class="count-input">
					</uni-easyinput>
				</view>
			</view>


			<view class="pda-list-cell">
				<view class="pda-list-cell-left">
					<view class="uni-label">报废数量</view>
				</view>
				<view class="pda-list-cell-db">
					<uni-easyinput placeholder="请输入数字" type="number" v-model="jobContent.crackQty" @blur="" @clear="clearCrackQty"
						class="count-input">
					</uni-easyinput>
				</view>
			</view>

			<view class="pda-list-cell">
				<view class="pda-list-cell-left">
					<view class="uni-label">下一步动作</view>
				</view>
				<view class="pda-list-cell-db">
					<uni-data-picker class='uni-data-picker' placeholder="请选择下一步动作" popup-title="下一步动作"
						@change="nextStepChange" :localdata="nextActionList" v-model="nextAction">
					</uni-data-picker>
				</view>
			</view>

			<view class="pda-list-cell">
				<view class="pda-list-cell-left">
					<view class="uni-label">最终不合格</view>
				</view>
				<view class="pda-list-cell-db">
					<uni-easyinput placeholder="待计算" type="number" :disabled="true" v-model="jobContent.notPassedQty"
						class="count-input">
					</uni-easyinput>
				</view>
			</view>

			<view class="pda-list-cell">
				<view class="pda-list-cell-left">
					<view class="uni-label">备注</view>
				</view>
				<view class="pda-list-cell-db">
					<uni-easyinput placeholder="请输入备注" type="text" autoHeight v-model="jobContent.remark"
						class="count-input">
					</uni-easyinput>
				</view>
			</view>
		</view>

		<view class="" v-if="false">
			<uni-collapse>
				<uni-collapse-item title="库位列表" style='font-size: 40rpx;' :open='true'>
					<view style="">
						<scroll-view scroll-y="true" class="scroll-detail">
							<view class=""
								style="font-size: 35rpx;display: flex;flex-direction: row;align-items: center;width: 100%;"
								v-for="(item, index) in detailList" :key="item.id">
								<text style="font-size: 35rpx;margin-right: 20rpx;">{{index+1}}.</text>

								<view class="">
									<view class="">
										箱码:{{item.packingNumber}}
									</view>
									<view class="">
										库位:{{item.fromLocationCode}}
									</view>
									<u-line />
								</view>


							</view>
						</scroll-view>
					</view>
				</uni-collapse-item>
			</uni-collapse>

		</view>


		<view class="bottom" style="padding-bottom: 20rpx;">
			<button :disabled="submitting" type="primary" class="save-button" @click="commit">提交</button>
		</view>
		<comMessage ref="comMessage"></comMessage>
	</view>
</template>

<script>
	import comMessage from '@/mycomponents/common/comMessage.vue'
	import {
		getInspectJobDetail,
		takeInspectJob,
		cancleTakeInspectJob,
		inspectJobSubmit
	} from '@/api/request2.js';

	import {
		getInspectFailedReasonList,
		getNextActionList,
		getInspectType,
		getUnitInfo
	} from '@/common/directory.js';
	
	import {
		goHome,
		navigateBack
	} from '@/common/basic.js';

	export default {
		components: {
			comMessage
		},
		data() {
			return {
				id: '',
				status: "",
				itemDetail: {},
				inspectType: '', //免检 全检 抽检 
				failedReasonIndex: 0,
				failedReasonArray: [],
				nextStep: '',
				submitting: false,
				received: false,
				locations: [],
				value: ['0'],
				jobContent: {}, //任务内容
				detailList: [], //接口返回的任务details
				detailSource: [], //绑定在页面上的数据源
				nextActionList: [],
				nextAction: null,

			}
		},
		//返回首页
		onNavigationBarButtonTap(e) {
			if (e.index === 0) {
				goHome();
			}
		},
		onLoad(option) {
			this.id = option.id;
			this.status = option.status;
			this.nextActionList = getNextActionList();

			if (this.id != undefined) {
				// 	//新建的任务自动接收
				if (option.status == "1") {
					this.receive((callback => {
						this.received = true;
						this.getDetail();
					}));
				} else {
					this.getDetail();
				}
			}
		},
		onBackPress(e) {
			//已经接收但是没提交任务
			if (e.from == 'backbutton') {
				if (this.received) {
					//取消承接任务	
					cancleTakeInspectJob(this.id).then(res => {
						uni.navigateBack();
					}).catch(error => {
						uni.navigateBack();
					})
				} else {
					uni.navigateBack();
				}

				return true;
			}

		},
		methods: {
			//接收
			receive(callback) {
				uni.showLoading({
					title: "加载中....",
					mask: true
				});
				if (this.id != null) {
					takeInspectJob(this.id).then(res => {
						uni.hideLoading();
						callback();
					}).catch(error => {
						uni.hideLoading();
						this.showErrorMessage(error)
					})
				}
			},
			getDetail() {
				var that = this;
				uni.showLoading({
					title: "加载中....",
					mask: true
				});

				getInspectJobDetail(that.id).then(res => {
					uni.hideLoading();
					if (res.data == null) {
						that.showMessage('未获取到详情');
					} else {
						if (res.data.subList.length > 0) {
							that.jobContent = res.data;
							that.detailList = res.data.subList;
							that.inspectType = that.jobContent.inspectType;
							that.jobContent.goodQty = null, //合格数量
								that.jobContent.failedQty = null, //不合格数量
								that.jobContent.crackQty = null, //报废数量
								that.jobContent.notPassedQty = null; //最终不合格数量
						} else {
							that.showMessage('列表数据为0');
						}
					}
				}).catch(error => {
					uni.hideLoading()
					this.showMessage(error)
				})

			},
			showMessage(message) {
				this.$refs.comMessage.showErrorMessage(message, res => {
					if (res) {
						// this.afterCloseMessage()
					}
				});
			},
			//不合格数量确认事件
			failedQtyConfirm(value) {
				// if (value.detail.value > this.jobContent.receiveQty) {
				// 	this.jobContent.failedQty = null
				// 	this.showMessage("不合格数[" + value.detail.value + "]不能大于收货数量[" + this.jobContent.receiveQty + "]")
				// 	return
				// }
				// if (value.detail.value+ this.jobContent.crackQty> this.jobContent.receiveQty) {
				// 	this.jobContent.failedQty = null
				// 	this.showMessage("不合格数[" + value.detail.value +"+报废数量["+this.jobContent.crackQty+ "]不能大于收货数量[" + this.jobContent.receiveQty + "]")
				// 	return
				// }
				this.calcNotPassQty()
			},
			getInspectName(value) {
				return getInspectType(value)
			},
			getUnitInfo(value) {
				return getUnitInfo(value).label
			},

			nextStepChange(e) {
				this.nextAction = e.detail.value[0].value;
				this.calcNotPassQty();
			},

			calcNotPassQty() {
				var failedQty = Number(this.jobContent.failedQty == null ? 0 : this.jobContent.failedQty)
				//检验类型：全检(全部合格)
				if (this.inspectType == 'FUll') {
					//最终不合格数量等于不合格数量
					this.jobContent.notPassedQty = failedQty
				} else if (this.inspectType == 'SAMPLING') {
					//检验类型：抽检
					//下一步动作
					//全检、部分合格
					if (this.nextAction == "FULL_INSPECT" || this.nextAction == "PARTIAL_OK") {
						// 最终不合格数量=不合格数量
						this.jobContent.notPassedQty = failedQty
					} else if (this.nextAction == "ALL_NOK") {
						//最终不合格数量=收货数量-报废数量
						this.jobContent.notPassedQty = Number(this.jobContent.receiveQty) - Number(this.jobContent
							.crackQty == null ? 0 : this.jobContent.crackQty)

					}
				}
			},
			
			clearFailedQty(){
				this.jobContent.failedQty = null;
			},
			
			clearCrackQty(){
				this.jobContent.crackQty = null
			},

			commit() {
				
				if (this.jobContent.failedQty == null) {
					this.showMessage("请输入不合格数量")
					return
				}

				if (this.jobContent.crackQty == null) {
					this.showMessage("请输入不报废数量")
					return
				}
				var receiveQty = Number(this.jobContent.receiveQty)
				var failedQty = Number(this.jobContent.failedQty);
				var crackQty = Number(this.jobContent.crackQty);

				if (crackQty + failedQty > receiveQty) {
					var total = crackQty + failedQty;
					this.showMessage("不合格数[" + failedQty + "]+报废数量[" + crackQty + "]等于[" + total + "]大于收货数量[" +
						receiveQty + "]")
					return
				}

				//不合格数量==0、(全部合格)、明细全部合格
				if (failedQty == 0) {
					if (crackQty == 0) {
						if (this.nextAction == "ALL_NOK") {
							this.jobContent.subList.forEach(res => {
								res.handleQty = res.qty
								res.failedQty = res.qty;
								res.notPassedQty = res.failedQty;
								res.crackQty = 0;
								res.goodQty = 0;
								res.failedReason = "";
								res.photos = ""
								res.inspectResult = ""
								res.toInventoryStatus = "NOK"
								res.inspectUser = this.$store.state.user.id
							})
							this.jobContent.nextAction = this.nextAction;
							this.jobContent.goodQty = 0
							this.submitJob(this.jobContent)
						}else {
							//报废数==0，直接提交
							this.jobContent.subList.forEach(res => {
								res.handleQty = res.qty
								res.failedQty = 0;
								res.notPassedQty = res.failedQty;
								res.crackQty = 0;
								res.goodQty = res.qty;
								res.failedReason = "";
								res.photos = ""
								res.inspectResult = ""
								res.toInventoryStatus = "OK"
								res.inspectUser = this.$store.state.user.id
							})
							
							this.jobContent.nextAction = "";
							this.jobContent.goodQty = this.jobContent.receiveQty
							this.submitJob(this.jobContent)
						}

					} else {
						//报废数大于0，进入详情
						//进入下一页面
						uni.navigateTo({
							url: './inspectDetail?id=' + this.id +
								'&status=' + this.status +
								'&failedQty=' + this.jobContent.failedQty +
								'&crackQty=' + this.jobContent.crackQty
						});
					}

				} else {

					//不合格数量>0
					//下一步动作
					if (this.nextAction == null) {
						this.showMessage("请选择下一步动作")
						return
					}
					
					//全不合格
					if (this.nextAction == "ALL_NOK") {
						//提交
						this.jobContent.subList.forEach(res => {
							res.handleQty = res.qty
							res.failedQty = res.qty;
							res.notPassedQty = res.failedQty;
							res.crackQty = 0;
							res.goodQty = 0;
							res.failedReason = "";
							res.photos = ""
							res.inspectResult = ""
							res.toInventoryStatus = "NOK"
							res.inspectUser = this.$store.state.user.id
						})
						this.jobContent.nextAction = this.nextAction;
						this.jobContent.goodQty = 0
						this.submitJob(this.jobContent)
						//部分合格
					} else if (this.nextAction == "PARTIAL_OK") {
						//进入下一页面
						uni.navigateTo({
							url: './inspectDetail?id=' + this.id +
								'&status=' + this.status +
								'&failedQty=' + this.jobContent.failedQty +
								'&crackQty=' + this.jobContent.crackQty
						});
						//全检 生成全检任务
					} else if (this.nextAction == "FULL_INSPECT") {
						//提交
						this.jobContent.subList.forEach(res => {
							res.handleQty = res.qty
							res.failedQty = 0;
							res.notPassedQty = res.failedQty;
							res.crackQty = 0;
							res.goodQty = res.qty;
							res.failedReason = "";
							res.photos = ""
							res.inspectResult = ""
							res.toInventoryStatus = "OK"
							res.inspectUser = this.$store.state.user.id
						})
						this.jobContent.goodQty = this.jobContent.receiveQty
						this.jobContent.nextAction = this.nextAction;
						this.submitJob(this.jobContent)
					}

				}

			},
			submitJob(params) {
				uni.showLoading({
					title: "提交中....",
					mask: true
				});
				console.log("提交参数", JSON.stringify(params));
				inspectJobSubmit(params).then(res => {
					uni.hideLoading()
					if (res.data) {
						var hint="";
						if(this.jobContent.nextAction=="FULL_INSPECT"){
							hint ="提交成功<br>生成到货检验任务<br>" + res.data
						}else {
							hint ="提交成功<br>生成到货检验记录<br>" + res.data
						}
						this.showCommitSuccessMessage(hint)
					} else {
						this.showErrorMessage("提交失败[" + res.msg + "]")
					}

				}).catch(error => {
					uni.hideLoading()
					this.showErrorMessage(error)
				})
			},
			showCommitSuccessMessage(hint) {
				this.$refs.comMessage.showSuccessMessage(hint, res => {
					navigateBack(1)
				})
			},
			showErrorMessage(message) {
				this.$refs.comMessage.showErrorMessage(message, res => {
					if (res) {}
				});
			},

		}
	}
</script>

<style>
</style>
