Newer
Older
smartcity_video / src / views / device / editGroup.vue
wangxitong on 29 May 2023 8 KB first commit
<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" append-to-body>
    <el-form ref="dataForm" :rules="rules" :model="form" label-position="right" label-width="130px">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="门禁分组" prop="groupName">
            <el-input v-model.trim="form.groupName" :disabled="isEditMode" type="text" placeholder="必填" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="分组描述" prop="description">
            <el-input v-model.trim="form.description" :disabled="isEditMode" type="text" placeholder="必填" />
          </el-form-item>
        </el-col>
<!--        <el-col :span="24">-->
<!--          <el-form-item label="通道控制" prop="status">-->
<!--            <el-select v-model="form.status" placeholder="必填" :disabled="isEditMode">-->
<!--              <el-option-->
<!--                v-for="item in statusList"-->
<!--                :key="item.value"-->
<!--                :label="item.name"-->
<!--                :value="item.value"-->
<!--              />-->
<!--            </el-select>-->
<!--          </el-form-item>-->
<!--        </el-col>-->
        <el-col :span="24">
          <el-form-item label="门禁编号" prop="gateCodes">
            <el-select v-model="form.gateCodes" placeholder="必填" :disabled="isEditMode" multiple>
              <el-option
                v-for="item in deviceList"
                :key="item.devCode"
                :label="item.devCode+'( '+item.devName+' : ' + item.ip + ' )'"
                :value="item.devCode"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="备注">
            <el-input v-model.trim="form.remarks" :disabled="isEditMode" :rows="3" type="textarea" placeholder="备注" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div v-if="!isEditMode" 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 {addGroup, getDeviceList, updateGroup} from '@/api/device'
import { getDictByCode } from '@/api/system/dict'

export default {
  name: 'EditGroup',
  data() {
    return {
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型:create,update
      isEditMode: true,
      form: {
        id: '',
        groupName: '',
        description: '',
        gateCodes: '',
        status: '',
        remarks: ''
      }, // 表单
      statusList: [],
      deviceList: [],
      textMap: {
        update: '编辑闸机分组',
        create: '新增闸机分组',
        detail: '闸机分组详情'
      }, // 表头显示标题
      btnLoading: false, // 保存按钮的加载中状态
      rules: {
        groupName: [{ required: true, message: '设备名称不能为空', trigger: ['blur', 'change'] }],
        devType: [{ required: true, message: '设备类型不能为空', trigger: ['blur', 'change'] }],
        // status: [{ required: true, message: '设备状态不能为空', trigger: ['blur', 'change'] }],
        gateCodes: [{ required: true, message: '门禁编号不能为空', trigger: ['blur', 'change'] }],
        description: [{ required: true, message: '分组描述不能为空', trigger: ['blur', 'change'] }]
      } // 前端校验规则
    }
  },
  computed: {
  },
  created() {
    this.fetchTypeList()
  },
  methods: {
    fetchTypeList() {
      const params = {
        devType: '1'
      }
      getDeviceList(params).then(response => {
        if (response.code === 200) {
          this.deviceList = response.data
        }
      })
      getDictByCode('deviceStatus').then(response => {
        if (response.code === 200) {
          this.statusList = response.data
        }
      })
    },
    // 初始化对话框
    initDialog: function(dialogStatus, row = null) {
      this.dialogStatus = dialogStatus
      this.dialogFormVisible = true
      this.btnLoading = false
      if (dialogStatus === 'create') { // 如果是新增,清除验证
        this.resetForm()
        this.isEditMode = false
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      } else if (dialogStatus === 'update') { // 如果是修改,将row中数据填写到输入框中
        this.form = {
          id: row.id,
          groupName: row.groupName,
          description: row.description,
          gateCodes: row.gateCodes.split(','),
          status: row.status,
          remarks: row.remarks
        }
        this.isEditMode = false
      } else if (dialogStatus === 'detail') {
        this.form = {
          id: row.id,
          groupName: row.groupName,
          description: row.description,
          gateCodes: row.gateCodes.split(','),
          status: row.status,
          remarks: row.remarks
        }
        this.isEditMode = true
      }
    },
    // 重置表单
    resetForm() {
      this.form = {
        id: '',
        groupName: '',
        description: '',
        gateCodes: '',
        status: '',
        remarks: ''
      }
    },
    // 保存数据
    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.form)
        if (valid) {
          this.btnLoading = true
          let tmp = ''
          this.form.gateCodes.forEach(item => {
            tmp = tmp + item + ','
          })
          this.form.gateCodes = tmp
          this.form.gateCodes = this.form.gateCodes.substring(0, this.form.gateCodes.lastIndexOf(','))
          addGroup(this.form).then(response => {
            if (response.code === 200) {
              this.$confirm('新增成功,是否继续新增?', '提示', {
                confirmButtonText: '是',
                cancelButtonText: '否',
                type: 'info'
              }).then(() => { // 选是
                this.resetForm()
                this.btnLoading = false
                this.$nextTick(() => {
                  this.$refs['dataForm'].clearValidate()
                })
              }).catch(() => { // 选否,关闭窗口,并刷新页面
                this.$emit('watchChild')
                this.dialogFormVisible = false
              })
            }
          }).catch(_ => { // 异常情况,loading置为false
            this.btnLoading = false
            this.resetForm()
            this.$nextTick(() => {
              this.$refs['dataForm'].clearValidate()
            })
          })
        }
      })
    },
    // 修改数据
    updateData: function() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.btnLoading = true
          let tmp = ''
          this.form.gateCodes.forEach(item => {
            tmp = tmp + item + ','
          })
          this.form.gateCodes = tmp
          this.form.gateCodes = this.form.gateCodes.substring(0, this.form.gateCodes.lastIndexOf(','))
          updateGroup(this.form).then(response => {
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.$refs['dataForm'].clearValidate()
              this.$emit('watchChild')
              this.dialogFormVisible = false
            }
          }).catch(_ => { // 异常情况,loading置为false
            this.btnLoading = false
            this.resetForm()
            this.$nextTick(() => {
              this.$refs['dataForm'].clearValidate()
            })
          })
        }
      })
    },
    cancel: function() {
      this.dialogFormVisible = false
      this.$emit('watchChild')
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

  .el-dialog{
    width:700px
  }
  .el-select{
    width: 100%;
  }
</style>