<template>
	<view>
		<uni-popup ref="popupqtyedit" type="center">
			<view class="uni-popup-view" style="display: flex;flex-direction: column; width: 500rpx;">
				<view
					style="display: flex;  justify-content: center; align-items: center; padding: 10rpx;flex-direction: column;">
					<view style="padding: 10rpx; font-weight: bold; font-size: 35rpx;">{{title}}</view>
					<view class="" style="width: 100%; background-color: black;height: 2rpx;">
					</view>
				</view>
				<view style="padding: 6rpx;margin-left: 15rpx;font-size: 32rpx;">箱码 : {{dataContent.recommendPackingCode}}</view>
				<view style="padding: 6rpx;margin-left: 15rpx;font-size: 32rpx;">物料号 : {{dataContent.itemCode}}</view>
				<view style="padding: 6rpx;margin-left: 15rpx;font-size: 32rpx;">名称 : {{dataContent.itemName}}</view>
				<view style="padding: 6rpx;margin-left: 15rpx;font-size: 32rpx;">描述 : {{dataContent.itemDesc1}}</view>
				<view style="display: flex;  justify-content: center;align-items: center; padding: 6rpx;margin-top: 10rpx;">
					<com-number-box v-model="value" :max="99999999" :min="0" style='margin: 0px 5px' />
					<view class="">
						({{dataContent.uom}})
					</view>
				</view>
				<view class="uni-popup-button-box">
					<button @click="cancle" class="save-button" style="width: 220rpx;margin-top: 10rpx;margin-right: 10rpx; " type="default">取消</button>
					<button @click="submit" class="save-button" style="width: 220rpx;margin-top: 10rpx;margin-left: 10rpx; " type="primary">确定</button>

				</view>
			</view>
			<comMessage ref="refMessage" ></comMessage>
		</uni-popup>
		
	</view>
</template>

