<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" prop="code">
      <el-input v-model="dataForm.code" placeholder="点位编码，对应plc" clearable />
    </el-form-item>
    <el-form-item label="点位对应的设备名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="点位对应的设备名称" clearable />
    </el-form-item>
    <el-form-item label="点位说明" prop="note">
      <el-input v-model="dataForm.note" placeholder="点位说明；关联对应表数据的code，出入点code,库位code等等" clearable />
    </el-form-item>
    <el-form-item label="类型" prop="type">
      <el-input v-model="dataForm.type" placeholder="类型：1 液压台，2 库位" clearable />
    </el-form-item>
    <el-form-item label="距离" prop="distance">
      <el-input v-model="dataForm.distance" placeholder="距离" clearable />
    </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: '',
          note: '',
          type: '',
          distance: ''
        },
        dataRule: {
          code: [
            { required: true, message: '点位编码，对应plc不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '点位对应的设备名称不能为空', trigger: 'blur' }
          ],
          note: [
            { required: true, message: '点位说明；关联对应表数据的code，出入点code,库位code等等不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '类型：1 液压台，2 库位不能为空', trigger: 'blur' }
          ],
          distance: [
            { required: true, message: '距离不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || ''
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/work/mtpointinfo/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.mtPointInfo
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/work/mtpointinfo/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'code': this.dataForm.code,
                'name': this.dataForm.name,
                'note': this.dataForm.note,
                'type': this.dataForm.type,
                'distance': this.dataForm.distance
              })
            }).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>
