##判断数据字典
#set( $dictCount = 0)
#foreach ($column in $columns)
#if(${column.dictType} != '')
#set( $dictCount = $dictCount + 1)
#end
#end
#set( $listSize = $!{dictColumns.size()})
##判断是否包含排序号字段
#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
import AntModal from '@/components/pt/dialog/AntModal'
import { get${BusinessName}, add${BusinessName}, update${BusinessName}, findMaxSort#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 $columns)
#if(${column.dictType} != '')
    ${column.javaField}Options: {
      type: Array,
      required: true
    },
#end
#end
    ${businessName}Options: {
      type: Array,
      required: true
    }
  },
  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,
      total: 0,
      disabled: false,
      id: undefined,
      formTitle: '添加${functionName}',
      currentRow: undefined,
      oldParentId: '',
      // 表单参数
      form: {
        id: undefined,
        parentId: undefined
      },
#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
#if($hasAttach)
      if (this.formId) {
        // 清空附件的formId
        this.formId = ''
      }
#end
    },
    /** 新增按钮操作 */
    handleAdd (row) {
      this.reset()
      this.$emit('select-tree')
      this.oldParentId = ''
      if (row != null && row.id) {
        this.currentRow = row
        this.oldParentId = row.id
        this.form.parentId = row.id
      } else {
        this.form.parentId = 0
      }
      /** 获取最大编号 */
      findMaxSort(this.form.parentId).then(response => {
        this.form.treeSort = response.data
        this.open = true
        this.formTitle = '添加${functionName}'
      })
#if(${isSub} == 'true')
    this.form.${subTableFkNameUnCap} = this.${subTableFkNameUnCap}
#end
    },
#if($hasCopyRecordOption)
    /** 修改按钮操作 */
    handleCopy (row) {
      this.formTitle = '复制${functionName}'
      this.reset()
      this.$emit('select-tree', row)
      this.open = true
      this.spinning = !this.spinning
      this.currentRow = row
      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
        this.form.id = undefined
        /** 获取最大编号 */
        findMaxSort(this.form.parentId).then(response => {
          this.form.treeSort = response.data
        })
        this.oldParentId = response.data.parentId
        this.spinning = !this.spinning
      })
    },
#end
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset()
      this.$emit('select-tree', row)
      this.open = true
      this.spinning = !this.spinning
      this.currentRow = row
      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.oldParentId = response.data.parentId
        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))
          this.spinning = !this.spinning
#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)
                if (this.oldParentId !== this.form.parentId) {
                  // 如果修改父节点则刷新树
                  this.$emit('ok')
                } else {
                  this.setNodeData(response.data)
                }
                this.disabled = false
                this.cancel()
              }
#else
              this.$message.success('更新成功', 3)
              if (this.oldParentId !== this.form.parentId) {
                // 如果修改父节点则刷新树
                this.$emit('ok')
              } else {
                this.setNodeData(response.data)
              }
              this.disabled = false
              this.cancel()
#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)
                if (this.oldParentId !== this.form.parentId) {
                  // 如果修改父节点则刷新树
                  this.$emit('ok')
                } else {
                  this.appendTreeNode(this.currentRow, response.data)
                }
                this.disabled = false
                if (this.closeDialog) {
                  this.cancel()
                } else {
                  this.reset()
                  this.$emit('select-tree')
                  /** 获取最大编号 */
                  findMaxSort(this.form.parentId).then(response => {
                    this.form.treeSort = response.data
                  })
                }
              }
#else
              this.$message.success('新增成功', 3)
              if (this.oldParentId !== this.form.parentId) {
                // 如果修改父节点则刷新树
                this.$emit('ok')
              } else {
                this.appendTreeNode(this.currentRow, response.data)
              }
              this.disabled = false
              if (this.closeDialog) {
                this.cancel()
              } else {
                this.reset()
                this.$emit('select-tree')
                /** 获取最大编号 */
                findMaxSort(this.form.parentId).then(response => {
                  this.form.treeSort = response.data
                })
              }
#end
            })
          }
        } else {
          this.disabled = false
          return false
        }
      })
    },
    back () {
      const index = '/${moduleName}/${businessNameLowerCase}/index'
      this.$router.push(index)
    },
    setNodeData (data) {
#foreach($column in $editColumns)
#set($javaField=$column.javaField)
      this.currentRow.$javaField = data.$javaField
#end
    }#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)
        this.open = false
        this.$emit('ok')
        this.$emit('close')
      }
      this.uploaderButtonStatus = false // 返回按钮可用
    }
#end

  }
}