<!-- 新增mlogbIn -->
<template>
	<el-dialog
		title="添加日志详情"
		v-model="visible"
		:size="1000"
		destroy-on-close
		@closed="$emit('closed')"
	>
		<el-container v-loading="loading">
			<el-main style="padding: 0 20px 20px 20px">
				<el-form
					ref="dialogForm"
					:model="form"
					:rules="rules"
					label-position="right"
					label-width="80px"
					style="padding: 0 10px"
				>
					<el-form-item label="关联任务" v-if="!isfix">
						<el-select
							v-model="form.mtask"
							placeholder="关联任务"
							clearable
							style="width: 100%"
							@change="getMaterial"
						>
							<el-option
								v-for="item in options"
								:key="item.id"
								:label="item.number"
								:value="item.id"
							>
							</el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="批次号" prop="wm_in">
						<el-input  ref="codeInput" v-model="wm_in" clearable @change="formWminChange(wm_in)"></el-input>
					</el-form-item>
					<el-form-item label="使用数量" prop="count_use">
						<el-input-number  ref="codeInput" v-model="form.count_use" clearable></el-input-number>
					</el-form-item>
					<el-form-item label="主要批次" v-if="!isfix">
						<el-select
							v-model="form.parent"
							placeholder="主要批次"
							clearable
							style="width: 100%"
						>
							<el-option
								v-for="item in mlogbInOptions"
								:key="item.id"
								:label="item.batch"
								:value="item.id"
							>
							</el-option>
						</el-select>
					</el-form-item>
				</el-form>
				<el-footer>
					<el-button type="primary" :loading="isSaveing" @click="submit">提交</el-button>
					<el-button @click="visible = false">取消</el-button>
				</el-footer>
			</el-main>
		</el-container>
	</el-dialog>
</template>

