<template>
	<view>
		<view style="flex-direction: row;flex-direction: row;display: flex;padding: 2px;margin-top: 5px;">
			<text style="align-self: center;font-size: 35rpx;color: darkslategray;">交验单号：</text>
			<text style="align-self: center;font-size: 35rpx;word-wrap: break-word;width: 200px;font-weight: bold;">{{deliverNo}}</text>
		</view>
		<view style="flex-direction: row;flex-direction: row;display: flex;padding: 2px;">
			<text style="align-self: center;font-size: 35rpx;color: darkslategray;">物料号：</text>
			<text style="align-self: center;font-size: 35rpx;word-wrap: break-word;width: 200px;font-weight: bold;">{{SapDeliverRecordSumMd.materialCode}}</text>
		</view>
		<view style="flex-direction: row;flex-direction: row;display: flex;padding: 2px;">
			<text style="align-self: center;font-size: 35rpx;color: darkslategray;">物料名称：</text>
			<text style="align-self: center;font-size: 35rpx;word-wrap: break-word;width: 200px;font-weight: bold;">{{SapDeliverRecordSumMd.materialName}}</text>
		</view>
		<view style="flex-direction: row;flex-direction: row;display: flex;padding: 2px;">
			<text style="align-self: center;font-size: 35rpx;color: darkslategray;width: 260rpx;">规格型号/图号：</text>
			<input class="uni-input" style="margin-right: 5px;" v-model="materialSpecCode" :focus="foucusIndex === 0" @confirm="foucusChange0" placeholder="请输规格型号/图号" />
		</view>
		<view style="flex-direction: row;flex-direction: row;display: flex;padding: 1px;">
			<text style="align-self: center;font-size: 35rpx;color: darkslategray;">交验单数量：</text>
			<text style="align-self: center;font-size: 40rpx;font-weight: bold;">{{SapDeliverRecordSumMd.sumQty}}</text>
		</view>
		<view style="flex-direction: row;flex-direction: row;display: flex;padding: 1px;">
			<text style="align-self: center;font-size: 35rpx;color: darkslategray;">已质检数量：</text>
			<text style="align-self: center;font-size: 40rpx;font-weight: bold;color: blue;">{{SapDeliverRecordSumMd.sumCheckQty}}</text>
		</view>
		<view style="flex-direction: row;flex-direction: row;display: flex;padding: 2px; margin-top: 5px;">
			<text style="align-self: center;font-size: 35rpx;color: darkslategray;width: 260rpx;">BN/SN码：</text>
			<input class="uni-input" style="margin-right: 5px;" v-model="bnSnCode" :focus="foucusIndex === 1" @confirm="foucusChange1" placeholder="请输BN/SN码" />
		</view>
		<view style="flex-direction: row;flex-direction: row;display: flex;padding: 2px; margin-top: 5px;">
			<text style="align-self: center;font-size: 35rpx;color: darkslategray;width: 260rpx;">合格数量：</text>
			<input class="uni-input" type="number" style="margin-right: 5px;" v-model="checkOkQty" :focus="foucusIndex === 2" @confirm="foucusChange2" placeholder="请输合格数量" />
		</view>
		<button type="primary" style="margin-top: 15px;" @click="submitResult">质检结果提交</button>
		<button type="primary" style="margin-top: 8px;" @click="resetCondition">重   置</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				SapDeliverRecordSumMd: {},
				bnSnCode: '',
				checkOkQty: '',
				foucusIndex: 0,
				materialSpecCode: '',
				deliverNo: '',
			}
		},
		onUnload() {
			
		},
		onLoad(option) {
			this.deliverNo = option.deliverNo;
		},
		methods: {
			async submitResult() {
				// let that = this;
				if(this.isEmptyStr(this.materialSpecCode)) {
					uni.showToast({
						title: '规格型号/图号 不能为空！',
						icon: 'none',
						duration: 1000,
						mask:true,
					})
					return
				}
				if(this.isEmptyStr(this.bnSnCode)) {
					uni.showToast({
						title: 'BN/SN码不能为空！',
						icon: 'none',
						duration: 1000,
						mask:true,
					})
					return
				}
				if(Number(this.checkOkQty) <= 0) {
					uni.showToast({
						title: '未输入质检合格数量！',
						icon: 'none',
						duration: 1000,
						mask:true,
					})
					return
				}
				if((Number(this.checkOkQty) + this.SapDeliverRecordSumMd.sumCheckQty) > this.SapDeliverRecordSumMd.qty) {
					uni.showToast({
						title: '质检合格数量录入错误，数量不能大于未录入数！',
						icon: 'none',
						duration: 1000,
						mask:true,
					})
					return
				}
				let tmpMd = {};
				tmpMd.DeliverNo = this.deliverNo;
				tmpMd.MaterialSpec = this.materialSpecCode;
				tmpMd.bnSnCode = this.bnSnCode;
				tmpMd.checkOkQty = this.checkOkQty;
				let parm = {
					url : '/business/QaInspectRecord/addSapDeliverDtlInspectResult',
					data: tmpMd,
					method: 'POST',
					isHaveToken : true
				}
				let retData = await getApp().RequestData(parm)
				if(retData.isSuccess) {
					if(retData.data.code == 200) {
						let parm1 = {
							url : '/business/SapDeliverRecord/GetSapDeliverIsAllChecked',
							data: {},
							method: 'GET',
							isHaveToken : true
						}
						let retData1 = await getApp().RequestData(parm1)
						if(retData1.data.data)
						{
							uni.navigateBack({
								url: '/pages/Inspect/inspect01',
								success: () => {
									uni.$emit('QueryInspect01',null);
								}
							});
						}
						else
						{
							this.resetCondition()
						}
					}
					else {
						uni.showToast({
							title: '提交数据失败！' + retData.data.msg,
							icon: 'none',
							duration: 1000,
							mask:true,
						})
					}
				}
				else {
					uni.showToast({
						title: '接口异常！' + retData.retMsg,
						icon: 'none',
						duration: 1000,
						mask:true,
					})
				}
			},
			isEmptyStr(s) {
				if (s == undefined || s === '') {
					return true
				}
				return false
			},
			async foucusChange0(event) {
				let str = event.detail.value
				let parm = {
					url : '/business/SapDeliverRecord/GetSapDeliverRecordSum?materialSpecCode=' + this.materialSpecCode + '&deliverNo=' + this.deliverNo,
					data: { },
					method: 'POST',
					isHaveToken : true
				}
				let retData = await getApp().RequestData(parm)
				// console.log(retData)
				if(retData.isSuccess) {
					if(retData.data.code == 200) {
						let tmpMd = retData.data.data
						if(tmpMd.sumQty <= tmpMd.sumCheckQty) {
							let tmpMaterialSpecCode = this.materialSpecCode
							uni.showToast({
								title: `物料号：【${tmpMaterialSpecCode}】\r\n已全部质检完成！！`,
								icon: 'none',
								duration: 1500,
								mask:true,
								success: () => {
									this.materialSpecCode = ''
									this.foucusIndex = 3;
									setTimeout(() => {
										this.foucusIndex = 0;
									}, 100)
								}
							})
						}
						else {
							this.SapDeliverRecordSumMd = retData.data.data
							this.foucusIndex = 1
						}
					}
					else {
						uni.showToast({
							title: '获取交验单明细数据失败！\r\n' + retData.data.msg,
							icon: 'none',
							duration: 1500,
							mask:true,
							success: () => {
								this.materialSpecCode = ''
								this.foucusIndex = 3;
								setTimeout(() => {
									this.foucusIndex = 0;
								}, 100)
							}
						})
					}
				}
				else {
					uni.showToast({
						title: '接口异常！' + retData.retMsg,
						icon: 'none',
						duration: 1500,
						mask:true,
						success: () => {
							this.materialSpecCode = ''
							this.foucusIndex = 3;
							setTimeout(() => {
								this.foucusIndex = 0;
							}, 100)
						}
					})
				}
			},
			foucusChange1(event) {
				let str = event.detail.value
				if(str.length !== 0) {
					this.foucusIndex = 2
				}
			},
			foucusChange2(event) {
				let str = event.detail.value
				if(str.length !== 0) {
					this.foucusIndex = 3
				}
			},
			resetCondition() {
				this.materialSpecCode = ''
				this.SapDeliverRecordSumMd = {}
				this.bnSnCode = ''
				this.checkOkQty = ''
			    this.foucusIndex = 3;
				setTimeout(() => {
				   this.foucusIndex = 0;
				}, 100)
			}
		}
	}
</script>

<style lang="scss">
	@import '@/common/uni-nvue.css';
</style>