Newer
Older
baseResourceFront / src / views / deviceManage / editLamp.vue
yangqianqian on 6 Apr 2021 12 KB 按照新UI进行修改
<template>
  <el-dialog :title="textMap[dialogStatus]" :close-on-click-modal="false" :visible.sync="dialogFormVisible" append-to-body>
    <el-form ref="dataForm" :rules="rules" :model="lampForm" size="small" label-well-code="right" label-width="110px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="路灯编号" prop="lampCode">
            <el-input v-model.trim="lampForm.lampCode" :disabled="!isEditMode" :maxlength="15" clearable type="text" placeholder="必填" show-word-limit/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="路灯名称" prop="lampName">
            <el-input v-model.trim="lampForm.lampName" :disabled="!isEditMode" :maxlength="30" clearable type="text" placeholder="必填" show-word-limit/>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="路灯类型" prop="lampType">
            <el-select v-model="lampForm.lampType" :disabled="!isEditMode" placeholder="请选择路灯类型" clearable>
              <el-option v-for="item in typeList" :key="item.id" :label="item.typeName" :value="item.id"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="路灯功率" prop="power">
            <el-input v-model.trim="lampForm.power" :disabled="!isEditMode" :maxlength="30" clearable type="text" placeholder="(单位:瓦,非必填)" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="所属组织">
            <dept-select v-model="lampForm.deptId" :disabled="!isEditMode" :dept-show="true" :need-top="true" placeholder="所属组织"/>
          </el-form-item>
        </el-col>
        <!--<el-col :span="12">-->
        <!--<el-form-item label="电信设备编号">-->
        <!--<el-input v-model.trim="lampForm.teleId" clearable type="text" placeholder="非必填" />-->
        <!--</el-form-item>-->
        <!--</el-col>-->
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="灯头型号" prop="lampModel">
            <el-input v-model.trim="lampForm.lampModel" :disabled="!isEditMode" :maxlength="30" clearable type="text" placeholder="非必填" show-word-limit />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电源型号">
            <el-input v-model.trim="lampForm.supplyModel" :disabled="!isEditMode" :maxlength="30" clearable type="text" placeholder="非必填" show-word-limit />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="关联路灯杆">
            <el-select v-model="lampForm.lamppostId" :disabled="!isEditMode" placeholder="暂不关联路灯杆" filterable clearable>
              <el-option v-for="item in noBindLamppostList" :key="item.id" :label="item.lamppostName" :value="item.id"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="关联控制器">
            <el-select ref="linkController" v-model="lampForm.controllerId" :disabled="!isEditMode" placeholder="暂不关联控制器" filterable clearable @change="checkControllerType">
              <el-option v-for="item in noBindControllerList" :key="item.id" :label="item.controllerCode" :value="item.id"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="关联回路">
            <el-select v-model="lampForm.circuitId" :disabled="!isEditMode" placeholder="暂不关联回路" clearable>
              <el-option v-for="item in circuitList" :key="item.id" :label="item.circuitName" :value="item.id"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-show="isDualController" label="关联线路">
            <el-select v-model="lampForm.controllerLine" :disabled="!isEditMode" placeholder="暂不关联线路" clearable>
              <el-option v-for="item in lineList" :key="item.name" :label="item.name" :value="item.name"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="btnLoading" type="primary" @click="saveData">保存</el-button>
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import DeptSelect from '@/components/DeptSelect'
import { addLamp, editLamp, listNoBindLamppost, listNoBindController, getAllCircuitList } from '@/api/system/device'
import { getAllLampType } from '@/api/system/base'

