<template>
	<el-dialog
		:title="titleMap[mode]"
		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="100px"
					style="padding: 0 10px"
				>
					<el-form-item label="名称" prop="name">
						<el-input v-model="form.name" clearable></el-input>
					</el-form-item>
					<el-form-item label="大类">
						<el-input v-model="form.cate" clearable></el-input>
					</el-form-item>
					<el-form-item label="所属部门">
						<el-select
							v-model="form.belong_dept"
							placeholder="所属部门"
							clearable
							style="width: 100%"
						>
							<el-option
								v-for="item in deptOptions"
								:key="item.id"
								:label="item.name"
								:value="item.id"
							></el-option>
						</el-select>
					</el-form-item>
					<el-row>
						<el-col :span="12">
							<el-form-item label="交接到工段">
								<el-switch
									v-model="form.into_wm_mgroup"
								></el-switch>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="不合格品入库">
								<el-switch
									v-model="form.store_notok"
								></el-switch>
							</el-form-item>
						</el-col>
					</el-row>
				</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 = {
	name: "",
	cate: "",
	belong_dept: "",
	into_wm_mgroup: true,
	store_notok: true,
};
export default {
	emits: ["success", "closed"],
	data() {
		return {
			loading: false,
			mode: "add",
			titleMap: {
				add: "新增产品",
				edit: "编辑产品",
				show: "查看产品",
			},
			//表单数据
			form: defaultForm,
			//验证规则
			rules: {
				name: [
					{ required: true, message: "请输入名称", trigger: "blur" },
				],
			},
			visible: false,
			isSaveing: false,
			deptOptions: [],
			setFiltersVisible: false,
		};
	},
	mounted() {
		this.getDepts(); //获取部门
	},
	methods: {
		getDepts() {
			this.$API.system.dept.list
				.req({ page: 0, type: "dept" })
				.then((res) => {
					this.deptOptions = res;
				});
		},
		//显示
		open(mode = "add") {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		//表单注入数据
		setData(data) {
			Object.assign(this.form, data);
		},
		//表单提交方法
		submit() {
			let that = this;
			that.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					that.isSaveing = true;
					if (that.mode === "add") {
						that.$API.mtm.process.create
							.req(that.form)
							.then((res) => {
								that.isSaveing = false;
								that.$emit("success", that.form, that.mode);
								that.visible = false;
								that.$message.success("操作成功");
							})
							.catch((res) => {
								that.isSaveing = false;
							});
					} else {
						that.$API.mtm.process.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((res) => {
								that.isSaveing = false;
							});
					}
				}
			});
		},

		//设置过滤项
		setFilters(filters) {
			this.selectionFilters = filters;
			this.setFiltersVisible = true;
		},
	},
};
</script>

<style></style>
