<template>
	<el-dialog
		:title="titleMap[mode+type]"
		v-model="visible"
		width="1000px"
		:size="1000"
		destroy-on-close
		@closed="$emit('closed')"
	>
		<el-form
			ref="dialogForm"
			:model="form"
			:rules="rules"
			label-width="80px"
		>
		<el-row>
				<el-form-item label="交接物料">
					<el-button type="primary" @click="addMaterial" v-if="mode!=='show'">手动添加</el-button>
					<el-input  ref="codeInput" 
						id="wm_in"
						placeholder="扫描交接物料"
						v-model="wm_in"  clearable
						:disabled="imputDisable" 
						@change="formWminChange(wm_in)" 
						style="width: 300px;margin-left: 10px;"
					></el-input>
					<!-- <scScanner @scanResult="formWminChange"></scScanner> -->
				</el-form-item>
				<el-form-item label="总计：">
					{{ totalCount }}
				</el-form-item>
				<el-form-item label="原始物料" v-if="mode!='add'&&form.mtype==20" style="margin-left: 20px;">
					<el-col >{{ form.batch }}</el-col>
				</el-form-item>
			</el-row>
			<el-row v-show="addShow">
				<el-col>
					<el-form-item label="交接物料">
						<el-select
							v-model="selectItems"
							placeholder="交接物料"
							filterable
							multiple
							clearable
							@clear="clearSelect"
							@change="materialChange"
							style="width: 100%"
						>
							<el-option
								v-for="item in materialOptions"
								:key="item.id"
								:label="item.batch"
								:value="item.id"
							>
								<span>{{ item.batch }}</span>
								<span v-if="item.material_&&item.material_.process_name">({{ item.material_.process_name }})</span>
								<span v-else>(原料棒)</span>
								<span v-if="item.defect!==null" style="color: red;">{{ item.defect_name }}</span>
								<div style="float: right">
									<span>{{ item.count_canhandover }}</span>
									<span v-if="item.notok_sign_name !== null" style="color: #aaaaaa">
										({{ item.notok_sign_name }})
									</span>
								</div>
							</el-option>
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row v-for="(listItem,$index) in form.handoverb" :key="listItem">
				<el-col :span="10">
					<el-form-item label="交接物料">
						<el-input :placeholder="listItem.batch" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="5">
					<el-form-item label="总数量">
						<span>{{ listItem.count_canhandover }}</span>
					</el-form-item>
				</el-col>
				<el-col :span="7" >
					<el-form-item label="" style="position: relative;">
						<el-link 
							:underline="false"
							style="position: absolute;
							top: 0;
							left: -80px;
							width: 80px;
							height: 32px;
							color: #0052d9;
							line-height: 32px;"
							@click="showbw($index)"
						>交接数量</el-link>
						<el-input-number
							v-model="listItem.count"
							controls-position="right"
							:min="0"
							:step="1"
							:max="Number(listItem.count_canhandover)"
							:disabled="mode==='show'||listItem.tracking==20"
							:step-strictly="true"
							style="width: 100%"
							placeholder="交接数量"
							@change="countChanges($index)"
						>
						</el-input-number>
					</el-form-item>
				</el-col>
				<el-col :span="2" v-if="mode!=='show'">
					<el-button type="danger" icon="el-icon-delete" @click="delMaterial($index)"></el-button>
				</el-col>
			</el-row>
			<el-card shadow="never" v-if="bwVisible"  style="position: relative;">
				<template #header>
					<div style="width: 10px;height: 25px;"></div>
					<el-icon style="position: absolute;right: 10px;top: 10px;font-size: 25px;" @click="bwVisibleClose"><el-icon-circle-close /></el-icon>
				</template>
				<!--  批次物料列表 wprList  -->
				<template v-if="route_code=='shangpaipingmo'">
					<span v-for="item2 in form.handoverb[bwIndex].handoverbw" :key="item2" style="margin-right: 10px;">
						<el-checkbox v-model="item2.checked" :label="item2.number" @change="handleChange(bwIndex)"/>
					</span>
				</template>
				<template v-else>
					<div v-for="(item2,index2) in form.handoverb[bwIndex].handoverbw" :key="item2" style="display: flex;justify-content: space-between;padding-bottom: 10px;padding-left: 10px;">
						<span>{{ item2.number }}</span>
						<el-button @click="deletebw(index2)" type="primary">删除</el-button>
					</div>
				</template>
			</el-card>
			<el-row>
				<el-col :md="12" :sm="24">
					<el-form-item label="交送日期" prop="send_date">
						<el-date-picker
							v-model="form.send_date"
							type="date"
							placeholder="选择时间"
							format="YYYY-MM-DD"
							value-format="YYYY-MM-DD"
							style="width: 100%"
						/>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24">
					<el-form-item label="交送人">
						<el-select
							v-model="form.send_user"
							placeholder="交送人"
							clearable
							filterable
							style="width: 100%"
						>
							<el-option
								v-for="item in userList"
								:key="item.id"
								:label="item.name"
								:value="item.id"
							/>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24" v-if="mtype!==30&&form.handoverb.length>0">
					<el-form-item label="接收工段" prop="recive_mgroup">
						<el-select
							v-model="form.recive_mgroup"
							placeholder="接收工段"
							clearable
							filterable
							style="width: 100%"
							:disabled="type==40||mode!=='add'"
							@change="getUserList2"
						>
							<el-option
								v-for="item in mgroupOptions"
								:key="item.id"
								:label="item.name"
								:value="item.id"
							></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24" v-if="mtype!==30">
					<el-form-item label="接收人">
						<el-select
							v-model="form.recive_user"
							placeholder="接收人"
							clearable
							filterable
							style="width: 100%"
						>
							<el-option
								v-for="item in userList2"
								:key="item.id"
								:label="item.name"
								:value="item.id"
							/>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24" v-if="mtype==30||type==50">
					<el-form-item label="更改批次">	
						<el-switch v-model="change_batch" :disabled="type==50"></el-switch>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24" v-if="type==50">
					<el-form-item label="更改物料">	
						<xtSelect
							:apiObj="apiObj"
							:params="paramsM"
							v-model="form.material_changed"
							v-model:label="form.material_changed_fname"
							style="width:100%"
						>
							<el-table-column label="名称" prop="name"></el-table-column>
							<el-table-column label="工序" prop="process_name"></el-table-column>
							<el-table-column label="规格" prop="specification"></el-table-column>
						</xtSelect>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24">
					<el-form-item label="处理备注">	
						<el-input v-model="form.note" placeholder="处理备注"></el-input>
					</el-form-item>
				</el-col>
				<el-col :md="12" :sm="24" v-if="change_batch">
					<el-form-item label="新批次号" prop="new_batch">	
						<el-input v-model="form.new_batch" placeholder="新批次号" @change="new_batch(form.new_batch)" :disabled="type==50"></el-input>
						<span style="color: red;">请手动录入新批次号，不要用已有批次！</span>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<el-footer v-if="mode!=='show'">
			<el-button type="primary" v-loading="isSaveing" @click="submit">确定</el-button>
			<el-button @click="visible = false">取消</el-button>
		</el-footer>
	</el-dialog>
