<template>
	<el-dialog :title="!dataForm.id ? '新增' : '修改'" :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="plc类型" prop="plcType">
				<el-input v-model="dataForm.plcType" placeholder="plc类型"></el-input>
			</el-form-item>
			<el-form-item label="编码" prop="code">
				<el-input v-model="dataForm.code" placeholder="编码"></el-input>
			</el-form-item>
			<el-form-item label="plc名称（ip+名称对应实时数据库表名）" prop="name">
				<el-input v-model="dataForm.name" placeholder="plc名称（ip+名称对应实时数据库表名）"></el-input>
			</el-form-item>
			<el-form-item label="英文名称" prop="enName">
				<el-input v-model="dataForm.enName" placeholder="英文名称"></el-input>
			</el-form-item>
			<el-form-item label="描述" prop="description">
				<el-input v-model="dataForm.description" placeholder="描述"></el-input>
			</el-form-item>
			<el-form-item label="plc条码" prop="barcode">
				<el-input v-model="dataForm.barcode" placeholder="plc条码"></el-input>
			</el-form-item>
			<el-form-item label="链接方式，0 tcp，1 udp等" prop="controlType">
				<el-input v-model="dataForm.controlType" placeholder="链接方式，0 tcp，1 udp等"></el-input>
			</el-form-item>
			<el-form-item label="是否监控 0 代表不监控, 1 代表监控" prop="monitor">
				<el-input v-model="dataForm.monitor" placeholder="是否监控 0 代表不监控, 1 代表监控"></el-input>
			</el-form-item>
			<el-form-item label="是否采集 0 代表不采集, 1 代表采集" prop="collection">
				<el-input v-model="dataForm.collection" placeholder="是否采集 0 代表不采集, 1 代表采集"></el-input>
			</el-form-item>
			<el-form-item label="IP" prop="ip">
				<el-input v-model="dataForm.ip" placeholder="IP"></el-input>
			</el-form-item>
			<el-form-item label="端口号" prop="port">
				<el-input v-model="dataForm.port" placeholder="端口号"></el-input>
			</el-form-item>
			<el-form-item label="启用状态:0 、停用，1、启用" prop="enabled">
				<el-input v-model="dataForm.enabled" placeholder="启用状态:0 、停用，1、启用"></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="创建人" prop="creatorId">
				<el-input v-model="dataForm.creatorId" placeholder="创建人"></el-input>
			</el-form-item>
			<el-form-item label="创建人姓名" prop="creatorName">
				<el-input v-model="dataForm.creatorName" placeholder="创建人姓名"></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="更新人" prop="updaterId">
				<el-input v-model="dataForm.updaterId" placeholder="更新人"></el-input>
			</el-form-item>
			<el-form-item label="更新人姓名" prop="updaterName">
				<el-input v-model="dataForm.updaterName" placeholder="更新人姓名"></el-input>
			</el-form-item>
			<el-form-item label="更新时间" prop="updateTime">
				<el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
			</el-form-item>
			<el-form-item label="版本号" prop="version">
				<el-input v-model="dataForm.version" placeholder="版本号"></el-input>
			</el-form-item>
		</el-form>
		<span slot="footer" class="dialog-footer">
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="dataFormSubmit()">确定</el-button>
		</span>
	</el-dialog>
</template>

<script>
export default {
	data() {
		return {
			visible: false,
			dataForm: {
				id: 0,
				plcType: '',
				code: '',
				name: '',
				enName: '',
				description: '',
				barcode: '',
				controlType: '',
				monitor: '',
				collection: '',
				ip: '',
				port: '',
				enabled: '',
				remark: '',
				valid: '',
				creatorId: '',
				creatorName: '',
				createTime: '',
				updaterId: '',
				updaterName: '',
				updateTime: '',
				version: ''
			},
			dataRule: {
				plcType: [{ required: true, message: 'plc类型不能为空', trigger: 'blur' }],
				code: [{ required: true, message: '编码不能为空', trigger: 'blur' }],
				name: [{ required: true, message: 'plc名称（ip+名称对应实时数据库表名）不能为空', trigger: 'blur' }],
				enName: [{ required: true, message: '英文名称不能为空', trigger: 'blur' }],
				description: [{ required: true, message: '描述不能为空', trigger: 'blur' }],
				barcode: [{ required: true, message: 'plc条码不能为空', trigger: 'blur' }],
				controlType: [{ required: true, message: '链接方式，0 tcp，1 udp等不能为空', trigger: 'blur' }],
				monitor: [{ required: true, message: '是否监控 0 代表不监控, 1 代表监控不能为空', trigger: 'blur' }],
				collection: [{ required: true, message: '是否采集 0 代表不采集, 1 代表采集不能为空', trigger: 'blur' }],
				ip: [{ required: true, message: 'IP不能为空', trigger: 'blur' }],
				port: [{ required: true, message: '端口号不能为空', trigger: 'blur' }],
				enabled: [{ required: true, message: '启用状态:0 、停用，1、启用不能为空', 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/equipmenplc/info/${this.dataForm.id}`),
						method: 'get',
						params: this.$http.adornParams()
					}).then(({ data }) => {
						if (data && data.code === 0) {
							this.dataForm.plcType = data.equipmenplc.plcType
							this.dataForm.code = data.equipmenplc.code
							this.dataForm.name = data.equipmenplc.name
							this.dataForm.enName = data.equipmenplc.enName
							this.dataForm.description = data.equipmenplc.description
							this.dataForm.barcode = data.equipmenplc.barcode
							this.dataForm.controlType = data.equipmenplc.controlType
							this.dataForm.monitor = data.equipmenplc.monitor
							this.dataForm.collection = data.equipmenplc.collection
							this.dataForm.ip = data.equipmenplc.ip
							this.dataForm.port = data.equipmenplc.port
							this.dataForm.enabled = data.equipmenplc.enabled
							this.dataForm.remark = data.equipmenplc.remark
							this.dataForm.valid = data.equipmenplc.valid
							this.dataForm.creatorId = data.equipmenplc.creatorId
							this.dataForm.creatorName = data.equipmenplc.creatorName
							this.dataForm.createTime = data.equipmenplc.createTime
							this.dataForm.updaterId = data.equipmenplc.updaterId
							this.dataForm.updaterName = data.equipmenplc.updaterName
							this.dataForm.updateTime = data.equipmenplc.updateTime
							this.dataForm.version = data.equipmenplc.version
						}
					})
				}
			})
		},
		// 表单提交
		dataFormSubmit() {
			this.$refs['dataForm'].validate(valid => {
				if (valid) {
					this.$http({
						url: this.$http.adornUrl(`/monitoring/equipmenplc/${!this.dataForm.id ? 'save' : 'update'}`),
						method: 'post',
						data: this.$http.adornData({
							id: this.dataForm.id || undefined,
							plcType: this.dataForm.plcType,
							code: this.dataForm.code,
							name: this.dataForm.name,
							enName: this.dataForm.enName,
							description: this.dataForm.description,
							barcode: this.dataForm.barcode,
							controlType: this.dataForm.controlType,
							monitor: this.dataForm.monitor,
							collection: this.dataForm.collection,
							ip: this.dataForm.ip,
							port: this.dataForm.port,
							enabled: this.dataForm.enabled,
							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: '操作成功',
								type: 'success',
								duration: 1500,
								onClose: () => {
									this.visible = false
									this.$emit('refreshDataList')
								}
							})
						} else {
							this.$message.error(data.msg)
						}
					})
				}
			})
		}
	}
}
</script>
