Newer
Older
baseResourceFront / src / views / alarm / editCarThreshold.vue
zhangyingjie on 22 Mar 2021 6 KB 原车辆子系统代码提交
<template>
  <el-dialog :close-on-click-modal="false" :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" append-to-body width="700px">
    <el-form ref="dataForm" :rules="rules" :model="thresholdForm" label-position="right" label-width="180px">
      <el-row :gutter="20">
        <el-col :span="16" offset="4">
          <el-form-item label="设备离线时长(分钟)" prop="offlineTime">
            <el-input v-model.trim="thresholdForm.offlineTime" :placeholder="dialogStatus=='detail'?'':'设备离线时长(分钟)'" disabled type="text" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="16" offset="4">
          <el-form-item label="最高限速(km/h)" prop="limitSpeed">
            <el-input v-model.trim="thresholdForm.limitSpeed" :disabled="dialogStatus=='detail'" :placeholder="dialogStatus=='detail'?'':'最高限速'" type="text"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="16" offset="4">
          <el-form-item label="停留超时时长(分钟)" prop="stayTime">
            <el-input v-model.trim="thresholdForm.stayTime" :disabled="dialogStatus=='detail'" :placeholder="dialogStatus=='detail'?'':'停留超时时长'" type="text"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="16" offset="4">
          <el-form-item label="停留距离(米)" prop="stayDistance">
            <el-input v-model.trim="thresholdForm.stayDistance" :disabled="dialogStatus=='detail'" :placeholder="dialogStatus=='detail'?'':'停留距离'" type="text"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div v-if="dialogStatus !== 'detail'" slot="footer" class="dialog-footer">
      <el-button type="primary" style="width: 100px;font-size: 16px;" @click="saveData">保 存</el-button>
      <el-button style="width: 100px;font-size: 16px" @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateThreshold } from '@/api/alarm'
export default {
  name: 'EditCarThreshold',
  data() {
    return {
      deptShow: true,
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型:create,update
      thresholdForm: {
        carId: '',
        offlineTime: '',
        limitSpeed: '',
        stayTime: '',
        stayDistance: ''
      }, // 表单
      textMap: {
        update: '编辑车辆阈值',
        create: '新增车辆阈值',
        detail: '车辆阈值详情'
      }, // 表头显示标题
      rules: {
        offlineTime: [{ required: true, message: '设备离线时长不能为空', trigger: ['blur', 'change'] },
          { pattern: /^(([1-9]{1}\d*)|(0{1}))?$/, required: true, message: '请输入合法数字', trigger: ['blur', 'change'] }],
        limitSpeed: [{ required: true, message: '最高限速不能为空', trigger: ['blur', 'change'] },
          { pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/, required: true, message: '请输入合法数字,最多两位小数', trigger: ['blur', 'change'] }],
        stayTime: [{ required: true, message: '停留超时时长不能为空', trigger: ['blur', 'change'] },
          { pattern: /^(([1-9]{1}\d*)|(0{1}))?$/, required: true, message: '请输入合法数字', trigger: ['blur', 'change'] }],
        stayDistance: [{ required: true, message: '停留距离不能为空', trigger: ['blur', 'change'] },
          { pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/, required: true, message: '请输入合法数字,最多两位小数', trigger: ['blur', 'change'] }]
      } // 前端校验规则
    }
  },
  methods: {
    // 初始化对话框
    initDialog: function(dialogStatus, dialogFormVisible, row = null) {
      console.log(row)
      this.dialogStatus = dialogStatus
      this.dialogFormVisible = dialogFormVisible
      if (dialogStatus === 'create') { // 如果是新增,清除验证
        this.resetForm()
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      } else if (dialogStatus === 'update' || dialogStatus === 'detail') { // 如果是修改,将row中数据填写到输入框中
        this.thresholdForm = {
          carId: row.carId,
          offlineTime: row.offlineTime,
          limitSpeed: row.limitSpeed,
          stayTime: row.stayTime,
          stayDistance: row.stayDistance
        }
      }
    },
    // 重置表单
    resetForm() {
      this.thresholdForm = {
        carId: '',
        offlineTime: '',
        limitSpeed: '',
        stayTime: '',
        stayDistance: ''
      }
    },
    // 保存数据
    saveData: function() {
      if (this.dialogStatus === 'update') {
        this.updateData()
      } else if (this.dialogStatus === 'create') {
        // this.createData()
      }
    },
    // 新增数据
    // createData: function() {
    //   this.$refs['dataForm'].validate((valid) => {
    //     console.log(this.thresholdForm)
    //     if (valid) {
    //       addDeviceInfo(this.thresholdForm).then(response => {
    //         if (response.code === 200) {
    //           this.$confirm('新增成功,是否继续新增?', '提示', {
    //             confirmButtonText: '是',
    //             cancelButtonText: '否',
    //             type: 'info'
    //           }).then(() => {
    //             this.resetForm()
    //             this.$nextTick(() => {
    //               this.$refs['dataForm'].clearValidate()
    //             })
    //           }).catch(() => {
    //             this.$emit('watchChild')
    //             this.dialogFormVisible = false
    //           })
    //         }
    //       })
    //     }
    //   })
    // },
    // 修改数据
    updateData: function() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          console.log(this.thresholdForm)
          updateThreshold(this.thresholdForm).then(response => {
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.$emit('watchChild')
              this.dialogFormVisible = false
            }
          })
        }
      })
    },
    cancel: function() {
      this.dialogFormVisible = false
      this.$emit('watchChild')
    },
    changeiot(val) {
      this.thresholdForm.iot = val.iot
      this.thresholdForm.deviceId = val.id
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  $tableTitleHeight: 40px;
  .el-form-item {
    margin-bottom: 22px;
  }
  .el-select{
    width: 100%;
  }
  .table-title {
    background-color: rgba(243, 243, 243, 1);
    height: $tableTitleHeight;
    .title-header {
      line-height: $tableTitleHeight;
      color: #606266;
      font-size: 14px;
      font-weight: bold;
      i {
        margin-left: 5px;
        margin-right: 5px;
      }
    }
  }
</style>