export default {
  name: 'EditLamp',
  components: { DeptSelect },
  data() {
    const typeValidator = (rule, value, callback) => {
      if (value === 0 || value === '' || typeof (value) === 'undefined') {
        return callback(new Error('请选择一项'))
      } else {
        return callback()
      }
    }

    return {
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型:create,update,detail
      lampForm: {
        id: '', // id
        lampCode: '', // 路灯杆编号
        lampName: '', // 路灯杆名称
        lampType: '', // 路灯杆类型
        power: '', // 灯杆高度
        lampModel: '',
        supplyModel: '',
        deptId: '',
        // teleId: '',
        lamppostId: '', // 关联灯杆
        controllerId: '', // 关联控制器
        controllerLine: '', // 关联线路(主线/辅线)
        circuitId: '' // 关联回路
      }, // 表单
      textMap: {
        update: '编辑路灯',
        create: '新增路灯',
        detail: '详情'
      }, // 表头显示标题
      rules: {
        lampCode: [{ required: true, message: '路灯编号不能为空', trigger: ['blur', 'change'] }],
        lampName: [{ required: true, message: '路灯名称不能为空', trigger: ['blur', 'change'] }],
        lampType: [{ required: true, message: '所在道路不能为空', trigger: ['blur', 'change'], validator: typeValidator }]
      }, // 前端校验规则
      typeList: [], // 路灯类型列表
      lineList: [{ 'name': '主线' }, { 'name': '辅线' }],
      noBindLamppostList: [], // 未绑定的灯杆列表
      noBindControllerList: [], // 未绑定的控制器列表
      circuitList: [], // 回路列表
      isEditMode: false,
      isDualController: false, // 控制器是否双灯
      btnLoading: true // 保存按钮是否不允许点击
    }
  },
  created() {
    this.fetchLampType()// 获取设备类别
    this.fetchNoBindLamppost() // 获取未绑定的灯杆
    this.fetchNoBindController() // 获取可绑定的控制器
    this.fetchCircuitList() // 获取回路列表
  },
  methods: {
    // 初始化对话框
    initDialog: function(dialogStatus, dialogFormVisible, row = null) {
      this.btnLoading = false
      this.dialogStatus = dialogStatus
      this.dialogFormVisible = dialogFormVisible
      this.isDualController = false
      this.lampForm.controllerLine = ''
      if (dialogStatus === 'create') { // 如果是新增,清除验证
        this.resetForm()
        this.isEditMode = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      } else if (dialogStatus === 'update') { // 如果是修改,将row中数据填写到输入框中
        this.lampForm = {
          id: row.id,
          lampCode: row.lampCode,
          lampName: row.lampName,
          lampType: row.lampType,
          power: row.power,
          lampModel: row.lampModel,
          supplyModel: row.supplyModel,
          deptId: row.deptId,
          lamppostId: row.lamppostId,
          controllerId: row.controllerId,
          circuitId: row.circuitId,
          // teleId: row.teleId,
          controllerLine: row.controllerLine
        }
        this.isEditMode = true

        this.fetchNoBindController()
        this.fetchNoBindLamppost()

        if (row.controllerLine !== '') {
          this.isDualController = true
        }
      } else if (dialogStatus === 'detail') { // 如果是修改,将row中数据填写到输入框中
        this.lampForm = {
          id: row.id,
          lampCode: row.lampCode,
          lampName: row.lampName,
          lampType: row.lampType,
          power: row.power,
          lampModel: row.lampModel,
          supplyModel: row.supplyModel,
          deptId: row.deptId,
          lamppostId: row.lamppostId,
          controllerId: row.controllerId,
          circuitId: row.circuitId,
          // teleId: row.teleId,
          controllerLine: row.controllerLine
        }
        this.isEditMode = false

        this.fetchNoBindController()
        this.fetchNoBindLamppost()

        if (row.controllerLine !== '') {
          this.isDualController = true
        }
      }
    },
    // 检查控制器类型:单灯/双灯
    checkControllerType() {
      const id = this.lampForm.controllerId
      const selectedController = this.noBindControllerList.filter(item => {
        if (item.id.indexOf(id) >= 0) {
          return item
        }
      })

      // 获取控制器类型
      const controllerType = selectedController[0].controllerType
      if (controllerType === '2') {
        this.isDualController = true
      } else {
        this.isDualController = false
        this.lampForm.controllerLine = ''
      }
    },
    fetchLampType() {
      getAllLampType().then(response => {
        if (response.code === 200) {
          this.typeList = response.data
          console.log(this.typeList)
        }
      })
    },
    fetchNoBindLamppost() {
      console.log('lamppost not bind:' + this.lampForm.id)
      listNoBindLamppost({ 'lampId': this.lampForm.id }).then(response => {
        if (response.code === 200) {
          this.noBindLamppostList = response.data
        }
      })
    },
    fetchNoBindController() {
      console.log('controller not bind:' + this.lampForm.id)
      listNoBindController({ 'lampId': this.lampForm.id }).then(response => {
        if (response.code === 200) {
          this.noBindControllerList = response.data
        }
      })
    },
    fetchCircuitList() {
      getAllCircuitList().then(response => {
        if (response.code === 200) {
          this.circuitList = response.data
        }
      })
    },
    // 清除数据
    resetForm() {
      this.lampForm = {
        id: '', // id
        lampCode: '', // 路灯杆编号
        lampName: '', // 路灯杆名称
        lampType: '', // 路灯杆类型
        power: '', // 灯杆高度
        lampModel: '',
        supplyModel: '',
        deptId: '',
        // teleId: '',
        lamppostId: ''
      }

      this.btnLoading = false
    },
    // 保存数据
    saveData: function() {
      this.btnLoading = true

      if (this.dialogStatus === 'update') {
        this.updateData()
      } else if (this.dialogStatus === 'create') {
        this.createData()
      }
    },
    // 新增数据
    createData: function() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          addLamp(this.lampForm).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
              })
            } else {
              this.btnLoading = false
            }
          }).catch(() => {
            this.btnLoading = false
          })
        } else {
          this.btnLoading = false
        }
      })
    },
    // 修改数据
    updateData: function() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          editLamp(this.lampForm).then(response => {
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.cancel()
            } else {
              this.btnLoading = false
            }
          }).catch(() => {
            this.btnLoading = false
          })
        } else {
          this.btnLoading = false
        }
      })
    },
    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%;
  }
  .dialog-footer {
    margin-top: -20px;
    text-align: center;
  }
</style>