<script>
	import {
		showErrorMsg,
		getCurrDateTime
	} from '@/common/basic.js';
	import {
		generateNewPacking,
		unPacking,
		getInventoryLabel,
		unPackingByPurchase,
		unPackingByIssue
	} from '@/api/index.js';
	import comMessage from '@/mycomponents/common/comMessage.vue'
	import comNumberBox from '@/mycomponents/common/comNumberBox.vue'
	export default {
		components: {
			comMessage,
			comNumberBox
		},
		props: {
			title: {
				type: String,
				default: ""
			}
		},
		data() {
			return {
				value: 0,
				defaultValue: 0,
				vaildMaxValue: {
					type: Boolean,
					default: true
				},
				//允许小于等于0
				allowZero: {
					type: Boolean,
					default: true
				},
				dataContent: {},
				jobContent:{},
				jobType:""
			}
		},
		created() {

		},
		methods: {
			openPopup(qty) {
				this.value = qty;
				this.defaultValue = qty;
				this.$refs['popupqtyedit'].open("center");
			},
			//vaild,提交是是否校验数量
			openPopup1(qty, vaild) {
				this.value = qty;
				this.defaultValue = qty;
				this.vaildMaxValue = vaild;
				this.$refs['popupqtyedit'].open("center");
			},
			//vaildqty校验的数量,提交时与输入的值做比较
			openPopup2(qty, vaildqty) {
				this.value = qty;
				this.defaultValue = vaildqty;
				this.vaildMaxValue = true;
				this.$refs['popupqtyedit'].open("center");
			},
			//qty当前数量，vaildqty校验的最大数量 , isZero数量是否允许为0
			openPopup3(jobContent,item, qty, vaildqty, isZero) {
				this.jobContent =jobContent
				this.dataContent = item
				this.jobType =jobContent.jobType
				this.value = qty;
				this.defaultValue = vaildqty;
				this.vaildMaxValue = true;
				this.allowZero = isZero;
				this.$refs['popupqtyedit'].open("center");
			},
			//qty当前数量，不验证最大值，不允许为0
			openPopup4(qty, vaildMaxValue, isZero) {
				this.value = qty;
				this.defaultValue = qty;
				this.vaildMaxValue = vaildMaxValue;
				this.allowZero = isZero;
				this.$refs['popupqtyedit'].open("center");
			},
			cancle(){
				this.$refs['popupqtyedit'].close();
			},
			//确定
			submit() {
				let that = this;
				if (that.value === "") {
					this.showMessage('数量不能为空');
					that.value = that.defaultValue;
				} else {
					let qty = Number(that.value);
					if (that.vaildMaxValue) {
						if (qty > that.defaultValue) {
							that.value = that.defaultValue;
							this.showMessage('数量不能大于' + that.defaultValue);
							
						} else {
							if (!that.allowZero) //不允许0
							{
								if (qty == 0) {
									this.showMessage('数量不能为0');
									that.value = that.value;
								} else if (qty < 0) {
									this.showMessage('数量不能小于0');
									that.value = that.value;
								} else {
									this.getDataInfo(qty)
								}
							} else {
								this.getDataInfo(qty)
							}
						}
					} else {
						if (!that.allowZero) //不允许0
						{
							if (qty == 0) {
								this.showMessage('数量不能为0');
								that.value = that.defaultValue;
							} else if (qty < 0) {
								this.showMessage('数量不能小于0');
								that.value = that.defaultValue;
							} else {
								this.getDataInfo(qty)
							}
						} else {
							this.getDataInfo(qty)
						}
					}
				}
			},
			callBack(qty){
				var data =this.dataContent;
				var params = {
					  fullBarcodeString:data.handledPackingCode ,
					  itemCode: data.itemCode,
					  itemName: data.itemName,
					  itemDesc1:data.itemDesc1 ,
					  itemDesc2: data.itemDesc2,
					  lot: data.handledLot,
					  supplierBatch: data.handledSupplierBatch,
					  arriveDate: data.handledArriveDate,
					  produceDate: data.handledProduceDate,
					  expireDate:data.handledExpireDate ,
					  stdPackQty: data.stdPackQty,
					  uom: data.uom,
					  qty: data.handledQty,
					  labelStatus: 1,
					  recommendLocationCode:data.handledToLocationCode,
					  locationErpCode: data.handledToLocationErpCode,
					  containerCode:data.handledContainerCode ,
					  supplierCode:this.jobContent.supplierCode ,
					  poNumber:data.poNumber,
					  rpNumber:this.jobContent.rpNumber ,
					  asnNumber: this.jobContent.asnNumber,
					  qLevel: "",
					  qualityFile:"" ,
					  prodLine: "",
					  team: "",
					  shift: "",
					  specifications:"" ,
					  supplierName: this.jobContent.supplierName,
					  supplierSimpleName: "",
					  supplierItemCode:"",
					  supplierItemName: "",
					  labelType: 1,
					  planArriveDate: this.jobContent.planArriveDate,
					  remark: data.remark
				}
				
				
				generateNewPacking(qty,params).then(res=>{
					if(res){
						var unPackingParams =this.setUnPackingParams(qty)
						unPacking(unPackingParams).then(res=>{
							this.$emit("getvalue", qty);
							this.$refs['popupqtyedit'].close();
						}).catch(error=>{
							this.showMessage(error.message)
						})
					}else {
						this.showMessage("生成新箱码失败")
					}
				}).catch(error=>{
					this.showMessage(error.message)
				})
				
			},
			
			async getDataInfo(qty){
				try{
					uni.showLoading({
						title:"正在加载中...",
						mask:true
					})
					var labelData = await getInventoryLabel(this.dataContent.recommendPackingCode)
					
					var newPackingCodeParam = this.setNewPackingCodeParam(labelData,qty)
					var newPackingCodeData = await generateNewPacking(1,newPackingCodeParam);
					var unPackingParams;
					if(this.jobType==6){
						var unPackingParamsIssue= this.setUnPackingParamsByIssue(labelData,qty,newPackingCodeData[0].code)
					    var unPackingData = await unPackingByIssue(unPackingParamsIssue,this.dataContent.number,this.dataContent.recommendPackingCode,this.dataContent.recommendQty,newPackingCodeData[0].code,qty)
					}else{
						var unPackingParamsPur= this.setUnPackingParamsByReceipt(labelData,qty,newPackingCodeData[0].code)
					    var unPackingData = await unPackingByPurchase(unPackingParamsPur,this.dataContent.number,this.dataContent.recommendPackingCode,this.dataContent.recommendQty,newPackingCodeData[0].code,qty)
					}
					uni.hideLoading()
					this.showMessage("生成新的箱码【"+newPackingCodeData[0].code+"】请到PC端打印标签")
					this.$emit("update")
				}catch(error){
					uni.hideLoading()
					this.showMessage(error)
				}
			},
			
			setNewPackingCodeParam(labelData,qty){
				var params =labelData;
				params.qty = qty;
				return params;
			},
			
			showMessage(message) {
				this.$refs.refMessage.showMessage(message);
			},
			
			
			setUnPackingParamsByReceipt(labelData,qty,newPackingCode){
				var data = labelData;
				var itemData =this.dataContent;
				var param = {
				  tenantId: data.tenantId,
				  remark: "",
				  extraProperties: {},
				  worker: localStorage.userName_CN ==""?localStorage.userName:localStorage.userName_CN,
				  activeDate: data.arriveDate,
				  confirmed: true,
				  confirmTime: getCurrDateTime(),
				  requestNumber:"",
				  jobNumber: "",
				  type: "SplitPacking",
				  useOnTheWayLocation: false,
				  details: [
				    {
				      remark: itemData.remark,
				      itemCode: itemData.itemCode,
				      itemName:itemData.itemName,
				      itemDesc1: itemData.itemDesc1,
				      itemDesc2:itemData.itemDesc2,
				      uom: itemData.uom,
				      qty: qty,
				      stdPackQty: itemData.stdPackQty,
				      fromPackingCode: itemData.recommendPackingCode,
				      fromContainerCode: itemData.recommendContainerCode,
					  fromLocationCode: itemData.recommendToLocationCode,
					  fromLocationArea: itemData.recommendToLocationArea,
					  fromLocationGroup: itemData.recommendToLocationGroup,
					  fromLocationErpCode: itemData.recommendToLocationErpCode,
					  fromWarehouseCode: itemData.recommendToWarehouseCode,
				      fromLot: itemData.recommendLot,
					  fromStatus: 1,
					  
					  toPackingCode: newPackingCode,
					  toContainerCode: itemData.recommendContainerCode,
					  toLocationCode: itemData.recommendToLocationCode,
					  toLocationArea: itemData.recommendToLocationArea,
					  toLocationGroup: itemData.recommendToLocationGroup,
					  toLocationErpCode: itemData.recommendToLocationErpCode,
					  toWarehouseCode: itemData.recommendToWarehouseCode,
					  toLot: itemData.recommendLot,
					 
					  toStatus: 1,
				      supplierBatch: data.supplierBatch,
				      arriveDate: data.arriveDate,
				      produceDate: data.produceDate,
				      expireDate: data.expireDate,
				     
				      onTheWayLocationCode: "",
				      reason: ""
				    }
				  ]
				}
				
				return param;
			},
			setUnPackingParamsByIssue(labelData,qty,newPackingCode){
				var data = labelData;
				var itemData =this.dataContent;
				var param = {
				  tenantId: data.tenantId,
				  remark: "",
				  extraProperties: {},
				  worker: localStorage.userName_CN ==""?localStorage.userName:localStorage.userName_CN,
				  activeDate: data.arriveDate,
				  confirmed: true,
				  confirmTime: data.arriveDate,
				  requestNumber:"",
				  jobNumber: "",
				  type: "SplitPacking",
				  useOnTheWayLocation: false,
				  details: [
				    {
				      remark: itemData.remark,
				      itemCode: itemData.itemCode,
				      itemName:itemData.itemName,
				      itemDesc1: itemData.itemDesc1,
				      itemDesc2:itemData.itemDesc2,
				      uom: itemData.uom,
				      qty: qty,
				      stdPackQty: itemData.stdPackQty,
				      fromPackingCode: itemData.recommendPackingCode,
				      fromContainerCode: itemData.recommendContainerCode,
					  fromLocationCode: itemData.recommendFromLocationCode,
					  fromLocationArea: itemData.recommendFromLocationArea,
					  fromLocationGroup: itemData.recommendFromLocationGroup,
					  fromLocationErpCode: itemData.recommendFromLocationErpCode,
					  fromWarehouseCode: itemData.recommendFromWarehouseCode,
				      fromLot: itemData.recommendLot,
					  fromStatus: 2,
					  
					  toPackingCode: newPackingCode,
					  toContainerCode: itemData.recommendContainerCode,
					  toLocationCode: itemData.recommendFromLocationCode,
					  toLocationArea: itemData.recommendFromLocationArea,
					  toLocationGroup: itemData.recommendFromLocationGroup,
					  toLocationErpCode: itemData.recommendFromLocationErpCode,
					  toWarehouseCode: itemData.recommendFromWarehouseCode,
					  toLot: itemData.recommendLot,
					 
					  toStatus: 2,
				      supplierBatch: itemData.recommendSupplierBatch,
				      arriveDate: itemData.recommendArriveDate,
				      produceDate: itemData.recommendProduceDate,
				      expireDate: itemData.recommendExpireDate,
				      onTheWayLocationCode: "",
				      reason: ""
				    }
				  ]
				}
				
				return param;
			},
			
		}
	}
</script>

<!-- 	background-color: #fff; -->
<style scoped lang="scss">
	.center {
		flex: auto;
		display: flex;
		flex-direction: column;
		justify-content: center;
		// align-items: center;
	}

	.flex-item {
		width: 50%;
		text-align: center;
	}

	.messageButton {
		border-color: #F8F8F8;
	}

	.messagePopup {
		background-color: #fff;
		border-radius: 5px;
	}

	/deep/ .uni-input-input {
		font-size: 20px;
		height: 46px;
	}
</style>