<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连接表ID" prop="plcConId">
      <el-input v-model="dataForm.plcConId" placeholder="plc连接表ID"></el-input>
    </el-form-item>
    <el-form-item label="类型：status、data、constant" prop="type">
      <el-input v-model="dataForm.type" placeholder="类型：status、data、constant"></el-input>
    </el-form-item>
    <el-form-item label="参数编码" prop="paramCode">
      <el-input v-model="dataForm.paramCode" placeholder="参数编码"></el-input>
    </el-form-item>
    <el-form-item label="参数名称" prop="paramName">
      <el-input v-model="dataForm.paramName" placeholder="参数名称"></el-input>
    </el-form-item>
    <el-form-item label="参数地址，对应实时数据库表的列名" prop="paramAddress">
      <el-input v-model="dataForm.paramAddress" placeholder="参数地址，对应实时数据库表的列名"></el-input>
    </el-form-item>
    <el-form-item label="参数值类型：int、long、boolean、string、list 暂不使用" prop="valueType">
      <el-input v-model="dataForm.valueType" placeholder="参数值类型：int、long、boolean、string、list 暂不使用"></el-input>
    </el-form-item>
    <el-form-item label="单位 关联数据字典表label_value" prop="unitDictValue">
      <el-input v-model="dataForm.unitDictValue" placeholder="单位 关联数据字典表label_value"></el-input>
    </el-form-item>
    <el-form-item label="最小值" prop="minValue">
      <el-input v-model="dataForm.minValue" placeholder="最小值"></el-input>
    </el-form-item>
    <el-form-item label="最大值" prop="maxValue">
      <el-input v-model="dataForm.maxValue" placeholder="最大值"></el-input>
    </el-form-item>
    <el-form-item label="参数设定标准值" prop="defaultValue">
      <el-input v-model="dataForm.defaultValue" 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="0 代表不采集, 1 代表采集" prop="collection">
      <el-input v-model="dataForm.collection" placeholder="0 代表不采集, 1 代表采集"></el-input>
    </el-form-item>
    <el-form-item label="采集周期（s） 暂不使用" prop="collectionCycle">
      <el-input v-model="dataForm.collectionCycle" placeholder="采集周期（s） 暂不使用"></el-input>
    </el-form-item>
    <el-form-item label="上报周期（s） 暂不使用" prop="reportingCycle">
      <el-input v-model="dataForm.reportingCycle" placeholder="上报周期（s） 暂不使用"></el-input>
    </el-form-item>
    <el-form-item label="上报方式 暂不使用" prop="reportingMethod">
      <el-input v-model="dataForm.reportingMethod" placeholder="上报方式 暂不使用"></el-input>
    </el-form-item>
    <el-form-item label="上报编码 暂不使用" prop="reportingCode">
      <el-input v-model="dataForm.reportingCode" 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,
          plcConId: '',
          type: '',
          paramCode: '',
          paramName: '',
          paramAddress: '',
          valueType: '',
          unitDictValue: '',
          minValue: '',
          maxValue: '',
          defaultValue: '',
          description: '',
          enabled: '',
          remark: '',
          valid: '',
          creatorId: '',
          creatorName: '',
          createTime: '',
          updaterId: '',
          updaterName: '',
          updateTime: '',
          version: '',
          collection: '',
          collectionCycle: '',
          reportingCycle: '',
          reportingMethod: '',
          reportingCode: ''
        },
        dataRule: {
          plcConId: [
            { required: true, message: 'plc连接表ID不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '类型：status、data、constant不能为空', trigger: 'blur' }
          ],
          paramCode: [
            { required: true, message: '参数编码不能为空', trigger: 'blur' }
          ],
          paramName: [
            { required: true, message: '参数名称不能为空', trigger: 'blur' }
          ],
          paramAddress: [
            { required: true, message: '参数地址，对应实时数据库表的列名不能为空', trigger: 'blur' }
          ],
          valueType: [
            { required: true, message: '参数值类型：int、long、boolean、string、list 暂不使用不能为空', trigger: 'blur' }
          ],
          unitDictValue: [
            { required: true, message: '单位 关联数据字典表label_value不能为空', trigger: 'blur' }
          ],
          minValue: [
            { required: true, message: '最小值不能为空', trigger: 'blur' }
          ],
          maxValue: [
            { required: true, message: '最大值不能为空', trigger: 'blur' }
          ],
          defaultValue: [
            { 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' }
          ],
          collection: [
            { required: true, message: '0 代表不采集, 1 代表采集不能为空', trigger: 'blur' }
          ],
          collectionCycle: [
            { required: true, message: '采集周期（s） 暂不使用不能为空', trigger: 'blur' }
          ],
          reportingCycle: [
            { required: true, message: '上报周期（s） 暂不使用不能为空', trigger: 'blur' }
          ],
          reportingMethod: [
            { required: true, message: '上报方式 暂不使用不能为空', trigger: 'blur' }
          ],
          reportingCode: [
            { 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/equipmenplcparam/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.plcConId = data.equipmenplcParam.plcConId
                this.dataForm.type = data.equipmenplcParam.type
                this.dataForm.paramCode = data.equipmenplcParam.paramCode
                this.dataForm.paramName = data.equipmenplcParam.paramName
                this.dataForm.paramAddress = data.equipmenplcParam.paramAddress
                this.dataForm.valueType = data.equipmenplcParam.valueType
                this.dataForm.unitDictValue = data.equipmenplcParam.unitDictValue
                this.dataForm.minValue = data.equipmenplcParam.minValue
                this.dataForm.maxValue = data.equipmenplcParam.maxValue
                this.dataForm.defaultValue = data.equipmenplcParam.defaultValue
                this.dataForm.description = data.equipmenplcParam.description
                this.dataForm.enabled = data.equipmenplcParam.enabled
                this.dataForm.remark = data.equipmenplcParam.remark
                this.dataForm.valid = data.equipmenplcParam.valid
                this.dataForm.creatorId = data.equipmenplcParam.creatorId
                this.dataForm.creatorName = data.equipmenplcParam.creatorName
                this.dataForm.createTime = data.equipmenplcParam.createTime
                this.dataForm.updaterId = data.equipmenplcParam.updaterId
                this.dataForm.updaterName = data.equipmenplcParam.updaterName
                this.dataForm.updateTime = data.equipmenplcParam.updateTime
                this.dataForm.version = data.equipmenplcParam.version
                this.dataForm.collection = data.equipmenplcParam.collection
                this.dataForm.collectionCycle = data.equipmenplcParam.collectionCycle
                this.dataForm.reportingCycle = data.equipmenplcParam.reportingCycle
                this.dataForm.reportingMethod = data.equipmenplcParam.reportingMethod
                this.dataForm.reportingCode = data.equipmenplcParam.reportingCode
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/monitoring/equipmenplcparam/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'plcConId': this.dataForm.plcConId,
                'type': this.dataForm.type,
                'paramCode': this.dataForm.paramCode,
                'paramName': this.dataForm.paramName,
                'paramAddress': this.dataForm.paramAddress,
                'valueType': this.dataForm.valueType,
                'unitDictValue': this.dataForm.unitDictValue,
                'minValue': this.dataForm.minValue,
                'maxValue': this.dataForm.maxValue,
                'defaultValue': this.dataForm.defaultValue,
                '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,
                'collection': this.dataForm.collection,
                'collectionCycle': this.dataForm.collectionCycle,
                'reportingCycle': this.dataForm.reportingCycle,
                'reportingMethod': this.dataForm.reportingMethod,
                'reportingCode': this.dataForm.reportingCode
              })
            }).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>
