<template>
	<el-dialog :title="title" :visible.sync="dialogFormVisible" width="800px" @close="close">
		<el-form ref="form" :model="form" :rules="rules" label-width="150px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="编号" prop="id"><el-input class="inputs" v-model="form.id" autocomplete="off" :disabled="!idAdd?true:false"></el-input></el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="编码" prop="nemCode"><el-input class="inputs" v-model="form.nemCode" autocomplete="off"></el-input></el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="名称" prop="name"><el-input class="inputs" v-model="form.name" autocomplete="off"></el-input></el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="单位" prop="valueUnit"><el-input class="inputs" v-model="form.valueUnit" autocomplete="off"></el-input></el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="英文名称" prop="englishName"><el-input class="inputs" v-model="form.englishName" autocomplete="off"></el-input></el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="类型编号" prop="typeId"><el-input class="inputs" v-model="form.typeId" autocomplete="off"></el-input></el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="最大值" prop="maxval"><el-input class="inputs" v-model="form.maxval" autocomplete="off"></el-input></el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="最小值" prop="minval"><el-input class="inputs" v-model="form.minval" autocomplete="off"></el-input></el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="合理最大值" prop="reasonableMaxval"><el-input class="inputs" v-model="form.reasonableMaxval" autocomplete="off"></el-input></el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="合理最小值" prop="reasonableMinval"><el-input class="inputs" v-model="form.reasonableMinval" autocomplete="off"></el-input></el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="统一编码" prop="uniformCode"><el-input class="inputs" v-model="form.uniformCode" autocomplete="off"></el-input></el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部件编号" prop="logicalUnitId"><el-input class="inputs" v-model="form.logicalUnitId" autocomplete="off"></el-input></el-form-item>
        </el-col>
      </el-row>
		</el-form>
		<div slot="footer" class="dialog-footer">
			<el-button @click="close">取 消</el-button>
			<el-button type="primary" @click="save">确 定</el-button>
		</div>
	</el-dialog>
</template>

<script>
import api from '@/api/realtimeDatabaseConfiguration'
import dayjs from "dayjs";
export default {
	name: 'TableEdit',
	props: {
		options: {
			type: Array,
			default: []
		},
		stationList: {
			type: Array,
			default: []
		},
	},
	data() {
		return {
			form: {
				id: "",
        nemCode: "",
        name: "",
        valueUnit: "",
        englishName: "",
        typeId: "",
        maxval: "",
        minval: "",
        reasonableMaxval: "",
        reasonableMinval: "",
        uniformCode: "",
        logicalUnitId: "",
			},
			rules: {
				id: [{ required: true, trigger: 'blur', message: '请输入' }],
				nemCode: [{ required: true, trigger: 'blur', message: '请输入' }],
				name: [{ required: true, trigger: 'blur', message: '请输入' }],
				valueUnit: [{ required: true, trigger: 'blur', message: '请输入' }],
				englishName: [{ required: true, trigger: 'blur', message: '请输入' }],
				typeId: [{ required: true, trigger: 'blur', message: '请输入' }],
				maxval: [{ required: true, trigger: 'blur', message: '请输入' }],
				minval: [{ required: true, trigger: 'blur', message: '请输入' }],
				reasonableMaxval: [{ required: true, trigger: 'blur', message: '请输入' }],
				reasonableMinval: [{ required: true, trigger: 'blur', message: '请输入' }],
				uniformCode: [{ required: true, trigger: 'blur', message: '请输入' }],
				logicalUnitId: [{ required: true, trigger: 'blur', message: '请输入' }],
			},
			title: '',
			dialogFormVisible: false,
			idAdd: true,
		}
	},
	created() { },
	methods: {
    changeDate() {
      this.$forceUpdate()
    },
		showEdit(row) {
			if (!row) {
				this.title = '添加'
				this.idAdd = true
			} else {
				this.title = '编辑'
				this.idAdd = false
				this.form = Object.assign({}, row)
			}
			this.dialogFormVisible = true
		},
		close() {
			this.$refs['form'].resetFields()
			this.form = this.$options.data().form
			this.dialogFormVisible = false
			this.$emit('fetch-data')
		},
		save() {
			this.$refs['form'].validate(async (valid) => {
				if (valid) {
					api.addTesting(this.form).then(res => {
						if (res.data) {
							this.$message({
								type: 'success',
								message: '添加成功!'
							});
							this.$emit('save-success');
							this.dialogFormVisible = false
						}
					})
				} else {
					return false
				}
			})
		},
		handleChange() {
		},
		onBlur(val, param) {
			if (typeof (this.form[param]) !== 'number') {
				this.form[param] = val.replace(/[^0-9]/ig, "")
			}
		},
	},
}
</script>
<style lang="less" scoped>
.inputs {
	width: 200px;
}

.lists {
	display: flex;
	flex-direction: row;
	align-items: center;
	justify-content: space-between;
}
</style>
