<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="编码" prop="code">
				<el-input v-model="dataForm.code" placeholder="编码"></el-input>
			</el-form-item>
			<el-form-item label="设备名称" prop="name">
				<el-input v-model="dataForm.name" placeholder="设备名称"></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="abbr">
				<el-input v-model="dataForm.abbr" placeholder="缩写"></el-input>
			</el-form-item>
			<el-form-item label="进厂日期" prop="enterTime">
				<el-input v-model="dataForm.enterTime" placeholder="进厂日期"></el-input>
			</el-form-item>
			<el-form-item label="生产日期" prop="productionTime">
				<el-input v-model="dataForm.productionTime" placeholder="生产日期"></el-input>
			</el-form-item>
			<el-form-item label="设备类型，关联设备类型表" prop="equipmentTypeId">
				<el-input v-model="dataForm.equipmentTypeId" placeholder="设备类型，关联设备类型表"></el-input>
			</el-form-item>
			<el-form-item label="分组id （关联 T_EQUIPMENT_GROUP）" prop="groupId">
				<el-input v-model="dataForm.groupId" placeholder="分组id （关联 T_EQUIPMENT_GROUP）"></el-input>
			</el-form-item>
			<el-form-item label="0:无类别，1:上片数据设备，2:下片数据设备" prop="dataType">
				<el-input v-model="dataForm.dataType" placeholder="0:无类别，1:上片数据设备，2:下片数据设备"></el-input>
			</el-form-item>
			<el-form-item label="每小时生产数量" prop="tvalue">
				<el-input v-model="dataForm.tvalue" placeholder="每小时生产数量"></el-input>
			</el-form-item>
			<el-form-item label="单件加工时间 s/件" prop="processingTime">
				<el-input v-model="dataForm.processingTime" placeholder="单件加工时间 s/件"></el-input>
			</el-form-item>
			<el-form-item label="制造商" prop="manufacturer">
				<el-input v-model="dataForm.manufacturer" placeholder="制造商"></el-input>
			</el-form-item>
			<el-form-item label="规格" prop="spec">
				<el-input v-model="dataForm.spec" 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="启用状态: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-item label="外部系统代码" prop="externalCode">
				<el-input v-model="dataForm.externalCode" placeholder="外部系统代码"></el-input>
			</el-form-item>
			<el-form-item label="统一用的外部代码，代表用哪个设备的数据代替该设备，目前用于生产数量统计" prop="externalCodeStatis">
				<el-input v-model="dataForm.externalCodeStatis" 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,
				code: '',
				name: '',
				enName: '',
				abbr: '',
				enterTime: '',
				productionTime: '',
				equipmentTypeId: '',
				groupId: '',
				dataType: '',
				tvalue: '',
				processingTime: '',
				manufacturer: '',
				spec: '',
				description: '',
				enabled: '',
				remark: '',
				valid: '',
				creatorId: '',
				creatorName: '',
				createTime: '',
				updaterId: '',
				updaterName: '',
				updateTime: '',
				version: '',
				externalCode: '',
				externalCodeStatis: ''
			},
			dataRule: {
				code: [{ required: true, message: '编码不能为空', trigger: 'blur' }],
				name: [{ required: true, message: '设备名称不能为空', trigger: 'blur' }],
				enName: [{ required: true, message: '英文名称不能为空', trigger: 'blur' }],
				abbr: [{ required: true, message: '缩写不能为空', trigger: 'blur' }],
				enterTime: [{ required: true, message: '进厂日期不能为空', trigger: 'blur' }],
				productionTime: [{ required: true, message: '生产日期不能为空', trigger: 'blur' }],
				equipmentTypeId: [{ required: true, message: '设备类型，关联设备类型表不能为空', trigger: 'blur' }],
				groupId: [{ required: true, message: '分组id （关联 T_EQUIPMENT_GROUP）不能为空', trigger: 'blur' }],
				dataType: [{ required: true, message: '0:无类别，1:上片数据设备，2:下片数据设备不能为空', trigger: 'blur' }],
				tvalue: [{ required: true, message: '每小时生产数量不能为空', trigger: 'blur' }],
				processingTime: [{ required: true, message: '单件加工时间 s/件不能为空', trigger: 'blur' }],
				manufacturer: [{ required: true, message: '制造商不能为空', trigger: 'blur' }],
				spec: [{ required: true, message: '规格不能为空', trigger: 'blur' }],
				description: [{ 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' }],
				externalCode: [{ required: true, message: '外部系统代码不能为空', trigger: 'blur' }],
				externalCodeStatis: [{ 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/equipment/${this.dataForm.id}`),
						method: 'get',
						params: this.$http.adornParams()
					}).then(({ data }) => {
						if (data && data.code === 0) {
							this.dataForm.code = data.equipment.code
							this.dataForm.name = data.equipment.name
							this.dataForm.enName = data.equipment.enName
							this.dataForm.abbr = data.equipment.abbr
							this.dataForm.enterTime = data.equipment.enterTime
							this.dataForm.productionTime = data.equipment.productionTime
							this.dataForm.equipmentTypeId = data.equipment.equipmentTypeId
							this.dataForm.groupId = data.equipment.groupId
							this.dataForm.dataType = data.equipment.dataType
							this.dataForm.tvalue = data.equipment.tvalue
							this.dataForm.processingTime = data.equipment.processingTime
							this.dataForm.manufacturer = data.equipment.manufacturer
							this.dataForm.spec = data.equipment.spec
							this.dataForm.description = data.equipment.description
							this.dataForm.enabled = data.equipment.enabled
							this.dataForm.remark = data.equipment.remark
							this.dataForm.valid = data.equipment.valid
							this.dataForm.creatorId = data.equipment.creatorId
							this.dataForm.creatorName = data.equipment.creatorName
							this.dataForm.createTime = data.equipment.createTime
							this.dataForm.updaterId = data.equipment.updaterId
							this.dataForm.updaterName = data.equipment.updaterName
							this.dataForm.updateTime = data.equipment.updateTime
							this.dataForm.version = data.equipment.version
							this.dataForm.externalCode = data.equipment.externalCode
							this.dataForm.externalCodeStatis = data.equipment.externalCodeStatis
						}
					})
				}
			})
		},
		// 表单提交
		dataFormSubmit() {
			this.$refs['dataForm'].validate(valid => {
				if (valid) {
					this.$http({
						url: this.$http.adornUrl(`/monitoring/equipment/${!this.dataForm.id ? '' : this.dataForm.id}`),
						method: this.dataForm.id ? 'put' : 'post',
						data: this.$http.adornData({
							id: this.dataForm.id || undefined,
							code: this.dataForm.code,
							name: this.dataForm.name,
							enName: this.dataForm.enName,
							abbr: this.dataForm.abbr,
							enterTime: this.dataForm.enterTime,
							productionTime: this.dataForm.productionTime,
							equipmentTypeId: this.dataForm.equipmentTypeId,
							groupId: this.dataForm.groupId,
							dataType: this.dataForm.dataType,
							tvalue: this.dataForm.tvalue,
							processingTime: this.dataForm.processingTime,
							manufacturer: this.dataForm.manufacturer,
							spec: this.dataForm.spec,
							description: this.dataForm.description,
							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,
							externalCode: this.dataForm.externalCode,
							externalCodeStatis: this.dataForm.externalCodeStatis
						})
					}).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>
