#set($excludeColumns = ["create_time","update_time","create_by","update_by"])
<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    @close="closeDialog()"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
#foreach($column in $columns)
## 当列是主键  新增不显示，编辑(dataForm.id 有值)的时候显示单不能编辑
#if($column.columnName == $pk.columnName || $excludeColumns.contains($column.columnName))
    <el-form-item label="${column.comments}" prop="${column.lowerAttrName}" v-if="dataForm.id">
        <el-input v-model="dataForm.${column.lowerAttrName}" placeholder="${column.comments}" disabled></el-input>
    </el-form-item>
#else
    <el-form-item label="${column.comments}" prop="${column.lowerAttrName}">
        <el-input v-model="dataForm.${column.lowerAttrName}" placeholder="${column.comments}"></el-input>
    </el-form-item>
#end
#end
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {addObj, getObj, putObj} from '@/api/'

    export default {
    data () {
      return {
        visible: false,
        canSubmit: false,
        dataForm: {
#foreach($column in $columns)
          ${column.lowerAttrName}: ''#if($velocityCount != $columns.size()),#end
#end
        },
        dataRule: {
#foreach($column in $columns)
#if($column.columnName != $pk.columnName && !$excludeColumns.contains($column.columnName))
          ${column.lowerAttrName}: [
            { required: true, message: '${column.comments}不能为空', trigger: 'blur' }
          ]#if($velocityCount != $columns.size()),#end

#end
#end
        }
      }
    },
    methods: {
      init (id) {
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            if (id) {
            getObj(id).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        #[[this.$refs['dataForm'].validate((valid) => {]]#
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.${pk.lowerAttrName}) {
                putObj(this.dataForm).then(data => {
                    this.$notify.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            } else {
                addObj(this.dataForm).then(data => {
                    this.$notify.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            }
          }
        })
      },
      //重置表单
      closeDialog() {
          this.$refs["dataForm"].resetFields()
      }
    }
  }
</script>
