#set( $listSize = $!{dictColumns.size()})
##判断是否包含排序号字段
#set( $hasSort = 0)
##判断是否包含用户选择框的标志位
#set( $hasUser = 0)
##判断是否包含用户选择框的标志位
#set( $hasDept = 0)
##循环全部字段进行判断
#foreach($column in $columns)
#if($column.htmlType == "user")
#set( $hasUser = $hasUser + 1)
#elseif($column.htmlType == "dept")
#set( $hasDept = $hasDept + 1)
#end   
#end
##循环编辑的字段进行判断
#foreach ($column in $editColumns)
#if($column.columnName == "sort")
#set( $hasSort = $hasSort + 1)
#break
#end
#end
import AntModal from '@/components/pt/dialog/AntModal'
import { get${BusinessName}, add${BusinessName}, update${BusinessName}#if($hasSort > 0), findMaxSort#end#foreach($column in $uniqueColumns)#set($attrName = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)}), check${attrName}Unique#end } from '@/api/${moduleName}/${businessName}'
#if($hasUser > 0)
import SelectUser from '@/components/pt/selectUser/SelectUser'
#end
#if($hasDept > 0)
import SelectDept from '@/components/pt/selectDept/SelectDept'
#end
#if($hasAttach)
import SysUpload from '@/components/pt/uploader'
#end

export default {
  name: 'CreateForm',
  props: {
#foreach ($column in $dictColumns)
    ${column.javaField}Options: {
      type: Array,
      required: true
    }#if($foreach.hasNext || ${isSub} == 'true'),
#end
#end
#if(${isSub} == 'true')
    ${subTableFkNameUnCap}: {
      type: String,
      required: true
    }
  },
#else

  },
#end
  components: {
    AntModal#if($hasDept > 0 || $hasUser > 0 || $hasAttach),
#end
#if($hasUser > 0)
    SelectUser#if($hasDept > 0 || $hasAttach),
#end
#end
#if($hasDept > 0)
    SelectDept#if($hasAttach),
#end
#end
#if($hasAttach)
    SysUpload
#else

#end
  },
  data () {
#foreach($column in $uniqueColumns)
#set($attrName = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
#set($parentheseIndex=$column.columnComment.indexOf("（"))
#if($parentheseIndex != -1)
#set($comment=$column.columnComment.substring(0, $parentheseIndex))
#else
#set($comment=$column.columnComment)
#end
    const validate${attrName} = (rule, value, callback) => {
      if (value === '' || value === undefined || value === null) {
        callback(new Error('${comment}不允许为空'))
      } else {
        check${attrName}Unique(this.form.id, this.form.${column.javaField})
        callback()
      }
    }
#end
    return {
      open: false,
      closeDialog: true,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      loading: false,
      disabled: false,
      total: 0,
      id: undefined,
      formTitle: '添加${functionName}',
      // 表单参数
      form: {},
#if($hasAttach)
      attachmentRefName: 'addUploaderFile', // 标志表单是否含有附件
      formId: '',
#end
      rules: {
#foreach($column in $requireColumns)
#set($attrName = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
#set($parentheseIndex=$column.columnComment.indexOf("（"))
#if($parentheseIndex != -1)
#set($comment=$column.columnComment.substring(0, $parentheseIndex))
#else
#set($comment=$column.columnComment)
#end
        ${column.javaField}: [{ required: true, message: '${comment}不能为空',#foreach($uniqueColumn in $uniqueColumns)#if($column.javaField == $uniqueColumn.javaField) validator: validate${attrName},#break#end#end trigger: 'blur' }]#if($foreach.hasNext),
#else

#end
#end
      }
    }
  },
  filters: {},
  created () {},
  computed: {},
  watch: {},
  mounted () {},
  methods: {
    onClose () {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 表单重置
    reset () {
      this.form = {
        id: undefined,
#foreach ($column in $editColumns)
#set($javaFieldValue = "undefined")
#if($column.javaField == "status")
#set($javaFieldValue = "'0'")
#else
#set($javaFieldValue = "undefined")
#end
        ${column.javaField}: ${javaFieldValue}#if($foreach.hasNext),
#else

#end
#end
      }
#if($hasAttach)
      if (this.formId) {
        // 清空附件的formId
        this.formId = ''
      }
#end
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset()
#if($hasSort > 0)
      /** 获取最大编号 */
      findMaxSort().then(response => {
        this.form.sort = response.data
        this.open = true
        this.formTitle = '添加${functionName}'
      })
#else
      this.open = true
      this.formTitle = '添加${functionName}'
#end
#if(${isSub} == 'true')
    this.form.${subTableFkNameUnCap} = this.${subTableFkNameUnCap}
#end
    },
#if($hasCopyRecordOption)
    /** 复制按钮操作 */
    handleCopy (id) {
      this.formTitle = '复制${functionName}'
      this.reset()
      this.open = true
      this.spinning = !this.spinning
      const ${businessName}Id = id
      get${BusinessName}(${businessName}Id).then(response => {
#foreach($column in $columns)
#set($javaField=$column.javaField)
#if($column.htmlType == "selectMultiple" || $column.htmlType == "checkbox")
        if (response.data.$javaField !== null) {
          response.data.$javaField = response.data.$!{javaField}.split(',')
        } else {
          response.data.$javaField = []
        }
#elseif($column.htmlType == "user" || $column.htmlType == "dept")
        response.data.$javaField = { ids: response.data.$javaField, names: response.data.$!{javaField}Name }
#end
#end
        this.form = response.data
        this.form.id = undefined
        this.spinning = !this.spinning
#if($hasSort > 0)
        /** 获取最大编号 */
        findMaxSort().then(response => {
          this.form.sort = response.data
        })
#end
      })
    },
#end
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset()
      this.open = true
      this.spinning = !this.spinning
      const ${businessName}Id = row.id
      get${BusinessName}(${businessName}Id).then(response => {
#foreach($column in $columns)
#set($javaField=$column.javaField)
#if($column.htmlType == "selectMultiple" || $column.htmlType == "checkbox")
        if (response.data.$javaField !== null) {
          response.data.$javaField = response.data.$!{javaField}.split(',')
        } else {
          response.data.$javaField = []
        }
#elseif($column.htmlType == "user" || $column.htmlType == "dept")
        response.data.$javaField = { ids: response.data.$javaField, names: response.data.$!{javaField}Name }
#end
#end
        this.form = response.data
#if($hasAttach)
        this.formId = response.data.id
#end
        this.formTitle = '修改${functionName}'
        this.spinning = !this.spinning
      })
    },
    /** 提交按钮 */
#set($refs='$refs')
    submitForm: function (closeDialog) {
      this.closeDialog = closeDialog
      this.disabled = true
      this.${refs}.form.validate(valid => {
        if (valid) {
          const saveForm = JSON.parse(JSON.stringify(this.form))
#foreach($column in $columns)
#set($javaField=$column.javaField)
#if($column.htmlType == "selectMultiple" || $column.htmlType == "checkbox")
          if (saveForm.$javaField) {
            saveForm.$javaField = saveForm.$!{javaField}.join(',')
          } else {
            saveForm.$javaField = null
          }
#elseif($column.htmlType == "user" || $column.htmlType == "dept")
          if (this.form.$javaField !== undefined) {
            saveForm.$javaField = this.form.$!{javaField}.ids
          }
#end
#end
          if (this.form.id !== undefined) {
            update${BusinessName}(saveForm).then(response => {
#if($hasAttach)
              if (this.attachmentRefName) {
                // 表单含有附件
                this.formId = response.data.id // 附件上传formId
                this.${refs}[this.attachmentRefName].upload(this.formId) // 附件上传
              } else {
                this.$message.success('更新成功', 3)
                this.open = false
                this.$emit('ok')
                this.$emit('close')
                this.disabled = false
              }
#else
              this.$message.success('更新成功', 3)
              this.open = false
              this.$emit('ok')
              this.$emit('close')
              this.disabled = false
#end
            })
          } else {
            add${BusinessName}(saveForm).then(response => {
#if($hasAttach)
              if (this.attachmentRefName) {
                // 表单含有附件
                this.formId = response.data.id // 附件上传formId
                this.${refs}[this.attachmentRefName].upload(this.formId) // 附件上传
              } else {
                this.$message.success('新增成功', 3)
                this.$emit('ok')
#if($hasSaveAndAddOption)
                if (this.closeDialog) {
                  this.open = false
                  this.$emit('close')
                  this.disabled = false
                } else {
                  this.reset()
#if($hasSort > 0)
                  /** 获取最大编号 */
                  findMaxSort().then(response => {
                    this.form.sort = response.data
                    this.disabled = false
                  })
#else
                  this.disabled = false
#end
                }
#else
                this.open = false
                this.$emit('ok')
                this.$emit('close')
                this.disabled = false
#end
              }
#else
#if($hasSaveAndAddOption)
              if (this.closeDialog) {
                this.$message.success('新增成功', 3)
                this.open = false
                this.$emit('ok')
                this.$emit('close')
                this.disabled = false
              } else {
                this.$emit('ok')
                this.reset()
#if($hasSort > 0)
                /** 获取最大编号 */
                findMaxSort().then(response => {
                  this.form.sort = response.data
                  this.disabled = false
                })
#else
                this.disabled = false
#end
              }
#else
              this.$message.success('新增成功', 3)
              this.open = false
              this.$emit('ok')
              this.$emit('close')
              this.disabled = false
#end
#end
            })
          }
        } else {
          this.disabled = false
          return false
        }
      })
    },
    back () {
      const index = '/${moduleName}/${businessNameLowerCase}/index'
      this.$router.push(index)
    }#if($hasAttach),
    uploadCompleteFile: function (successFile, errorFile) {
      if (errorFile.length > 0) {
        // 有附件保存失败的处理
        this.attachmentUploadStatus = false // 记录附件上传失败
        this.${refs}[this.attachmentRefName].$el.scrollIntoView() // 页面滚动到附件位置
        this.uploaderButtonStatus = false // 按钮关闭loading状态
      } else {
        // 所有附件都保存成功的处理
        this.attachmentUploadStatus = true // 记录附件上传成功
        this.$message.success('保存成功', 3)
#if($hasSaveAndAddOption)
        if (this.closeDialog) {
          this.open = false
          this.$emit('ok')
          this.$emit('close')
        } else {
          this.$emit('ok')
          this.reset()
#if($hasSort > 0)
          /** 获取最大编号 */
          findMaxSort().then(response => {
            this.form.sort = response.data
            this.disabled = false
          })
#else
        this.disabled = false
#end
        }
#else
		this.open = false
		this.$emit('ok')
        this.$emit('close')
#end
      }
      this.uploaderButtonStatus = false // 返回按钮可用
    }
#else

#end
  }
}
