<template>
	<view>
			<uni-forms ref="form" :modelValue="formData" :rules="rules">
				<uni-row>
					<uni-col :span="24">
						<uni-forms-item label="领料任务单" :labelWidth='90' name="llrwdCode">
							<uni-easyinput suffixIcon="scan" @iconClick="scanBar" v-model="formData.llrwdCode" @confirm="scanBarCode" type="text"  />
						</uni-forms-item>
					</uni-col>
					<uni-col :span="24">
						<uni-forms-item label="拣货单" :labelWidth='90' name="pickCode">
							<uni-easyinput suffixIcon="scan" @confirm="scanBarCode" @iconClick="scanBar4" v-model="formData.pickCode" type="text"  />
						</uni-forms-item>
					</uni-col>
					<uni-col :span="24">
						<uni-forms-item label="物料编码" :labelWidth='90' name="wlCode">
							<uni-easyinput disabled  v-model="formData.wlCode"  type="text"  />
						</uni-forms-item>
					</uni-col>
					<uni-col :span="24">
						<uni-forms-item label="物料名称" :labelWidth='90' name="wlName">
							<uni-easyinput type="text" v-model="formData.wlName" disabled/>
						</uni-forms-item>
					</uni-col>
					<uni-col :span="24">
						<uni-forms-item label="物料批号" :labelWidth='90' name="wlBatchNum">
							<uni-easyinput disabled  type="text" v-model="formData.wlBatchNum"/>	
						</uni-forms-item>
					</uni-col>
					<uni-col :span="24">
						<uni-forms-item label="仓库编码" :labelWidth='90' name="ckCode">
							<uni-easyinput disabled  type="text" v-model="formData.ckCode"/>	
						</uni-forms-item>
					</uni-col>
					
					<uni-col :span="12">
						<uni-forms-item label="原有数量" :labelWidth='90' name="yyNum">
							<uni-easyinput disabled type="number" v-model="formData.yyNum"/>	
						</uni-forms-item>
					</uni-col>
					<uni-col :span="12">
						<uni-forms-item label="拣货数量" :labelWidth='90' name="jhNum">
							<uni-easyinput disabled type="number" v-model="formData.jhNum"/>	
						</uni-forms-item>
					</uni-col>
					<uni-col :span="12">
						<uni-forms-item label="现有数量" :labelWidth='90' name="xyNum">
							<uni-easyinput disabled type="number" v-model="formData.xyNum"/>	
						</uni-forms-item>
					</uni-col>
					<uni-col :span="12">
						<uni-forms-item label="拣货区" :labelWidth='90' name="pickArea">
							<uni-easyinput  type="text" v-model="formData.pickArea"/>	
						</uni-forms-item>
					</uni-col>
					<uni-col :span="12">
						<uni-forms-item label="拣货时间" :labelWidth='90' name="jhDate">
							<view class="uni-list-cell-db">
								<picker style="padding-top: 10px;" mode="date" :value="formData.jhDate" :start="startDate" :end="endDate" @change="bindDateChange">
									<view class="uni-input" v-model="formData.jhDate">{{formData.jhDate}}</view>
								</picker>
							</view>	
						</uni-forms-item>
					</uni-col>
				</uni-row>
			</uni-forms>
		<u-button type="primary" @click="submit">提交</u-button>
	</view>
</template>

