Newer
Older
smartwell_front / src / views / deviceManage / deviceModel / components / editDeviceModel.vue
yuexiaosheng on 24 Jun 2022 6 KB fix<main>:联调接口
<template>
  <el-dialog
    append-to-body
    :title="textMap[dialogStatus]"
    :visible.sync="dialogFormVisible"
  >
    <el-form
      ref="dataForm"
      :rules="rules"
      :model="deviceForm"
      label-width="100px"
      label-well-code="right"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="设备型号" prop="modelName">
            <el-input
              v-model.trim="deviceForm.modelName"
              type="text"
              clearable
              placeholder="请输入型号名称"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备类型" prop="deviceType">
            <el-select
              v-model="deviceForm.deviceType"
              placeholder="请选择设备类型"
              clearable
            >
              <el-option
                v-for="item in deviceTypeList"
                :key="item.value"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="通信方式" prop="communication">
            <el-select
              v-model="deviceForm.communication"
              clearable
              multiple
              placeholder="请选择通信方式"
              :disabled="dialogStatus === 'detail' ? true : false"
              @change="selectChange"
            >
              <el-option
                v-for="item in commuList"
                :key="item.value"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div
      v-show="dialogStatus === 'detail' ? false : true"
      slot="footer"
      class="dialog-footer"
    >
      <el-button type="primary" @click="saveData">
        保存
      </el-button>
      <el-button @click="dialogFormVisible = false">
        取消
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { communicationType } from '@/api/well/well'
import { getDevice, updateDeviceModel, addDeviceModel } from '@/api/device/deviceModel'
export default {
  name: 'EditDeviceModel',
  data() {
    return {
      dialogFormVisible: false, // 对话框显示隐藏
      dialogStatus: '', // 对话框类型:create,update,detail
      deviceTypeList: [],
      commuList: [],
      // 对话框标题
      textMap: {
        update: '编辑',
        create: '新增',
        detail: '详情'
      },
      // 表单内容
      deviceForm: {
        id: '',
        modelName: '',
        deviceType: '',
        communication: []
      },
      // 表单校验规则
      rules: {
        id: [{ required: true, message: '型号编号不能为空', trigger: ['blur', 'change'] }],
        modelName: [{ required: true, message: '型号名称必填', trigger: ['blur', 'change'] }],
        deviceType: [{ required: true, message: '设备主键必填', trigger: ['blur', 'change'] }],
        communication: [{ required: true, message: '通讯方式必选', trigger: ['blur', 'change'] }]
      }
    }
  },
  created() {
    this.fetchDeviceType()
    this.fetchcommunType()
  },
  methods: {
    selectChange() {
      console.log(this.deviceForm.communication)
    },
    // 获取通讯方式
    fetchcommunType() {
      communicationType().then(response => {
        console.log(response, '通讯方式')
        this.commuList = response.data
      })
    },
    // 获取设备型号
    fetchDeviceType() {
      getDevice(this.listQuery).then(response => {
        this.deviceTypeList = response.data
      })
    },
    // 重置表单
    resetForm() {
      this.deviceForm = {
        id: '',
        modelName: '',
        deviceType: '',
        communication: ''
      }
    },
    // 父子组件通信
    initDialog(dialogStatus, row = null) {
      console.log(row, '========')
      this.dialogFormVisible = true
      this.dialogStatus = dialogStatus
      if (this.dialogStatus === 'create') {
        this.resetForm()
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      } else if (this.dialogStatus === 'update' || this.dialogStatus === 'detail') {
        this.deviceForm = {
          modelName: row.modelName,
          deviceType: row.deviceType,
          communication: row.communication.split(','),
          id: row.id
        }
      }
    },
    // 保存数据
    saveData() {
      if (this.dialogStatus === 'update') {
        this.updateData()
      } else if (this.dialogStatus === 'create') {
        this.createData()
      }
    },
    // 修改数据
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.deviceForm.communication = this.deviceForm.communication.toString()
          updateDeviceModel(this.deviceForm).then(response => {
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.$emit('watchChild')
              this.dialogFormVisible = false
            }
          })
        }
      })
    },
    // 新增数据
    createData: function() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.deviceForm.communication = this.deviceForm.communication.toString()
          addDeviceModel(this.deviceForm).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
              })
            }
          })
        }
      })
    }
  }
}
</script>