Newer
Older
GDT_FRONT / src / views / property / editMoney.vue
[wangxitong] on 21 Jun 2022 7 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="20" :offset="2">
          <el-form-item label="预算内容" prop="name">
            <el-input v-model.trim="form.name" :disabled="isEditMode" type="text" placeholder="必填" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="20" :offset="2">
          <el-form-item label="预算类型" prop="budgetType">
            <el-select v-model="form.budgetType" placeholder="必选" :disabled="isEditMode" >
              <el-option
                v-for="item in typeList"
                :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="20" :offset="2">
          <el-form-item label="维修花费金额" prop="money">
            <el-input v-model.trim="form.money" :disabled="isEditMode" type="text" placeholder="必填" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="20" :offset="2">
          <el-form-item label="维修时间" prop="time">
            <el-date-picker
              style="width: 100%"
              v-model="form.time"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="必选"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="20" :offset="2">
          <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 slot="footer" class="dialog-footer" v-if="!isEditMode">
      <el-button :loading="btnLoading" type="primary" @click="saveData">
        保存
      </el-button>
      <el-button @click="cancel">
        取消
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import {  addMoney, updateMoney } from '@/api/property'
import { validateFloatPlus } from '@/utils/validate'
import { getDictByCode } from '@/api/system/dict'
export default {
  name: 'EditMoney',
  data() {
    const validateNum = (rule, value, callback) => {
      if (value !== '') {
        if (validateFloatPlus(value,0,100000000) === true) {
          callback()
        } else {
          callback(new Error('请输入合法花费金额'))
        }
      } else {
        callback(new Error('维修花费金额不能为空'))
      }
    }
    return {
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型:create,update
      isEditMode: true,
      typeList:[],
      form: {
        id: '',
        name: '',
        budgetType :'',
        money:'',
        time:'',
        remarks:''
      }, // 表单
      textMap: {
        update: '编辑危险级别',
        create: '新增危险级别',
        detail:  '危险级别详情'
      }, // 表头显示标题
      btnLoading: false, // 保存按钮的加载中状态
      rules: {
        name: [{ required: true, message: '预算内容不能为空', trigger: ['blur', 'change'] }],
        time: [{ required: true, message: '维修时间不能为空', trigger: ['blur', 'change'] }],
        budgetType : [{ required: true, message: '预算类型必选', trigger: ['blur', 'change'] }],
        money: [{ required: true, validator: validateNum, trigger: ['blur', 'change'] }]
      } // 前端校验规则
    }
  },
  computed: {
  },
  created() {
    this.fetchOptions()

  },
  methods: {
    fetchOptions(){
      // 物业公司
      getDictByCode('budgetType').then(response => {
        if (response.code === 200) {
          this.typeList = 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,
          name: row.name,
          budgetType :row.budgetType,
          money:row.money,
          time:row.time,
          remarks:row.remarks
        }
        this.isEditMode = false
      }else if (dialogStatus === 'detail') {
        this.form = {
          id: row.id,
          code: row.code,
          name: row.name,
          description:row.description,
          weight:row.weight,
          remarks:row.remarks
        }
        this.isEditMode = true
      }
    },
    // 重置表单
    resetForm() {
      this.form = {
        id: row.id,
        name: row.name,
        budgetType :row.budgetType,
        money:row.money,
        time:row.time,
        remarks:row.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
          addMoney(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
          })
        }
      })
    },
    // 修改数据
    updateData: function() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.btnLoading = true
          updateMoney(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
          })
        }
      })
    },
    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>