<template>
	<el-dialog :title="!dataForm.id ? i18n.t('add') : i18n.t('update')" :close-on-click-modal="false" :visible.sync="visible">
		<el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
			<el-form-item label="报警信息ID，关联T_equipment_group_alarm表" prop="alarmId">
				<el-input v-model="dataForm.alarmId" placeholder="报警信息ID，关联T_equipment_group_alarm表"></el-input>
			</el-form-item>
			<el-form-item label="报警来源" prop="alarmSource">
				<el-input v-model="dataForm.alarmSource" placeholder="报警来源"></el-input>
			</el-form-item>
			<el-form-item label="报警详细内容" prop="alarmContent">
				<el-input v-model="dataForm.alarmContent" placeholder="报警详细内容"></el-input>
			</el-form-item>
			<el-form-item label="报警设备id 关联equipment表" prop="alarmEquipmentId">
				<el-input v-model="dataForm.alarmEquipmentId" placeholder="报警设备id 关联equipment表"></el-input>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="dataForm.remark" placeholder="备注"></el-input>
			</el-form-item>
			<el-form-item label="删除标志，是否有效:1 可用 0不可用" prop="valid">
				<el-input v-model="dataForm.valid" placeholder="删除标志，是否有效:1 可用 0不可用"></el-input>
			</el-form-item>
			<el-form-item :label="$t('creator')" prop="creatorId">
				<el-input v-model="dataForm.creatorId" :placeholder="$t('creator')"></el-input>
			</el-form-item>
			<el-form-item :label="$t('creatorName')" prop="creatorName">
				<el-input v-model="dataForm.creatorName" :placeholder="$t('creatorName')"></el-input>
			</el-form-item>
			<el-form-item label="创建时间" prop="createTime">
				<el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
			</el-form-item>
			<el-form-item :label="$t('updator')" prop="updaterId">
				<el-input v-model="dataForm.updaterId" :placeholder="$t('updator')"></el-input>
			</el-form-item>
			<el-form-item :label="$t('updatorName')" prop="updaterName">
				<el-input v-model="dataForm.updaterName" :placeholder="$t('updatorName')"></el-input>
			</el-form-item>
			<el-form-item :label="$t('updateTime')" prop="updateTime">
				<el-input v-model="dataForm.updateTime" :placeholder="$t('updateTime')"></el-input>
			</el-form-item>
			<el-form-item :label="$t('version')" prop="version">
				<el-input v-model="dataForm.version" :placeholder="$t('version')"></el-input>
			</el-form-item>
		</el-form>
		<span slot="footer" class="dialog-footer">
			<el-button @click="visible = false">{{ $t('cancel') }}</el-button>
			<el-button type="primary" @click="dataFormSubmit()">{{ $t('confirm') }}</el-button>
		</span>
	</el-dialog>
</template>

<script>
export default {
	data() {
		return {
			visible: false,
			dataForm: {
				id: 0,
				alarmId: '',
				alarmSource: '',
				alarmContent: '',
				alarmEquipmentId: '',
				remark: '',
				valid: '',
				creatorId: '',
				creatorName: '',
				createTime: '',
				updaterId: '',
				updaterName: '',
				updateTime: '',
				version: ''
			},
			dataRule: {
				alarmId: [{ required: true, message: '报警信息ID，关联T_equipment_group_alarm表不能为空', trigger: 'blur' }],
				alarmSource: [{ required: true, message: '报警来源不能为空', trigger: 'blur' }],
				alarmContent: [{ required: true, message: '报警详细内容不能为空', trigger: 'blur' }],
				alarmEquipmentId: [{ required: true, message: '报警设备id 关联equipment表不能为空', trigger: 'blur' }],
				remark: [{ required: true, message: '备注不能为空', trigger: 'blur' }],
				valid: [{ required: true, message: '删除标志，是否有效:1 可用 0不可用不能为空', trigger: 'blur' }],
				creatorId: [{ required: true, message: '创建人不能为空', trigger: 'blur' }],
				creatorName: [{ required: true, message: '创建人姓名不能为空', trigger: 'blur' }],
				createTime: [{ required: true, message: '创建时间不能为空', trigger: 'blur' }],
				updaterId: [{ required: true, message: '更新人不能为空', trigger: 'blur' }],
				updaterName: [{ required: true, message: '更新人姓名不能为空', trigger: 'blur' }],
				updateTime: [{ required: true, message: '更新时间不能为空', trigger: 'blur' }],
				version: [{ required: true, message: '版本号不能为空', trigger: 'blur' }]
			}
		}
	},
	methods: {
		init(id) {
			this.dataForm.id = id || 0
			this.visible = true
			this.$nextTick(() => {
				this.$refs['dataForm'].resetFields()
				if (this.dataForm.id) {
					this.$http({
						url: this.$http.adornUrl(`/monitoring/equipmentAlarmLog/${this.dataForm.id}`),
						method: 'get',
						params: this.$http.adornParams()
					}).then(({ data }) => {
						if (data && data.code === 0) {
							this.dataForm.alarmId = data.equipmenalarmLog.alarmId
							this.dataForm.alarmSource = data.equipmenalarmLog.alarmSource
							this.dataForm.alarmContent = data.equipmenalarmLog.alarmContent
							this.dataForm.alarmEquipmentId = data.equipmenalarmLog.alarmEquipmentId
							this.dataForm.remark = data.equipmenalarmLog.remark
							this.dataForm.valid = data.equipmenalarmLog.valid
							this.dataForm.creatorId = data.equipmenalarmLog.creatorId
							this.dataForm.creatorName = data.equipmenalarmLog.creatorName
							this.dataForm.createTime = data.equipmenalarmLog.createTime
							this.dataForm.updaterId = data.equipmenalarmLog.updaterId
							this.dataForm.updaterName = data.equipmenalarmLog.updaterName
							this.dataForm.updateTime = data.equipmenalarmLog.updateTime
							this.dataForm.version = data.equipmenalarmLog.version
						}
					})
				}
			})
		},
		// 表单提交
		dataFormSubmit() {
			this.$refs['dataForm'].validate(valid => {
				if (valid) {
					this.$http({
						url: this.$http.adornUrl(`/monitoring/equipmentAlarmLog/${!this.dataForm.id ? '' : this.dataForm.id}`),
						method: this.dataForm.id ? 'put' : 'post',
						data: this.$http.adornData({
							id: this.dataForm.id || undefined,
							alarmId: this.dataForm.alarmId,
							alarmSource: this.dataForm.alarmSource,
							alarmContent: this.dataForm.alarmContent,
							alarmEquipmentId: this.dataForm.alarmEquipmentId,
							remark: this.dataForm.remark,
							valid: this.dataForm.valid,
							creatorId: this.dataForm.creatorId,
							creatorName: this.dataForm.creatorName,
							createTime: this.dataForm.createTime,
							updaterId: this.dataForm.updaterId,
							updaterName: this.dataForm.updaterName,
							updateTime: this.dataForm.updateTime,
							version: this.dataForm.version
						})
					}).then(({ data }) => {
						if (data && data.code === 0) {
							this.$message({
								message: i18n.t('prompt.success'),
								type: 'success',
								duration: 1500,
								onClose: () => {
									this.visible = false
									this.$emit('refreshDataList')
								}
							})
						} else {
							this.$message.error(data.msg)
						}
					})
				}
			})
		}
	}
}
</script>
