Newer
Older
smartcity_env_front / src / views / device / editDevice.vue
StephanieGitHub on 27 Jul 2021 12 KB MOD:设备管理、报警阈值管理
<!--设备编辑-->
<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :fullscreen="false" width="60%" append-to-body>
    <el-form ref="dataForm" :rules="rules" :model="DeviceForm" label-well-code="right" label-width="100px">
      <div class="form-div">
        <div class="form-left">
          <el-row :gutter="10">
            <el-col :span="12">
              <el-form-item label="设备编号" prop="deviceNo">
                <el-input v-model.trim="DeviceForm.deviceNo" :disabled="isDetailMode||isEditMode" type="text" placeholder="必填"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="点位名称" prop="deviceName">
                <el-input v-model.trim="DeviceForm.deviceName" :disabled="isDetailMode" type="text" placeholder="必填"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="12">
              <el-form-item label="区/县" prop="area">
                <!--<el-input v-model.trim="DeviceForm.area" :disabled="isDetailMode" type="text" placeholder="必填"/>-->
                <el-select v-model="DeviceForm.areaCode" :disabled="isDetailMode" placeholder="请选择区/县">
                  <el-option
                  v-for="item in areaList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"/>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="详细地址" prop="position">
                <el-input v-model.trim="DeviceForm.position" :disabled="isDetailMode" type="text" placeholder="社区/道路"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="12">
              <el-form-item label="经度" prop="lng">
                <el-input v-model.trim="DeviceForm.lng" :disabled="isDetailMode" type="text" placeholder="经度,如:116.4">
                  <el-button v-if="!isDetailMode" slot="append" icon="el-icon-map-location" @click="choosePoint('edit')"/>
                  <el-button v-if="isDetailMode" slot="append" icon="el-icon-map-location" @click="choosePoint('readonly')"/>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="纬度" prop="lat">
                <el-input v-model.trim="DeviceForm.lat" :disabled="isDetailMode" type="text" placeholder="纬度,如:39.9"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="12">
              <el-form-item label="权属单位" prop="deptid">
                <dept-select v-model="DeviceForm.deptid" :need-top="false" :dept-show="true" dept-type="03" placeholder="选择权属单位"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="安装时间" prop="installDate">
                <el-date-picker
                  v-model="DeviceForm.installDate"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd"
                  type="date"
                  placeholder="选择日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="24">
              <el-form-item label="备注" prop="notes">
                <el-input v-model.trim="DeviceForm.notes" :disabled="isDetailMode" type="text" placeholder=""/>
              </el-form-item>
            </el-col>
          </el-row>
        </div>
      </div>

    </el-form>
    <div v-show="!isDetailMode" slot="footer" class="dialog-footer">
      <el-button :disabled="!canEdit" type="primary" @click="saveData">保存</el-button>
      <el-button @click="cancel">取消</el-button>
    </div>
    <!--地图选点-->
    <choose-point-dialog ref="choosepoint" @choose="chooseOver"/>
  </el-dialog>
</template>

<script>
import { addDevice, updateDevice } from '@/api/environment/device'
import { Uploadimg } from '@/api/common'
import { getAreaList, getAreaByDept } from '@/api/system/area'
import ChoosePointDialog from "../mapViews/choosePointDialog";
import DeptSelect from "../../components/DeptSelect/index";

