<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="设备外部编码" prop="externalCode">
				<el-input v-model="dataForm.externalCode" placeholder="设备外部编码"></el-input>
			</el-form-item>
			<el-form-item label="设备名称" prop="equipmentName">
				<el-input v-model="dataForm.equipmentName" placeholder="设备名称"></el-input>
			</el-form-item>
			<el-form-item label="进入设备的数量" prop="inQuantity">
				<el-input v-model="dataForm.inQuantity" placeholder="进入设备的数量"></el-input>
			</el-form-item>
			<el-form-item label="离开设备的数量，若plc只记录一个生产数量，也写入该字段" prop="outQuantity">
				<el-input v-model="dataForm.outQuantity" placeholder="离开设备的数量，若plc只记录一个生产数量，也写入该字段"></el-input>
			</el-form-item>
			<el-form-item label="" prop="okQuantity">
				<el-input v-model="dataForm.okQuantity" placeholder=""></el-input>
			</el-form-item>
			<el-form-item label="设备上报的报废数量" prop="nokQuantity">
				<el-input v-model="dataForm.nokQuantity" 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="备注" 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-item label="生产数量的记录时间" prop="recordTime">
				<el-input v-model="dataForm.recordTime" placeholder="生产数量的记录时间"></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,
				externalCode: '',
				equipmentName: '',
				inQuantity: '',
				outQuantity: '',
				okQuantity: '',
				nokQuantity: '',
				description: '',
				remark: '',
				valid: '',
				creatorId: '',
				creatorName: '',
				createTime: '',
				updaterId: '',
				updaterName: '',
				updateTime: '',
				version: '',
				recordTime: ''
			},
			dataRule: {
				externalCode: [{ required: true, message: '设备外部编码不能为空', trigger: 'blur' }],
				equipmentName: [{ required: true, message: '设备名称不能为空', trigger: 'blur' }],
				inQuantity: [{ required: true, message: '进入设备的数量不能为空', trigger: 'blur' }],
				outQuantity: [{ required: true, message: '离开设备的数量，若plc只记录一个生产数量，也写入该字段不能为空', trigger: 'blur' }],
				okQuantity: [{ required: true, message: i18n.t('cannotempty'), trigger: 'blur' }],
				nokQuantity: [{ required: true, message: '设备上报的报废数量不能为空', trigger: 'blur' }],
				description: [{ required: true, message: i18n.t('cannotempty'), 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' }],
				recordTime: [{ 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/equipmentQuantity/${this.dataForm.id}`),
						method: 'get',
						params: this.$http.adornParams()
					}).then(({ data }) => {
						if (data && data.code === 0) {
							this.dataForm.externalCode = data.equipmenquantity.externalCode
							this.dataForm.equipmentName = data.equipmenquantity.equipmentName
							this.dataForm.inQuantity = data.equipmenquantity.inQuantity
							this.dataForm.outQuantity = data.equipmenquantity.outQuantity
							this.dataForm.okQuantity = data.equipmenquantity.okQuantity
							this.dataForm.nokQuantity = data.equipmenquantity.nokQuantity
							this.dataForm.description = data.equipmenquantity.description
							this.dataForm.remark = data.equipmenquantity.remark
							this.dataForm.valid = data.equipmenquantity.valid
							this.dataForm.creatorId = data.equipmenquantity.creatorId
							this.dataForm.creatorName = data.equipmenquantity.creatorName
							this.dataForm.createTime = data.equipmenquantity.createTime
							this.dataForm.updaterId = data.equipmenquantity.updaterId
							this.dataForm.updaterName = data.equipmenquantity.updaterName
							this.dataForm.updateTime = data.equipmenquantity.updateTime
							this.dataForm.version = data.equipmenquantity.version
							this.dataForm.recordTime = data.equipmenquantity.recordTime
						}
					})
				}
			})
		},
		// 表单提交
		dataFormSubmit() {
			this.$refs['dataForm'].validate(valid => {
				if (valid) {
					this.$http({
						url: this.$http.adornUrl(`/monitoring/equipmentQuantity/${!this.dataForm.id ? '' : this.dataForm.id}`),
						method: this.dataForm.id ? 'put' : 'post',
						data: this.$http.adornData({
							id: this.dataForm.id || undefined,
							externalCode: this.dataForm.externalCode,
							equipmentName: this.dataForm.equipmentName,
							inQuantity: this.dataForm.inQuantity,
							outQuantity: this.dataForm.outQuantity,
							okQuantity: this.dataForm.okQuantity,
							nokQuantity: this.dataForm.nokQuantity,
							description: this.dataForm.description,
							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,
							recordTime: this.dataForm.recordTime
						})
					}).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>
