Newer
Older
dcms_front / src / views / busAdmin / areaPersonliable / editAreaPersonliable.vue
yangqianqian on 28 Dec 2020 4 KB leaflet地图
<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false" append-to-body width="35%">
    <el-form ref="dataForm" :model="AreaPersonliableForm" :rules="rules" label-width="100px">
      <el-form-item label="网格名称" prop="areaName">
        <el-input
          v-model="AreaPersonliableForm.areaName"
          :disabled="dialogStatus === 'update'"
          placeholder="请选择责任网格"
          @focus="openAreaSelect"/>
      </el-form-item>
      <el-form-item label="责任人姓名" prop="userId">
        <el-select v-model="AreaPersonliableForm.userId" filterable clearable style="width:100%" placeholder="请选择责任人">
          <el-option v-for="user in userList" :key="user.id" :label="user.name" :value="user.id"/>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="saveData">保存</el-button>
      <el-button @click="cancel">取消</el-button>
    </div>
    <area-select-tree v-if="areaSelectShow" ref="areaSelect" @selectDone="selectArea"/>
  </el-dialog>
</template>

<script>
import AreaSelectTree from './areaSelectTree'
import { getSupervisorList, addAreaPersonliable, updateAreaPersonliable } from '@/api/busAdmin/areaPersonliable'

export default {
  name: 'EditAreaPersonliable',
  components: { AreaSelectTree },
  data() {
    return {
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      AreaPersonliableForm: {
        id: '',
        areaId: '',
        areaName: '',
        userId: ''
      },
      userList: [],
      rules: {
        areaName: [{ required: true, message: '区域不能为空', trigger: ['change'] }],
        userId: [{ required: true, message: '用户不能为空', trigger: ['blur', 'change'] }]
      },
      areaSelectShow: false
    }
  },
  mounted() {
    this.fetchUserList()
  },
  methods: {
    initDialog(dialogFormVisible, dialogStatus, row = null) {
      this.dialogFormVisible = dialogFormVisible
      this.dialogStatus = dialogStatus
      if (this.dialogStatus === 'create') {
      // 新增时,清空表格和校验
        this.resetForm()
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      } else if (this.dialogStatus === 'update') {
      // 更新时,填充数据
        console.log(row)
        this.AreaPersonliableForm.id = row.id
        this.AreaPersonliableForm.areaId = row.areaId
        this.AreaPersonliableForm.areaName = row.areaName
        this.AreaPersonliableForm.userId = row.userId
      }
    },
    resetForm() {
      this.AreaPersonliableForm = {
        id: '',
        areaId: '',
        areaName: '',
        userId: ''
      }
    },
    fetchUserList() {
      getSupervisorList().then(response => {
        this.userList = response.data
      })
    },
    openAreaSelect() {
      this.areaSelectShow = true
      setTimeout(this.$refs.areaSelect.initDialog(), 200)
    },
    selectArea(node) {
      this.AreaPersonliableForm.areaId = node.id
      this.AreaPersonliableForm.areaName = node.name
    },
    cancel() {
      this.dialogFormVisible = false
    },
    saveData() {
      if (this.dialogStatus === 'create') {
        this.createData()
      } else if (this.dialogStatus === 'update') {
        this.updateData()
      }
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          console.log('create valid success')
          console.log(this.AreaPersonliableForm)
          addAreaPersonliable(this.AreaPersonliableForm).then(response => {
            if (response.code === 200) {
              this.$message.success('添加成功')
              this.$emit('watchChild')
              this.dialogFormVisible = false
            } else {
              this.$message.error(response.message)
            }
          })
        } else {
          console.log('create valid error')
        }
      })
    },
    updateData() {
      console.log('update data')
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          console.log('update valid success')
          console.log(this.AreaPersonliableForm)
          updateAreaPersonliable(this.AreaPersonliableForm).then(response => {
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.$emit('watchChild')
              this.dialogFormVisible = false
            } else {
              this.$message.error('修改失败')
            }
          })
        } else {
          console.log('update valid error')
        }
      })
    }
  }
}
</script>

<style scoped>
.el-form-item {
  margin-bottom: 22px;
  margin-left: 20px;
  margin-right: 50px;
}
</style>