export default {
  name: 'EditDevice',
  components: {DeptSelect, ChoosePointDialog},
  data() {
    return {
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型:create,update,detail
      DeviceForm: {
        id: '', // id
        deviceNo: '', // 编号
        deviceName: '', // 名称
        type: '1', // 设备类型
        provinceCode: this.baseConfig.province, // 省编码
        cityCode: this.baseConfig.city, // 城市编码
        areaCode: this.baseConfig.area, // 区域编码
        position: '', // 详细地址
        lng: '', // 经度
        lat: '', // 纬度
        deptid: '', // 权属单位
        installDate: '', // 安装时间
        notes: '' // 备注
      }, // 表单
      textMap: {
        update: '编辑',
        create: '新增',
        detail: '详情'
      }, // 表头显示标题
      areaList: [],
      rules: {
        deviceNo: [{ required: true, message: '设备编号不能为空', trigger: ['blur', 'change'] }],
        deviceName: [{ required: true, message: '安装点位不能为空', trigger: ['blur', 'change'] }],
        position: [{ required: true, message: '详细地址不能为空', trigger: ['blur', 'change'] }],
        area: [{ required: true, message: '区/县不能为空', trigger: ['blur', 'change'] }],
        deptid: [{ required: true, message: '权属单位不能为空', trigger: ['blur', 'change'] }],
        lng: [{ required: true, message: '经度不能为空', trigger: ['blur', 'change'] }],
        lat: [{ required: true, message: '纬度不能为空', trigger: ['blur', 'change'] }]
      }, // 前端校验规则
      isEditMode: false, // 编辑模式
      isDetailMode: false, // 详情模式
      canEdit: true
    }
  },
  created() {
    this.fetchAreaByCity()
  },
  methods: {
    /**
     * 初始化对话框
     * @param dialogStatus 对话框类型
     * @param dialogFormVisible 对话框是否可见
     * @param row 内容参数(编辑和详情需要)
     */
    initDialog: function(dialogStatus, dialogFormVisible, row = null) {
      this.dialogStatus = dialogStatus
      this.dialogFormVisible = dialogFormVisible
      if (dialogStatus === 'create') { // 如果是新增,清除验证
        this.resetForm()
        this.isEditMode = false
        this.isDetailMode = false
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      } else if (dialogStatus === 'update') { // 如果是修改,将row中数据填写到输入框中
        this.DeviceForm = {
          id: row.id, // id
          deviceNo: row.deviceNo, // 编号
          deviceName: row.deviceName, // 名称
          position: row.position, // 详细地址:社区/道路
          lng: row.lng, // 经度
          lat: row.lat, // 纬度
          provinceCode: row.provinceCode, // 纬度
          cityCode: row.cityCode, // 纬度
          areaCode: row.areaCode, // 区/县
          deptid: row.deptid, // 权属单位
          installDate: row.installDate, // 安装时间
          notes: row.notes // 备注
        }
        this.isEditMode = true
        this.isDetailMode = false
      } else {
        this.DeviceForm = {
          id: row.id, // id
          deviceNo: row.deviceNo, // 编号
          deviceName: row.deviceName, // 名称
          position: row.position, // 详细地址:社区/道路
          lng: row.lng, // 经度
          lat: row.lat, // 纬度
          provinceCode: row.provinceCode, // 纬度
          cityCode: row.cityCode, // 纬度
          areaCode: row.areaCode, // 区/县
          deptid: row.deptid, // 权属单位
          installDate: row.installDate, // 安装时间
          notes: row.notes // 备注
        }
        this.isEditMode = true
        this.isDetailMode = true
      }
    },
    // 重置表单
    resetForm() {
      this.DeviceForm = {
        id: '', // id
        deviceNo: '', // 编号
        deviceName: '', // 名称
        type: '1', // 设备类型
        provinceCode: this.baseConfig.province, // 省编码
        cityCode: this.baseConfig.city, // 城市编码
        areaCode: this.baseConfig.area, // 区域编码
        position: '', // 详细地址
        lng: '', // 经度
        lat: '', // 纬度
        deptid: '', // 权属单位
        installDate: '', // 安装时间
        notes: '' // 备注
      }
      this.photo = ''
    },
    // 保存数据
    saveData: function() {
      if (this.dialogStatus === 'update') {
        this.updateData()
      } else if (this.dialogStatus === 'create') {
        this.createData()
      }
    },
    // 新增数据
    createData: function() {
      this.canEdit = false
      this.$refs['dataForm'].validate((valid) => {
        console.log(this.DeviceForm)
        if (valid) {
          addDevice(this.DeviceForm).then(response => {
            if (response.code === 200) {
              this.$confirm('新增成功,是否继续新增?', '提示', {
                confirmButtonText: '是',
                cancelButtonText: '否',
                type: 'info'
              }).then(() => {
                this.resetForm()
                this.$nextTick(() => {
                  this.$refs['dataForm'].clearValidate()
                  this.canEdit = true
                })
              }).catch(() => {
                this.$emit('watchChild')
                this.dialogFormVisible = false
                this.canEdit = true
              })
            }
          }).catch((e) => {
            this.canEdit = true
          })
        } else {
          this.canEdit = true
        }
      })
    },
    // 修改数据
    updateData: function() {
      this.canEdit = false
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          updateDevice(this.DeviceForm).then(response => {
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.$emit('watchChild')
              this.dialogFormVisible = false
              this.canEdit = true
            }
          }).catch((e) => {
            this.canEdit = true
          })
        } else {
          this.canEdit = true
        }
      })
    },
    // 打开地图选点
    choosePoint(type) {
      let position = null
      if (this.DeviceForm.lng && this.DeviceForm.lat) {
        position = { lng: this.DeviceForm.lng, lat: this.DeviceForm.lat }
      }
      this.$refs.choosepoint.initDialog(type, position)
    },
    // 地图选点完毕
    chooseOver(marker) {
      this.$set(this.DeviceForm, 'lng', marker.lng)
      this.$set(this.DeviceForm, 'lat', marker.lat)
    },
    // 获取区域
    fetchAreaByCity() {
      getAreaList(this.DeviceForm.cityCode).then(response => {
        this.areaList = response.data
      })
    },
    // 打开地图选点
    choosePoint(type) {
      let position = null
      if (this.DeviceForm.lng && this.DeviceForm.lat) {
        position = { lng: this.DeviceForm.lng, lat: this.DeviceForm.lat }
      }
      this.$refs.choosepoint.initDialog(type, position)
    },
    // 地图选点完毕
    chooseOver(marker) {
      this.$set(this.DeviceForm, 'lng', marker.lng)
      this.$set(this.DeviceForm, 'lat', marker.lat)
    },
    cancel: function() {
      this.dialogFormVisible = false
      this.$emit('watchChild')
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .el-select{
    width: 100%;
  }
  .el-date-editor{
    width: 100%;
  }
  .form-div{
    width:100%;
    height:100%;
    display:flex;
    justify-content: space-between;
  }
  .form-left{
    flex:1;
    height:100%;
  }
  .form-right{
    width: 0px;
    height:100%;
    .avatar{
      margin-bottom: 10px;
    }
    text-align: center;
  }
</style>