<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"
					:disabled="mode == 'show'"
					label-width="90px"
				>
					<el-row>
						<el-col :md="8" :sm="24">
							<el-form-item label="测点类型">
								<el-select
									v-model="form.type"
									style="width: 100%"
								>
									<el-option
										v-for="e in mpointTypeEnum.values"
										:key="e.key"
										:value="e.key"
										:label="e.text"
									></el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :md="4" :sm="24">
							<el-form-item label="是否启用">
								<el-switch v-model="form.enabled" />
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item
								label="是否代表所在工段数据"
								label-width="220"
							>
								<el-switch v-model="form.is_rep_mgroup" />
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="名称" prop="name">
								<el-input
									v-model="form.name"
									clearable
								></el-input>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="别名" prop="nickname">
								<el-input
									v-model="form.nickname"
									clearable
								></el-input>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="测点编号" prop="code">
								<el-input
									v-model="form.code"
									clearable
								></el-input>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="单位" prop="unit">
								<el-input
									v-model="form.unit"
									clearable
								></el-input>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="值类型" prop="val_type">
								<el-select
									v-model="form.val_type"
									style="width: 100%"
								>
									<el-option
										v-for="e in valTypeEnum.values"
										:key="e.key"
										:value="e.key"
										:label="e.text"
									></el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="所属设备">
								<el-select
									v-model="form.ep_belong"
									placeholder="所属设备"
									clearable
									filterable
									style="width: 100%"
								>
									<el-option
										v-for="item in epOptions"
										:key="item.id"
										:label="item.name"
										:value="item.id"
									></el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item
								label="所属工段"
								prop="mgroup"
								label-width="120"
							>
								<el-select
									v-model="form.mgroup"
									placeholder="所属工段"
									clearable
									style="width: 100%"
								>
									<el-option
										v-for="item in mgroupOptions"
										:key="item.id"
										:label="item.name"
										:value="item.id"
									></el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="计量物料">
								<el-select
									v-model="form.material"
									placeholder="计量物料"
									clearable
									filterable
									style="width: 100%"
								>
									<el-option
										v-for="item in materials"
										:key="item.id"
										:label="item.name"
										:value="item.id"
									></el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :md="24" :sm="24">
							<el-form-item label="计算公式">
								<el-input
									v-model="form.formula"
									clearable
								></el-input>
							</el-form-item>
						</el-col>
						<!-- <el-col :md="12" :sm="24">
              <el-form-item label="调用方法">
                <el-input v-model="form.func_on_change" clearable></el-input>
              </el-form-item>
            </el-col> -->
						<el-col :md="12" :sm="24">
							<el-form-item label="采集间隔(s)" label-width="90">
								<el-input-number
									v-model="form.interval"
								></el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item
								label="关联环保字段"
								label-width="120"
							>
								<el-select
									v-model="form.enp_field"
									clearable
									style="width: 100%"
								>
									<el-option
										v-for="e in enpfieldEnum.values"
										:key="e.key"
										:value="e.key"
										:label="e.text"
									></el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="是否展示">
								<el-switch v-model="form.need_display" />
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item
								label="报告排序"
								prop="report_sortstr"
							>
								<el-input
									v-model="form.report_sortstr"
									clearable
								></el-input>
							</el-form-item>
						</el-col>
						<el-divider />
						<el-col :md="12" :sm="24">
							<el-form-item label="所监测设备">
								<el-select
									v-model="form.ep_monitored"
									placeholder="所监测设备"
									clearable
									filterable
									style="width: 100%"
								>
									<el-option
										v-for="item in epOptions"
										:key="item.id"
										:label="item.name"
										:value="item.id"
									></el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item
								label="是否表示所监测设备的运行状态"
								label-width="220"
							>
								<el-switch
									v-model="form.is_rep_ep_running_state"
								/>
							</el-form-item>
						</el-col>
						<el-col :md="8" :sm="24">
							<el-form-item
								label="状态量基准值"
								label-width="100"
							>
								<el-input-number
									v-model="form.ep_rs_val"
									:precision="3"
								></el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="16" :sm="24">
							<el-form-item
								label="状态量表达式"
								label-width="100"
							>
								<el-input
									v-model="form.ep_rs_expr"
									clearable
								></el-input>
							</el-form-item>
						</el-col>
						<el-col :md="16" :sm="24">
							<el-form-item
								label="影响测点编号"
								label-width="100"
							>
								<el-input
									v-model="form.mpoint_affect"
									clearable
								></el-input>
							</el-form-item>
						</el-col>
						<!-- <el-col :md="12" :sm="24">
							<el-form-item
								label="累计量工段分配"
								label-width="120"
							>
								<el-button
									type="primary"
									icon="el-icon-plus"
									@click="addAllocate"
								></el-button>
							</el-form-item>
						</el-col> -->
					</el-row>
					<!-- <el-row>
						<template
							v-for="(item, $index) in form.mgroups_allocate"
							:key="$index"
						>
							<el-col :md="11" :sm="24">
								<el-form-item label="分配工段" prop="mgroup">
									<el-select
										v-model="item.mgroup"
										placeholder="分配工段"
										clearable
										style="width: 100%"
									>
										<el-option
											v-for="item in mgroupOptions"
											:key="item.id"
											:label="item.name"
											:value="item.id"
										></el-option>
									</el-select>
								</el-form-item>
							</el-col>
							<el-col :md="11" :sm="24">
								<el-form-item label="分配比例" prop="rate">
									<el-input-number
										precision="2"
										v-model="item.ratio"
										step="0.1"
										min="0"
										max="1"
										clearable
										controls-position="right"
									></el-input-number>
								</el-form-item>
							</el-col>
							<el-col :md="2" :sm="24">
								<el-button
									type="primary"
									icon="el-icon-delete"
									@click="delAllocate($index)"
								></el-button>
							</el-col>
						</template>
					</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>