<script>
	var arry;
	import {addPick,getTask,updatePick,listPick} from "@/api/wms/pdcMaterial.js";
	import {listMaterial} from "@/api/wms/request.js";
	
	export default{
		mounted() {
			this.test();
		},
		data(){
			const currentDate = this.getDate({
			    format: true
			})
			return{
				formData: {
					llrwdCode : null,
					wlCode : null,
					wlName : null,
					wlBatchNum : null,
					ckCode : null,
					yyNum : null,
					jhNum : null,
					xyNum : null,
					jhDate : currentDate,
					pickCode : null,
				 	pickArea : null
				 },
				rules:{
					llrwdCode:{
						rules:[
							{required:true,errorMessage:'请输入领料任务单!'}
						]
					},
					pickCode:{
						rules:[
							{required:true,errorMessage:'请输入拣货单!'}
						]
					},
					pickArea:{
						rules:[
							{required:true,errorMessage:'请输入拣货区!'}
						]
					},
					jhNum:{
						rules:[
							{required:true,errorMessage:'请输入拣货数量!'}
						]
					},
				}
			}
			
		},
		computed: {
		        startDate() {
		            return this.getDate('start');
		        },
		        endDate() {
		            return this.getDate('end');
		        }
		},
		methods:{
			test(){
				// 查询生产领料单详细
				getTask('00000550').then(async res => {
					console.log(res);
				});
				let q = {
					drawTaskDetailCode : 'DRA200009495'
				}
				listPick(q).then(async res => {
					console.log(res);
				});
			},
			bindDateChange(e){
				this.formData.jhDate = e.detail.value
			},
			getDate(type){
			            const date = new Date();
			            let year = date.getFullYear();
			            let month = date.getMonth() + 1;
			            let day = date.getDate();
			
			            if (type === 'start') {
			                year = year - 60;
			            } else if (type === 'end') {
			                year = year + 2;
			            }
			            month = month > 9 ? month : '0' + month;
			            day = day > 9 ? day : '0' + day;
			            return `${year}-${month}-${day}`;
			},
			scanBarCode(){
				if (this.formData.pickCode && this.formData.llrwdCode) {
					let obj = {
					  drawTaskCode : this.formData.llrwdCode,
					  pickCode : this.formData.pickCode
					}
					console.log(obj);
					listPick(obj).then(async res => {
						console.log(res);
						 if(res.rows.length!=0){
							arry = res.rows[0];
							console.log(arry);
							this.formData.wlBatchNum = res.rows[0].materialBatchNo,
							this.formData.wlName = res.rows[0].materialName,
							this.formData.wlCode = res.rows[0].materialCode,
							this.formData.ckCode = res.rows[0].storageLocationCode,
							this.formData.yyNum = res.rows[0].originNumber,
							this.formData.jhNum = res.rows[0].pickNumber,
							this.formData.xyNum = res.rows[0].originNumber-res.rows[0].pickNumber
							}else{
								this.$modal.msgError("未检索到该拣货信息！");
							}
						});
				}else if(!this.formData.llrwdCode || this.formData.llrwdCode == "" || this.formData.llrwdCode == null){
					this.$modal.msg("请输入领料任务单！");
				}else if(!this.formData.pickCode || this.formData.pickCode == "" || this.formData.pickCode == null){
					this.$modal.msg("请输入拣货单！");
				}
			},
// 			scanBarCode1(){
// 				if(!this.formData.pickCode || this.formData.pickCode == "" || this.formData.pickCode == null){
// 					this.$modal.msgError("请输入拣货单！");
// 				}
			// },
			//领料任务单                
			scanBar() {
			  const _this = this;
			  uni.scanCode({
				scanType: ['barCode','qrCode'],
			  	success: function (res) {
					_this.formData.llrwdCode = res.result;
					_this.scanBarCode();
			  	}
			  });
			},
			//拣货单                 
			scanBar4() {
			  const _this = this;
			  uni.scanCode({
				scanType: ['barCode','qrCode'],
			  	success: function (res) {
					_this.formData.pickCode = res.result;
					_this.scanBarCode(_this.formData.pickCode);
			  	}
			  });
			},
			submit() {
				const _this = this;
				this.$refs.form.validate().then(res => {
					  uni.showModal({
					  	title: '提示',
					  	content: '您确定拣货该物料吗？',
					  	success: function (res) {
					  		if (res.confirm) {
								arry.status = '2';
								arry.inStatus = '2';
								arry.pickArea = _this.formData.pickArea;
								console.log(arry);
								updatePick(arry).then(async res => {
									_this.$modal.msgSuccess("拣货成功!");
									setTimeout(()=>{
										_this.$tab.switchTab("/pages/work/index");
									}, 500);
								});
					  		} else if (res.cancel) {
					  			console.log('用户点击取消');
					  		}
					  	}
					  });
				});
			},
		}
	}
	
</script>

<style>
</style>