<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"
				>
					<el-form-item label="名称" prop="name">
						<el-input v-model="form.name" clearable></el-input>
					</el-form-item>
					<el-form-item label="编号" prop="number">
						<el-input v-model="form.number" clearable></el-input>
					</el-form-item>
					<el-form-item label="等级">
						<el-select
							v-model="form.level"
							placeholder="等级"
							clearable
							style="width: 100%"
						>
							<el-option
								v-for="item in options"
								:key="item.value"
								:label="item.label"
								:value="item.value"
							></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="分类" prop="cate">
						<el-select
							v-model="form.cate"
							placeholder="分类"
							clearable
							style="width: 100%"
						>
							<el-option
								v-for="item in cateOptions"
								:key="item.value"
								:label="item.label"
								:value="item.value"
							></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="排气筒高度">
						<el-input-number
							v-model="form.height"
							:min="1"
							controls-position="right"
							style="width: 100%"
						></el-input-number>
					</el-form-item>
					<el-form-item label="处理量">
						<el-input
							v-model="form.treatment_capacity"
							style="width: 100%"
						></el-input>
					</el-form-item>
					<el-form-item label="最低限值">
						<el-input-number
							v-model="form.pm_limit"
							:min="1"
							controls-position="right"
							style="width: 100%"
						></el-input-number>
					</el-form-item>
					<el-form-item label="治理措施">
						<el-input v-model="form.measure" clearable></el-input>
					</el-form-item>
					<el-form-item label="坐标">
						<el-input
							v-model="form.coordinates"
							clearable
						></el-input>
					</el-form-item>
					<el-form-item label="所属工段">
						<el-select
							v-model="form.mgroup"
							placeholder="所属工段"
							clearable
							style="width: 100%"
						>
							<el-option
								v-for="item in mgroups"
								:key="item.id"
								:label="item.name"
								:value="item.id"
							></el-option>
						</el-select>
					</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 = {
	type: 10,
	number: "",
	name: "",
	cate: "",
	level: "",
	height: "",
	treatment_capacity: "", //处理量
	pm_limit: "", //单位
	measure: "", //治理措施
	coordinates: "", //坐标
	mgroup: "",
};
export default {
	emits: ["success", "closed"],
	data() {
		return {
			mgroupList: [],
			loading: false,
			mode: "add",
			titleMap: {
				add: "新增排放口",
				edit: "编辑排放口",
				show: "查看排放口",
			},
			form: defaultForm,
			rules: {
				name: [
					{
						required: true,
						message: "请输入排放口名称",
						trigger: "blur",
					},
				],
				number: [
					{
						required: true,
						message: "请输入排放口编号",
						trigger: "blur",
					},
				],
			},
			visible: false,
			isSaveing: false,
			options: [
				{ label: "主要排放口", value: 10 },
				{ label: "次要排放口", value: 20 },
			],
			cateOptions: [
				{ label: "生产工艺", value: "product" },
				{ label: "物料输送", value: "mtrans" },
				{ label: "物料储存", value: "mstore" },
			],
			mgroups: [],
		};
	},
	mounted() {
		this.getMgroups();
	},
	methods: {
		//获取工段列表
		getMgroups() {
			var res = this.$API.mtm.mgroup.list.req({ page: 0 }).then((res) => {
				this.mgroups = 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.enp.drain.create
							.req(that.form)
							.then((res) => {
								that.isSaveing = false;
								that.$emit("success", that.form, that.mode);
								that.visible = false;
								that.$message.success("操作成功");
							});
					} else {
						res = that.$API.enp.drain.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("操作成功");
							});
					}
				} else {
					that.isSaveing = false;
				}
			});
		},

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

<style></style>
