Newer
Older
eryuan_iris_temperature_front / src / views / cardManage / editCard.vue
[wangxitong] on 30 May 2022 4 KB first commit
<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" append-to-body @close="cancel">
    <el-form ref="dataForm" :rules="rules" :model="cardForm" label-well-code="right" label-width="100px">
      <el-row type="flex" justify="center">
        <el-col :span="18">
          <el-form-item label="员工编号" prop="personCode">
            <el-input v-model.trim="cardForm.personCode" type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" justify="center">
        <el-col :span="18">
          <el-form-item label="卡编号" prop="cardCode">
            <el-input v-model.trim="cardForm.cardCode" type="text" placeholder="必填" show-word-limit/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="saveData">保存</el-button>
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addCard, updateCard } from '@/api/card'

export default {
  name: 'EditCard',
  data() {
    return {
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型:create,update
      cardForm: {
        personCode: '',
        cardCode: '',
        devType: '',
        devIp: '',
        description: ''
      }, // 表单
      textMap: {
        update: '编辑',
        create: '新增',
        detail: '详情'
      }, // 表头显示标题
      rules: {
        personCode: [{ required: true, message: '员工编号不能为空', trigger: ['blur', 'change'] }],
        cardCode: [{ required: true, message: '卡编号不能为空', trigger: ['blur', 'change'] }]
      }, // 前端校验规则
      isEditMode: false
    }
  },
  watch: {
  },
  mounted() {
  },
  methods: {
    // 初始化对话框
    initDialog: function(dialogStatus, dialogFormVisible, row = null) {
      this.dialogStatus = dialogStatus
      this.dialogFormVisible = dialogFormVisible
      if (dialogStatus === 'create') { // 如果是新增,清除验证
        this.resetForm()
        this.isEditMode = false
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      } else if (dialogStatus === 'update') { // 如果是修改,将row中数据填写到输入框中
        this.cardForm = {
          personCode: row.personCode,
          cardCode: row.cardCode,
          id: row.id
        }
        this.isEditMode = true
      }
    },
    // 重置表单
    resetForm() {
      this.cardForm = {
        personCode: '',
        cardCode: ''
      }
    },
    // 保存数据
    saveData: function() {
      if (this.dialogStatus === 'update') {
        this.updateData()
      } else if (this.dialogStatus === 'create') {
        this.createData()
      }
    },
    // 新增数据
    createData: function() {
      const that = this
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          addCard(that.cardForm).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
              })
            }
          })
        }
      })
    },
    // 修改数据
    updateData: function() {
      const that = this
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          updateCard(that.cardForm).then(response => {
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.$emit('watchChild')
              this.dialogFormVisible = 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%;
  }
</style>