<template>
  <a-modal
    title="操作"
    style="top: 20px;"
    :width="800"
    v-model="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
  >
    <a-form :form="form">
      <a-form-item style="display:none">
        <a-input v-decorator="['${pkColumn.javaField}']"/>
      </a-form-item>
      <a-form-item style="display:none">
        <a-input v-decorator="['version']"/>
      </a-form-item>
#foreach($column in $columns)
#if(!$column.pk)
#set($parentheseIndex=$column.columnComment.indexOf("（"))
#if($parentheseIndex != -1)
#set($comment=$column.columnComment.substring(0, $parentheseIndex))
#else
#set($comment=$column.columnComment)
#end
#set($field=$column.javaField)
#set($dictType=$column.dictType)
#if($column.htmlType == "input")
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="${comment}">
        <a-input placeholder="${comment}" v-decorator="['${field}'#if($column.required), {rules: [{required: true, message: '请输入${comment}'}]}#end]"/>
      </a-form-item>
#end
#end
#end
    </a-form>
  </a-modal>
</template>
<script>
    import {save${ClassName}} from '@/api/admin/meeting/${ClassName}'
    import pick from 'lodash.pick'

    export default {
  name: '${ClassName}Modal',
  props: {
  },
  components: {
  },
  data () {
    return {
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      mdl: {},
      form: this.$form.createForm(this)
    }
  },
  beforeCreate () {
  },
  created () {
  },
  methods: {
    add () {
      this.form.resetFields()
      this.edit({ ${pkColumn.javaField}: 0 })
    },
    edit (record) {
      this.mdl = Object.assign(record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.mdl, #foreach($column in $columns)#if($foreach.hasNext)'${column.javaField}', #else'${column.javaField}'#end#end))
      })
    },
#set($foo="$")
    handleSubmit (e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          console.log('Received values of form: ', values)
          this.confirmLoading = true
          save${ClassName}(values).then(res => {
            if (res.code === 0) {
              this.${foo}message.success('保存成功')
              this.$emit('ok')
              this.visible = false
            } else {
              this.${foo}message.error(res.msg)
            }
          }).catch(() => {
            this.$message.error('系统错误，请稍后再试')
          }).finally(() => {
            this.confirmLoading = false
          })
        }
      })
    }
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  }
}
</script>
