<template>
  <div>
    <Form ref="form" :rules="formValidate" :label-width="90" :model="form">
#foreach ($column in $columnList)
#if($column.columnName != 'id' && $column.fieldName != 'updateTime' && $column.fieldName != 'createTime')
          <FormItem label="${column.columnDesc}" prop="${column.fieldName}">
#if($column.isNumber)
            <Input type="number" v-model.number="form.${column.fieldName}" />
#end
#if(!$column.isNumber)
            <Input v-model="form.${column.fieldName}" />
#end
          </FormItem>
#end
#end
    </Form>
    <Row class="code-row-bg" justify="end" type="flex">
      <Button @click="cancel" style="margin-right:10px">取消</Button>
      <Button @click="save" type="primary">保存</Button>
    </Row>
  </div>
</template>
<script>
  import { ${moduleVar}Api } from '@/api/${webModuleName}';
  export default {
    name: '${moduleClass}ListForm',
    components: {
    },
    props: {
      //是否为更新表单
      isUpdate: {
        type: Boolean,
        default: true
      },
      //更新的表单数据对象
      updateData: {
        type: Object
      }
    },
    data() {
      return {
        //表单数据
        form: {
#foreach ($column in $columnList)
#if($column.columnName != 'id' && $column.fieldName != 'updateTime' && $column.fieldName != 'createTime')
         //${column.columnDesc}
         ${column.fieldName}:null,
#end
#end
        },
        //表单验证
        formValidate: {
#foreach ($column in $columnList)
#if($column.columnName != 'id' && $column.fieldName != 'updateTime' && $column.fieldName != 'createTime')
        //${column.columnDesc}
#if($column.isNumber)
        ${column.fieldName}:[{ type:'number',required: true, message: '请输入${column.columnDesc}', trigger: 'blur' }],
#end
#if(!$column.isNumber)
        ${column.fieldName}:[{ required: true, message: '请输入${column.columnDesc}', trigger: 'blur' }],
#end
#end
#end
        }
      };
    },
  watch: {
      updateData: function(newValue, oldValue) {
          this.$refs['form'].resetFields();
          if (this.isUpdate) {
              for (let k in this.form) {
                  this.$set(this.form, k, newValue[k]);
              }
              this.$set(this.form, 'id', newValue['id']);
          }
      },
      isUpdate: function(newValue, oldValue) {
          if (!newValue) {
              this.resetForm();
              this.$refs['form'].resetFields();
          }
      }
  },
    created() {},
    mounted() {},
    methods: {
      cancel() {
        this.$emit('on-form-close');
      },
      save() {
       this.${VueRefs}['form'].validate(valid => {
         if (valid) {
           if (this.isUpdate) {
            this.update();
           } else {
             this.add();
           }
         } else {
          this.$Message.error('参数验证错误，请仔细填写表单数据!');
         }
       });
      },
    resetForm() {
        this.form = {
#foreach ($column in $columnList)
#if( $column.fieldName != 'updateTime' && $column.fieldName != 'createTime')
          //${column.columnDesc}
          ${column.fieldName}:null,
#end
#end
        };
        this.$refs['form'].resetFields();
      },
      async add() {
        this.$Spin.show();
        let res = await ${moduleVar}Api.add${moduleClass}(this.form);
        this.${ViewUIMessage}.success(res.msg);
        this.$Spin.hide();
        this.resetForm();
        this.$emit('on-form-close');
      },
      async update() {
        this.$Spin.show();
        let res = await ${moduleVar}Api.update${moduleClass}(this.form);
        this.${ViewUIMessage}.success(res.msg);
        this.$Spin.hide();
        this.resetForm();
        this.$emit('on-form-close');
      }
    }
  };
</script>