</template>
<script>
import scanDialog from "./../template/scan.vue";
export default {
	emits: ["success", "closed"],
	props: {
		type: {
			type: Number,
			default: 10,
		},
		mgroupId: {
			type: String,
			default: "",
		},
		mgroupName: {
			type: String,
			default: "",
		},
		tag: {
			type: String,
			default: "done",
		},
		wmItem: {
			type: Object,
			default: () => ({}),
		},
	},
	components: {
		scanDialog
	},
	data() {
		return {
			selectItems:[],
			yseorno: ["是", "否"],
			loading: false,
			mode: "add",
			titleMap: {
				add10: "新增",
				add20: "新增",
				add40: "新增",
				add50: "新增",
				edit10: "编辑",
				edit20: "编辑",
				edit40: "编辑",
				edit50: "编辑",
				show: "查看",
			},
			lists:[],//交接数组
			handle_user: [],
			form: {
				send_date: null,
				send_user: null,
				send_mgroup: null,
				recive_user: null,
				recive_mgroup: null,
				handoverb:[],
				new_batch:''
			},
			rules: {
				new_batch:[
					{
						required: true,
						message: "请输入新批次号",
						trigger: "blur",
					},
				],
				batch: [
					{
						required: true,
						message: "请输入批次号",
						trigger: "blur",
					},
				],
				wm: {
					required: true,
					message: "请选择物料",
					trigger: "blur",
				},
				send_date: [
					{
						required: true,
						message: "请选择送料日期",
						trigger: "blur",
					},
				],
				send_user: [
					{
						required: true,
						message: "请选择交送人",
						trigger: "blur",
					},
				],
				send_mgroup: [
					{
						required: true,
						message: "请选择交送工段",
						trigger: "blur",
					},
				],
				recive_user: [
					{
						required: true,
						message: "请选择接收人",
						trigger: "blur",
					},
				],
				recive_mgroup: [
					{
						required: true,
						message: "请选择接收工段",
						trigger: "blur",
					},
				],
			},
			apiObj:this.$API.mtm.material.list,
			paramsM:{
				type:20,
				is_hidde:true,
			},
			wm_in:"",
			route_code: "",
            materials:[],
			totalCount: 0,
			deptID:'',
			bwIndex:0,
			userList: [],
			userList2: [],
			deptOptions:[],
			mgroupOptions: [],
			materialOptions: [],
			addShow: false,
			visible: false,
			bwVisible:false,
			scanVisible:false,
			isSaveing: false,
			imputDisable:false,
			change_batch:false,
			setFiltersVisible: false,
		};
	},
	mounted() {
		let that  = this;
		that.form.type = that.type;
		if(that.type==50){
			that.form.mtype=30;
		    that.change_batch = true;
		}
		let arr = that.$route.path.split("/");
		that.route_code = arr[2];
		let materialObj = that.$TOOL.data.get("MATERIAL_OBJECT");
		that.materialObj = materialObj;
		that.form.handle_date =that.form.send_date =this.$TOOL.dateFormat2(new Date());
		that.form.send_mgroup = that.mgroupId;
		if(that.wmItem.wm){
			that.form.new_batch = that.wmItem.batch;
			that.form.material_changed = that.wmItem.material;
			that.form.material_changed_fname = that.wmItem.material_name;
		}
		if(that.type==20||that.type==50){
			that.form.recive_mgroup = that.mgroupId;
		}
		// type=10 正常交接   物料都可交接，接收工段有限制
		// type=20 返工交接   不合格品交接，接收工段为该工艺流程中的工段
		// type=30 物料检验   合格品交接，检验部接收
		// type=40 废品入库   不合格品交接，接收工段固定为废品库
		// type=50 改版       灵活无限制
		//获取当前工段/部门交送人
		if(that.type==20&&that.mgroupName=='废品库'){//废品交接
			that.getCkUserList();
		}else{
			that.deptID = that.$TOOL.data.get('gx_deptID');
			that.getUserList();
		}
		//获取交送物料
		if(that.type==10||that.type==50){//正常交接
			that.getMaterial();
		}else if(that.type==20){//返工交接
			if(that.mgroupName=='废品库'){
				that.getMaterialFP();
			}else{
				that.getMaterialOkFangong();
			}
		}else if(that.type==40){//废品入库
		    that.getMaterialNotok();
		}
		that.getMgroupOptions();
	},
	methods: {
		deletebw(index){
			let that = this;
			this.form.handoverb[this.bwIndex].handoverbw.splice(index,1);
			this.form.handoverb[this.bwIndex].count--;
			let totalCount = 0;
			that.form.handoverb.forEach((item)=>{
			    totalCount += item.count;
			})
			that.totalCount = totalCount;
		},
		showbw(index){
			this.bwIndex = index;
			this.bwVisible = true;
		},
		bwVisibleClose(){
			this.bwVisible = false;
		},
		//获取部门列表
		getDeptOptions() {
			this.$API.system.dept.list.req({ page: 0, type: "dept" }).then((res) => {
				this.deptOptions = res;
			});
		},
		//获取工段列表
		getMgroupOptions() {
			let that = this;
			that.$API.mtm.mgroup.list.req({ page: 0}).then((res) => {
				that.mgroupOptions = res;
				if(that.type==40){
					res.forEach(item=>{
						if(item.name=="废品库"){
							that.form.recive_mgroup = item.id;
						}
					})
					that.getCkUserList();//废品库接收人
				}else if(that.type==30){
					this.getUserList3();
				}
			});
		},
		//获取车间物料
		getMaterial() {
			let that = this;
			var req = {
				page: 0,
				tag:that.tag,
				mgroupx:that.mgroupId
			};
			if(that.type!==50){
				req.state = 10;
			}
			that.materialOptions = [];
			this.$API.wpm.wmaterial.list.req(req).then((res) => {
				that.materialOptions = res.filter(item=>{
					return item.count_canhandover>0;
				})
			});
		},
		//获取车间不合格物料
		getMaterialNotok() {
			let that = this;
			that.materialOptions = [];
			var req = {
				mgroupx: that.mgroupId,
				page: 0
			};
			this.$API.wpm.wmaterial.list.req(req).then((res) => {
				that.materialOptions = res.filter(item=>{
					return item.count_canhandover>0;
				})
			});
		},
		//获取废品库物料
		getMaterialFP() {
		    let that = this;
			that.materialOptions = [];
			var obj = {
				page: 0,
				state : 50,
				state_all: 1
			};
			that.$API.wpm.wmaterial.list.req(obj).then((res) => {
				that.materialOptions = res.filter(item=>{
					return item.count_canhandover>0;
				})
			});
		},
		//获取该工段的可返工物料
		getMaterialOkFangong(){
			let that = this;
			that.materialOptions = [];
			var obj = {
				page: 0,
				tag:'done',
				state__in:'20,34',
				mgroupx: that.mgroupId,
			};
			that.$API.wpm.wmaterial.list.req(obj).then((res) => {
				that.materialOptions = res.filter(item=>{
					return item.count_canhandover>0;
				})
			});
		},
		//获取交送工段人员
		getUserList() {
			let that = this;
			let userInfo = that.$TOOL.data.get("USER_INFO");
			this.$API.system.user.list.req({ depts: that.deptID, page: 0 }).then((res) => {
				that.userList = res;
				let arr = res.filter(item=>{
					return item.id==userInfo.id;
				})
				if(arr.length>0){
					if(that.mode=="add"){
						that.form.send_user = userInfo.id;
					}
				}else{
					let obj = {};
					obj.id = userInfo.id;
					obj.name = userInfo.name;
					that.userList.push(obj)
					if(that.mode=="add"){
						that.form.send_user = userInfo.id;
					}
				}
			});
		},
		//获取仓库人员
		getCkUserList() {
			let that = this;
			this.$API.system.user.list.req({ page: 0, posts__code: "inm&check" })
			.then((res) => {
				if(that.type==40){
					that.userList2 = [];
					that.userList2 = res;
				}else if(that.type==20&&that.mgroupName=='废品库'){
					that.userList = [];
					that.userList = res;
				}
			});
		},
		//获取接收工段人员
		getUserList2() {
			let that = this;
			if(that.mode=="add"){
				that.$API.mtm.mgroup.item.req(that.form.recive_mgroup).then((res) => {
					this.$API.system.user.list.req({ depts: res.belong_dept, page: 0 }).then((res) => {
						that.userList2 = res;
					});
				})
			}else{
				this.mgroupOptions.forEach(item => {
					if(item.id==that.form.recive_mgroup){
						this.$API.system.user.list.req({ depts: item.belong_dept, page: 0 }).then((res) => {
							that.userList2 = res;
						});
					}
				});
			}
		},
		//获取接收部门人员
		getUserList3() {
			let that = this;
			this.$API.system.user.list.req({ depts: that.form.recive_dept, page: 0 }).then((res) => {
				that.userList2 = res;
			});
		},
		addMaterial(){
			this.addShow = true;
		},
		delMaterial(index){
			this.selectItems.splice(index,1);
			this.form.handoverb.splice(index,1);
			this.countChange();
		},
		//显示
		open(mode = "add",data,mtype) {
			this.mode = mode;
			this.mtype = mtype;
			if(mtype==30){
				this.change_batch = true;
			}
			this.form.handoverb = data?data:[];
			this.visible = true;
			return this;
		},
		clearSelect(){
			let that = this;
			that.totalCount = 0;                                                                                                                                                                                 
			that.form.handoverb = [];
		},
		 materialChange() {
			let that = this;
			that.form.new_batch = "";
			let totalCount = 0,data = [];
			if(that.selectItems.length>0){
				data = that.materialOptions.filter((item) => {
					return that.selectItems.indexOf(item.id)>-1;
				});
				if(that.type==50){
					that.form.new_batch = data[0].batch+'-G';
				}
				let materialId = data[0].material;
				if(that.type==50){}else{
					let params = {
						material: materialId,
						type: that.type,
					};
					that.$API.wpm.handover.mgroups.req(params).then((res) => {
						that.mgroupOptions = res;
						if(res.length==1){
							that.form.recive_mgroup = res[0].id;
							that.getUserList2();
						}
					})
				}
				data.forEach((item,index)=>{
					item.wm = item.id;
					item.count = item.count_canhandover;
					totalCount += Number(item.count_canhandover);
					item.tracking = item.material_.tracking;
					if(item.material_.process_name){
						item.process = item.material_.process_name;
					}
					item.handoverbw = [];
					that.getWprList(item.id,index);
				})
			}
			that.totalCount = totalCount;
			that.form.handoverb = data;
		},
		getWprList(id,index){
			let that = this;
			let handoverbw = [];
		    that.$API.wpm.wpr.list.req({wm:id,page:0,can_use:'yes'}).then((res) => {
				res.forEach(wpritem=>{
					let obj = {};
					obj.wpr = wpritem.id;
					obj.checked = true;
					obj.number = wpritem.number;
					handoverbw.push(obj);
				})
				that.form.handoverb[index].handoverbw = handoverbw;
			})
		},
		handleChange(index){
			let trueCount = this.form.handoverb[index].handoverbw.filter(item => item.checked === true).length;
			this.form.handoverb[index].count = trueCount;
			this.countChange();
		},
		countChange(){
			let that = this;
			let totalCount = 0;
			if(this.form.handoverb&&this.form.handoverb.length&&this.form.handoverb.length>0){
				this.form.handoverb.forEach(item=>{
					totalCount += Number(item.count);
				})
				that.totalCount = totalCount;
			}else{
				that.totalCount = 0;
			}
		},
		countChanges(index){
			if(this.form.handoverb[index]){}else{}
			if(this.form.handoverb.length>0){
				this.form.handoverb.forEach(item=>{
					totalCount += item.count;
				})
				that.totalCount = totalCount;
			}
		},
		//提交
		submit() {
			let that = this;
			that.$refs.dialogForm.validate(async (valid) => {	
				if (valid) {
					if(that.mtype==30){
						that.form.recive_mgroup = that.form.send_mgroup;
					}
					that.form.mtype=that.mtype;
					if(that.route_code=='shangpaipingmo'){
						that.form.handoverb.forEach(item=>{
							let handoverbw = [];
							item.handoverbw.forEach(item1=>{
								if(item1.checked){
									handoverbw.push(item1);
								}
							})
							item.handoverbw = handoverbw;
						})
					}
					//合批、分批、报废，这三个交接操作直接创建并提交
					if(that.mtype==20||that.mtype==30||that.type==40){
						that.$API.wpm.handover.createsubmit.req(that.form).then((res) => {
							that.isSaveing = false;
								that.$emit("success");
								that.visible = false;
								that.$message.success("操作成功");
						}).catch((err) => {
							that.isSaveing = false;
							return err;
						});	
					}else{
						if (that.mode == "add") {
							that.$API.wpm.handover.create.req(that.form).then((res) => {
								that.isSaveing = false;
								that.$emit("success");
								that.visible = false;
								that.$message.success("操作成功");
							}).catch((err) => {
								that.isSaveing = false;
								return err;
							});
						} else if (that.mode == "edit") {
							that.$API.wpm.handover.update.req(that.form.id, that.form).then((res) => {
								that.isSaveing = false;
								that.$emit("success", that.form, that.mode);
								that.visible = false;
								that.$message.success("操作成功");
							}).catch((err) => {
								that.isSaveing = false;
								return err;
							});
						}
					}
				}
			});
		},
		//表单注入数据
		setData(data) {
			let that = this;
			this.totalCount = data.count?data.count:data.handoverb.count;
			Object.assign(this.form, data);
			this.$API.system.user.list.req({ depts: data.send_dept, page: 0 }).then((res) => {
				that.userList = res;
			});
			this.$API.system.user.list.req({ depts: data.recive_dept, page: 0 }).then((res) => {
				that.userList2 = res;
			});
			setTimeout(() => {
				let arr =that.userList2.filter((item) => {
					return item.id == data.recive_user
				})
				if(arr.length>0){}else{
					let obj = {};
					obj.id = data.recive_user;
					obj.name = data.recive_user_name;
					that.userList2.push(obj);
				}
			},500)
			if(data.new_batch!==''&&data.new_batch!==undefined&&data.new_batch!==null){
				this.change_batch = true;
			}
		},
		//设置过滤项
		setFilters(filters) {
			this.selectionFilters = filters;
			this.setFiltersVisible = true;
		},
		//扫描
		scanCode(){
			let that = this;
			that.scanVisible = true;
		},
		async formWminChange(data){
			this.imputDisable = true;
			await this.getInputText(data);
			this.imputDisable = false;
			this.$refs.codeInput.focus();
		},
		
		async getInputText(data){
			if(data==''){
				return;
			}
			let that = this;
			that.wm_in = '';
			if(data.indexOf('#')>-1){
				let id = data.split('#')[1];
				let res = await this.$API.cm.labelmat.item.req(id);
				let arr = [];
				if(that.form.handoverb&&that.form.handoverb.length>0){
					that.form.handoverb.forEach((item) => {
						if(item.batch == res.batch&&item.state==res.state){
							arr.push(item);
						}
					})
				}
				if(arr.length>0){
					that.$message.error("该批次已存在")
				}else{
					let hasArr = [];
					that.materialOptions.forEach((item) => {
						if(item.batch == res.batch&&item.state==res.state&&item.defect==res.defect){
							if(that.type==50){
								that.form.new_batch = res.batch+'-G';
							}
							hasArr.push(item);
							let params = {material: item.material,type: that.type};
							that.$API.wpm.handover.mgroups.req(params).then((res0) => {
								that.mgroupOptions = res0;
								if(res0.length==1){
									that.form.recive_mgroup = res0[0].id;
									that.getUserList2();
								}
							})
							let obj = {};
							obj.wm = item.id;
							obj.batch = item.batch;
							if(item.material_.process_name){
								obj.process = item.material_.process_name;
							}
							obj.count_cando = item.count_canhandover;
							obj.count = item.count_canhandover;
							obj.state = res.state;
							obj.defect = res.defect;
							obj.handoverbw = [];
							that.$API.wpm.wpr.list.req({wm:item.id,page:0}).then((res) => {
								let handoverbw = [];
								let list = res;
								list.forEach(item1=>{
									let obj1 = {};
									obj1.wpr = item1.id;
									obj1.number = item1.number;
									handoverbw.push(obj1);
								})
								obj.handoverbw = handoverbw;
								that.form.handoverb.push(obj);
							})
						}
					})
					if(hasArr.length>0){}else{
						that.$message.error("该批次不存在或没有可交接数量")
					}
				}
			}else{//wpr的number
				let arr = [];
				arr = that.materialOptions.filter((item) => {
					return item.batch == data;
				})
				if(arr.length>0){//有批次
					console.log('批次处理');
					let arr2 = [];
					if(that.form.handoverb&&that.form.handoverb.length>0){
						arr2 = that.form.handoverb.filter((item) => {
							return item.batch == data;
						})
					}
					if(arr2.length>0){
						that.$message.error("该批次已存在")
					}else{
						if(that.type==50){
							that.form.new_batch = arr[0].batch+'-G';
						}
						let params = {material: arr[0].material,type: that.type};
						that.$API.wpm.handover.mgroups.req(params).then((res0) => {
							that.mgroupOptions = res0;
						})
						let obj2 = {};
						obj2.wm = arr[0].id;
						obj2.batch = arr[0].batch;
						if(arr[0].material_.process_name){
							obj2.process = arr[0].material_.process_name;
						}
						obj2.count_cando = arr[0].count_canhandover;
						obj2.count = arr[0].count_canhandover;
						that.form.handoverb.push(obj2);
					}
				}else{//单件
					console.log('单件处理');
					let res = await that.$API.wpm.wpr.list.req({number:data,page:0});
					if(res.length>0){
						let indexs = 0,arrs =[];
						if( that.form.handoverb&&that.form.handoverb.length>0){
							that.form.handoverb.forEach((item,index) => {
								if(item.wm == res[0].wm){
									indexs = index;
									arrs.push(item);
								}
							})
						}
						//handoverb里有这个物料批次
						if(arrs.length>0){
							//判断是否有wpr，若无将wpr放进去
							if(that.form.handoverb[indexs].handoverbw&&that.form.handoverb[indexs].handoverbw.length>0){
								let arr1 = [];
								arr1 = that.form.handoverb[indexs].handoverbw.filter((item1) => {
									return item1.wpr == res[0].id;
								})
								if(arr1.length>0){
									that.$message.error("该物料已存在");
								}else{
									let obj1 = {};
									obj1.wpr = res[0].id;
									obj1.number = res[0].number;
									that.form.handoverb[indexs].count+=1;
									that.form.handoverb[indexs].handoverbw.push(obj1);
								}
							}else{
								that.form.handoverb[indexs].handoverbw = [];
								let obj1 = {};
								obj1.wpr = res[0].id;
								obj1.number = res[0].number;
								that.form.handoverb[indexs].count=1;
								that.form.handoverb[indexs].handoverbw.push(obj1);
							}
						}else{//handoverb里没有有这个物料批次
							that.materialOptions.forEach((item) => {
								if(item.id == res[0].wm){
									if(that.type==50){
										that.form.new_batch = item.batch+'-G';
									}
									let params = {material: item.material,type: that.type};
										that.$API.wpm.handover.mgroups.req(params).then((res1) => {
										that.mgroupOptions = res1;
									})
									let obj2 = {};
									obj2.wm = item.id;
									obj2.batch = item.batch;
									if(item.material_.process_name){
										obj2.process = item.material_.process_name;
									}
									obj2.count_cando = item.count_canhandover;
									obj2.handoverbw = [];
									obj2.count = 1;
									let obj3 = {};
									obj3.wpr = res[0].id;
									obj3.number = res[0].number;
									obj2.handoverbw.push(obj3);
									that.form.handoverb.push(obj2);
								}	
							})
						}
					}else{
						that.$message.error("该批次不存在")
					}
				}
			}
			setTimeout(() => {
				this.countChange();
			}, 500);
		},
		new_batch(){
			let that = this;
			if(that.form.new_batch.indexOf('#')>-1){
				that.form.new_batch = '';
				that.$message.error("新批次号不能用以有批次需要手动录入新批次号！");
			}

		},
	},
};
</script>
<style scoped>
.formTitle {
	margin-bottom: 10px;
	font-weight: 600;
}
.total-count{
	width: 100%;
    text-align: right;
    height: 50px;
    line-height: 50px;
}
</style>
