<template>
  <el-form ref="form" :rules="rules" label-width="100px" :model="form">
    <el-row>
      <el-col :span="12">
        <el-form-item label="关联表名" prop="plcTableName">
          <el-input v-model="form.plcTableName"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="关联表编码" prop="code">
          <el-input v-model="form.code" disabled></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="标识名" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="英文标识名" prop="enName">
          <el-input v-model="form.enName"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="是否采集" prop="collection">
          <el-switch v-model="form.collection"></el-switch>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import { getEnergyPlc, updateEnergyPlc, createEnergyPlc, getCode } from '@/api/base/energyPlc'
export default {
  name: 'energyPlcAdd',
  data() {
    return {
      form: {
        id: '',
        plcTableName: '',
        code: '',
        name: '',
        enName: '',
        description: '',
        collection: true
      },
      isEdit: false, //是否是编辑
      rules: {
        plcTableName: [{ required: true, message: '关联表名不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '关联表编码不能为空', trigger: 'blur' }],
        name: [{ required: true, message: '标识名称不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    init(id) {
      if (id) {
        this.isEdit = true
        this.form.id = id
        getEnergyPlc( id ).then((res) => {
          if (res.code === 0) {
            this.form = res.data
            this.form.collection = this.form.collection === 0 ? false : true
          }
        })
      } else {
        this.isEdit = false
        this.form.id = ''
        getCode().then((res) => {
          this.form.code = res.data
        })
      }
    },
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.form.collection = this.form.collection === false ? 0 : 1
          if (this.isEdit) {
            // 编辑
            updateEnergyPlc({...this.form}).then((res) => {
              if (res.code === 0) {
                this.$modal.msgSuccess("操作成功");
                this.$emit('successSubmit')
              }
            })
          } else {
            createEnergyPlc({...this.form}).then((res) => {
              if (res.code === 0) {
                this.$modal.msgSuccess("操作成功");
                this.$emit('successSubmit')
              }
            })
          }
        } else {
          return false
        }
      })
    },
    formClear() {
      this.$refs.form.resetFields()
      this.isEdit = false
    }
  }
}
</script>