<script>
const defaultForm = {
	mlog: "",
	mtask: "",
	batch: "",
	wm_in: "",
	count_use: 1,
	note:'',
	parent: "",
};
export default {
	props: {
		mlog: {
			type: String,
			default: "",
		},
		mgroup: {
			type: String,
			default: "",
		},
		tracking:{
			type: Number,
			default: 10,
		},
		routeId:{
			type: String,
			default: "",
		},
		materialIn: {
			type: String,
			default: "",
		},
		isfix:{
			type: Boolean,
			default: false,
		}
	},
	emits: ["success", "closed"],
	data() {
		return {
			loading: false,
			mode: "add",
			titleMap: {
				add: "新增",
				edit: "编辑",
			},
			//表单数据
			form: defaultForm,
			//验证规则
			rules: {
				wm_in: [
					{
						required: true,
						message: "请填写批次号",
						trigger: "blur",
					},
				]
			},
			wm_in:'',
			mlogbIns:[],
			options: [],
			mlogbInOptions:[],
			materialOptions: [],
			mgroup_code:'',
			mlogbInId:'',
			mlogbInCount:0,
			hasMlogbIn:false,
			visible: false,
			isSaveing: false,
			setFiltersVisible: false,
			params: {
				page: 0,
				mlog: "",
				material_in__isnull: 0,
			},
		};
	},
	mounted() {
		this.form.mlog = this.mlog;
		this.params.mlog = this.mlog;
		let arr = this.$route.path.split("/");
		this.mgroup_code = arr[2];
		this.getMlogbIn();
		this.getMtask();
		this.getMaterial();
		this.getParentList();
	},
	methods: {
		open() {
			this.visible = true;
		},
		getMlogbIn(){
			let that = this;
			let params = {};
			params.page= 0;
			params.mlog= that.mlog;
			params.material_in__isnull=0;
			this.$API.wpm.mlogb.list.req(params).then((res) => {
			    that.mlogbIns = res;
			})
		},
		getParentList(){
			let that = this;
			that.$API.wpm.mlogb.list.req(that.params).then((res) => {
				if(res.length>0){
					that.mlogbInOptions = res.filter((item)=>{
						return item.parent==null;
					})
					that.form.parent = that.mlogbInOptions[0].id;
				}else{
					that.form.parent = '';
				}
			})
		},
		//获取任务列表
		getMtask() {
			let that = this;
			this.$API.pm.mtask.list.req({ page: 0, mgroup: that.mgroup, state: 20 }).then((res) => {
				that.options = res;
			});
		},
		//获取车间物料
		getMaterial() {
			let that = this;
			let obj = {};
			obj.mtaskx =that.form.mtask;
			obj.mgroupx =that.mgroup;
			obj.route =that.routeId;
			obj.page =0;
			if(that.isfix){
				obj.state =30;
			}
			this.$API.wpm.wmaterial.list.req(obj).then((res) => {
				that.materialOptions = res;
			});
		},
		//扫描后处理方法
		formWminChange(code){
			let that = this,codeId='',arr=[];
			if(code.indexOf("#")>-1){
				let arrs = code.split("#");
				codeId = arrs[1];
				that.scanKyes = arrs[0];
				this.$API.cm.labelmat.item.req(codeId).then((res) => {
					if(res){
						arr = that.materialOptions.filter((item) => {
							return item.batch == res.batch&&item.state==res.state;
						})
						if (arr.length > 0) {
							that.form.batch = arr[0].batch;
							that.form.wm_in = arr[0].id;
							that.form.count_use = arr[0].count;
							that.wm_in = arr[0].batch;
						}else{
							that.wm_in = '';
							that.$message.error("批次号不存在");
						}
					}else{
						that.wm_in = '';
					}
				}).catch((err) => {
					that.wm_in = '';
					that.$message.error("批次号不存在");
				});
			}else{
				that.$API.wpm.wpr.list.req({number:code,page:0}).then((res) => {
				    if(res.length>0){
						let bwitem = {};
						bwitem.mlogb="";
						bwitem.number=res[0].number;
						bwitem.wpr=res[0].id;
						let arr = that.materialOptions.filter((item) => {
							return item.id == res[0].wm;
						})
						if(arr.length>0){ //车间存在该批次
							let batch = arr[0].batch;
							that.wm_in = arr[0].batch;
							that.mlogbInCount = arr[0].count;//该批次数量
							//判断日志中是否存在该批次
							let arr0= that.mlogbIns.filter((item)=>{
								return item.batch == batch;
							})
							if (arr0.length > 0) {
								that.hasMlogbIn = true;
								that.form.count_use = 1;
								bwitem.mlogb = arr0[0].id;
							}else{
								that.form.batch = arr[0].batch;
								that.form.wm_in = arr[0].id;
								that.form.count_use = 1;
								that.wm_in = arr[0].batch;
							}
							that.bwItemForm = bwitem;
						}else{
							that.wm_in = '';
							that.$message.error("批次号不存在");
						}
					}
				})
			}
			
		},
		//表单提交方法
		submit() {
			let that = this;
			if(that.hasMlogbIn){
				that.$API.wpm.mlogbw.create.req(that.bwItemForm).then((res) => {
					that.$message.success("添加成功");
					that.wm_in = '';
					that.form.mtask = '';
					that.form.batch = '';
					that.form.parent = '';
					that.form.count_use = 0;
					that.$emit("closed");
					that.visible = false;
				})	
			}else{
				that.form.mlog = that.mlog;
				that.$API.wpm.mlogb.in.req(that.form).then((res) => {
					that.$message.success("添加成功");
					that.$emit("success");
					that.wm_in = '';
					that.form.mtask = '';
					that.form.batch = '';
					that.form.parent = '';
					that.form.count_use = 0;
					if(that.scanKyes!=='mat'&&that.mlogbInCount>1){
						//当批次数量不为1时
						that.bwItemForm.mlogb = res.id;
						that.$API.wpm.mlogbw.create.req(that.bwItemForm).then((res) => {})
					}
					that.$emit("closed");
					that.visible = false;
				}).catch((err) => {});
			}
		},
		//设置过滤项
		setFilters(filters) {
			this.selectionFilters = filters;
			this.setFiltersVisible = true;
		},
	},
};
</script>

<style></style>
