Newer
Older
smartwell_front / src / views / systemConfig / deptPermission / components / editDeptPermission.vue
<template>
  <el-dialog append-to-body :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
    <el-form ref="dataForm" :rules="rules" :model="deviceForm" label-width="140px" label-well-code="right">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="组织机构" prop="deptid">
            <dept-select v-model="deviceForm.deptid" :need-top="true" dept-type="03" placeholder="选择组织机构"
              :disabled="dialogStatus === 'detail' ? true : false" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="默认城市编码" prop="area">
            <el-input v-model="deviceForm.area" clearable placeholder="请输入城市编码"
              :disabled="dialogStatus === 'detail' ? true : false" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="支持设备类型" prop="deviceType">
            <el-select v-model="deviceForm.deviceType" placeholder="请选择设备类型" multiple collapse-tags clearable
              :disabled="dialogStatus === 'detail' ? true : false">
              <el-option v-for="item in deviceTypeList" :key="item.value" :label="item.name" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="支持点位类型" prop="wellType">
            <el-select v-model="deviceForm.wellType" multiple collapse-tags clearable placeholder="请选择支持点位类型"
              :disabled="dialogStatus === 'detail' ? true : false">
              <el-option v-for="item in wellTypeList" :key="item.value" :label="item.name" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="地图默认中心-经度" prop="lng">
            <el-input v-model="deviceForm.lng" clearable placeholder="请输入经度"
              :disabled="dialogStatus === 'detail' ? true : false" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="地图默认中心-纬度" prop="lat">
            <el-input v-model="deviceForm.lat" clearable placeholder="请输入纬度"
              :disabled="dialogStatus === 'detail' ? true : false" />
          </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 { updateDeptPermission, addDeptPermission, deptPermissionList } from '@/api/systemConfig/deptPermission'
import { getDeviceType } from '@/api/device/device'
import { getWellType } from '@/api/well/well'
import DeptSelect from '@/components/DeptSelect'
export default {
  name: 'EditDeptPermission',
  components: { DeptSelect },
  data() {
    return {
      dialogFormVisible: false, // 对话框显示隐藏
      deviceTypeList: [], // 设备类型
      dialogStatus: '', // 对话框类型:create,update,detail
      // 对话框标题
      textMap: {
        update: '编辑',
        create: '新增',
        detail: '详情'
      },
      // 支持点位类型
      wellTypeList: [],
      // 表单内容
      deviceForm: {
        id: '',
        deptid: '', // 部门id
        wellType: '', // 支持井类型
        deviceType: '', // 支持的设备类型
        area: '', // 默认城市编码
        lng: '', // 经度
        lat: '' // 纬度
      },
      // 表单校验规则
      rules: {
        deptid: [{ required: true, message: '组织id不能为空', trigger: ['blur', 'change'] }],
        wellType: [{ required: true, message: '支持点位类型必选', trigger: ['blur', 'change'] }],
        deviceType: [{ required: true, message: '支持设备类型必选', trigger: ['blur', 'change'] }],
        lng: [{ required: true, message: '经度不能为空', trigger: ['blur', 'change'] }],
        lat: [{ required: true, message: '纬度不能为空', trigger: ['blur', 'change'] }]
      }
    }
  },
  created() {
    this.fetchDeviceType()
    this.fetchWellType()
  },
  methods: {
    // 获取设备类型
    fetchDeviceType() {
      getDeviceType().then(response => {
        this.deviceTypeList = response.data
      })
    },
    // 获取点位类型
    fetchWellType() {
      getWellType().then(response => {
        this.wellTypeList = response.data
      })
    },
    // 重置表单
    resetForm() {
      this.deviceForm = {
        id: '',
        deptid: '',
        wellType: '',
        deviceType: '',
        area: '', // 默认城市编码
        lng: '',
        lat: ''
      }
    },
    // 父子组件通信
    initDialog(dialogStatus, row = null) {
      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 = {
          id: row.id,
          deptid: row.deptid,
          wellType: row.wellType,
          deviceType: row.deviceType,
          area: row.area,
          lng: row.lng,
          lat: row.lat
        }
      }
    },
    // 保存数据
    saveData() {
      if (this.dialogStatus === 'update') {
        this.updateData()
      } else if (this.dialogStatus === 'create') {
        this.createData()
      }
    },
    // 修改数据
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          updateDeptPermission(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) {
          deptPermissionList({ deptid: this.deviceForm.deptid }).then(res => {
            if ((res.data || []).every((item) => item.deptid !== this.deviceForm.deptid)) {
              addDeptPermission(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
                  })
                }
              })
            }
            else {
              this.$confirm('该组织已经存在,是否继续添加?', '提示', {
                confirmButtonText: '是',
                cancelButtonText: '否',
                type: 'info'
              }).then(() => {
                addDeptPermission(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
                    })
                  }
                })
              })
            }
          })
          // addDeptPermission(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>

<style rel="stylesheet/scss" lang="scss" scoped>
.el-select {
  width: 100%;
}
</style>