<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%"
						>
							<el-option
								v-for="item in options"
								:key="item.id"
								:label="item.material_out_name"
								:value="item.id"
							>
								<span>{{ item.material_out_name }}</span>
							</el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="批次号" prop="batch">
						<el-input v-model="form.batch" />
					</el-form-item>
					<el-form-item label="领用数量">
						<el-input-number
							v-model="form.count_use"
							:min="1"
							controls-position="right"
						/>
					</el-form-item>
					<!-- <el-form-item label="备注">
						<el-input v-model="form.note" clearable></el-input>
					</el-form-item> -->
				</el-form>
			</el-main>
			<el-footer>
				<el-button type="primary" :loading="isSaveing" @click="submit"
					>保存</el-button
				>
				<el-button @click="visible = false">取消</el-button>
			</el-footer>
		</el-container>
	</el-dialog>
</template>

<script>
const defaultForm = {
	mlog: "",
	mtask: "",
	batch: "",
	count_use: "",
};

export default {
	props: {
		mlog: {
			type: String,
			default: "",
		},
		mgroup: {
			type: String,
			default: "",
		},
	},
	emits: ["success", "closed"],
	data() {
		return {
			loading: false,
			mode: "add",
			titleMap: {
				add: "新增",
			},
			//表单数据
			form: defaultForm,
			//验证规则
			rules: {
				batch: [
					{
						required: true,
						message: "请填写批次号",
						trigger: "blur",
					},
				],
				count_use: [
					{
						required: true,
						message: "请填写领用数量",
						trigger: "blur",
					},
				],
			},
			options: [],
			visible: false,
			isSaveing: false,
			setFiltersVisible: false,
		};
	},
	mounted() {
		this.getMtask();
	},
	methods: {
		open() {
			this.visible = true;
		},
		getMtask() {
			let that = this;
			this.$API.pm.mtask.list
				.req({ page: 0, mgroup: that.mgroup })
				.then((res) => {
					that.options = res;
				});
		},
		//表单提交方法
		submit() {
			let that = this;
			that.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					that.isSaveing = true;
					that.form.mlog = that.mlog;
					that.$API.wpm.mlogb.in
						.req(that.form)
						.then((res) => {
							that.isSaveing = false;
							that.$emit("success");
							that.visible = false;
							that.$message.success("操作成功");
						})
						.catch((err) => {
							that.isSaveing = false;
						});
				}
			});
		},
		//设置过滤项
		setFilters(filters) {
			this.selectionFilters = filters;
			this.setFiltersVisible = true;
		},
	},
};
</script>

<style></style>