import { valTypeEnum, mpointTypeEnum, enpfieldEnum } from "@/utils/enum.js";
export default {
	emits: ["success", "closed"],
	data() {
		return {
			valTypeEnum,
			mpointTypeEnum,
			enpfieldEnum,
			loading: false,
			mode: "add",
			titleMap: {
				add: "新增",
				edit: "编辑",
				show: "查看",
			},
			form: {
				type: 10,
				belong_dept: null,
				mgroup: null,
				interval: 10,
				func_on_change: "",
				is_rep_ep_running_state: false,
			},
			rules: {
				name: [{ required: true, message: "请输入测点名称" }],
				code: [{ required: true, message: "请输入测点编号" }],
				unit: [{ required: true, message: "请输入测点单位" }],
				val_type: [{ required: true, message: "请选择值类型" }]
			},
			visible: false,
			isSaveing: false,
			setFiltersVisible: false,
			mgroupOptions: [],
			epOptions: [],
			materials: [],
		};
	},
	mounted() {
		this.getMgroup();
		this.getEquipment();
		this.getMaterial();
	},
	methods: {
		//获取集合列表
		getMgroup() {
			this.$API.mtm.mgroup.list.req({ page: 0 }).then((res) => {
				this.mgroupOptions = res;
			});
		},
		//获取设备列表
		getEquipment() {
			this.$API.em.equipment.list
				.req({ page: 0 })
				.then((res) => {
					this.epOptions = res;
				});
		},
		//获取产品列表
		getMaterial() {
			this.$API.mtm.material.list.req({ page: 0 }).then((res) => {
				this.materials = res;
			});
		},
		// mgroupChange(e) {
		// 	this.form.mgroups_allocate.push({ mgroup: e, ratio: 1 });
		// },
		//显示
		open(mode = "add") {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		// addAllocate() {
		// 	this.form.mgroups_allocate.push({ mgroup: null, ratio: 0 });
		// },
		// delAllocate(index) {
		// 	this.form.mgroups_allocate.splice(index, 1);
		// },
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					if (this.form.mgroup == "") {
						this.form.mgroup = null;
					}
					// if (this.form.mgroups_allocate.length > 0) {
					// 	let arr = this.form.mgroups_allocate.filter((item) => {
					// 		return item.mgroup !== "" && item.mgroup !== null;
					// 	});
					// 	this.form.mgroups_allocate = arr;
					// }
					if (this.mode == "add") {
						this.$API.enm.mpoint.create
							.req(this.form)
							.then((res) => {
								this.isSaveing = false;
								this.$emit("success", this.form, this.mode);
								this.visible = false;
								this.$message.success("操作成功");
							})
							.catch((res) => {
								this.isSaveing = false;
							});
					} else if (this.mode == "edit") {
						this.$API.enm.mpoint.update
							.req(this.form.id, this.form)
							.then((res) => {
								this.isSaveing = false;
								this.$emit("success", this.form, this.mode);
								this.visible = false;
								this.$message.success("操作成功");
							})
							.catch((res) => {
								this.isSaveing = false;
							});
					}
				}
			});
		},
		//表单注入数据
		setData(data) {
			debugger;
			Object.assign(this.form, data);
		},
		//设置过滤项
		setFilters(filters) {
			this.selectionFilters = filters;
			this.setFiltersVisible = true;
		},
	},
};
</script>

<style>
.formItem {
	width: 100%;
}
</style>
