<!-- 新增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="关联任务">
						<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="主要批次">
						<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="visibleDrawer = 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: "",
		},
	},
	emits: ["success", "closed"],
	data() {
		return {
			loading: false,
			mode: "add",
			titleMap: {
				add: "新增",
				edit: "编辑",
			},
			//表单数据
			form: defaultForm,
			//验证规则
			rules: {
				wm_in: [
					{
						required: true,
						message: "请填写批次号",
						trigger: "blur",
					},
				]
			},
			wm_in:'',
			options: [],
			mlogbInOptions:[],
			materialOptions: [],
			mgroup_code:'',
			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.getMtask();
		this.getMaterial();
		this.getParentList();
	},
	methods: {
		open() {
			this.visible = true;
		},
		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;
			this.$API.wpm.wmaterial.list.req({
				mtaskx: that.form.mtask,
				mgroupx: that.mgroup,
				route: that.routeId,
				page: 0,
			}).then((res) => {
				that.materialOptions = res;
			});
		},
		//扫描后处理方法
		formWminChange(wm_in){
			// wm_in:扫码获取的内容
			let that = this;
			let arr = that.materialOptions.filter((item) => {
				return item.batch == wm_in;
			});
			if (arr.length > 0) {
				that.form.batch = wm_in;
				that.form.wm_in = arr[0].id;
				that.form.count_use = arr[0].count;
				// if(that.tracking==10){}else{
				// 	that.submit();//提交
				// }
			}else{
				that.wm_in = '';
				that.$message.error("批次号不存在");
			}
		},
		//表单提交方法
		submit() {
			let that = this;
			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;
				that.$emit("closed");
				that.visible = false;
			}).catch((err) => {});
		},
		//设置过滤项
		setFilters(filters) {
			this.selectionFilters = filters;
			this.setFiltersVisible = true;
		},
	},
};
</script>

<style></style>
