<template>
  <a-card :bordered="false" v-show="visible" class="card" :title="modalTitle">
    <a-row :gutter="48" slot="extra">
      <a-col :md="48" :sm="48">
        <span class="table-page-search-submitButtons" style="float: right">
          <a-button :loading="confirmLoading" type="primary" @click="save()">保存</a-button>
          <a-button style="margin-left: 8px" type="default" @click="handleCancel()">返回</a-button>
        </span>
      </a-col>
    </a-row>
    <a-form :form="form">

      <a-form-item v-show="false">
        <a-input v-decorator="['id']" type="hidden"/>
      </a-form-item>

      <row-list :col="2">
        <row-item>

          <a-form-item
            label="表单代号"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              v-decorator="['code', {rules: [{required: true, message: '表单代号不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="表单名称"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              v-decorator="['name', {rules: [{required: true, message: '表单名称不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="是否复杂表单"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select v-decorator="['type', { rules: [{required: true, message: '是否复杂表单不能为空'}]}]" placeholder="请选择">
              <a-select-option
                v-for="(label,value) in typeMap"
                :key="value"
                :label="label"
                :value="parseInt(value)">{{ label }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="表单主题"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              v-decorator="['theme', {rules: [{required: true, message: '表单主题不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <!--        <row-item>

         <a-form-item
           label="表单设计数据"
           :labelCol="BaseTool.Constant.labelCol"
           :wrapperCol="BaseTool.Constant.wrapperCol"
         >
         </a-form-item>
       </row-item>
       <row-item>

         <a-form-item
           label="表单js代码"
           :labelCol="BaseTool.Constant.labelCol"
           :wrapperCol="BaseTool.Constant.wrapperCol"
         >
         </a-form-item>
       </row-item>-->
      </row-list>
    </a-form>
  </a-card>
</template>

<script>
import pick from 'lodash.pick'
import { addDesignForm, updateDesignForm } from '@/api/design/designForm'
export default {
  name: 'BaseDesignForm',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      typeMap: {},
      form: this.$form.createForm(this),
      visible: false
      // 下拉框map
    }
  },
  props: {},
  created () {
    // 下拉框map
    this.typeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.YES_NO)
  },
  methods: {
    base (record) {
      this.visible = true
      // 如果是空标识添加
      if (this.BaseTool.Object.isBlank(record)) {
        this.modalTitle = '添加'
        return
      }
      this.modalTitle = '编辑'
      const { form: { setFieldsValue } } = this
      // 日期处理
      record.createTime = this.BaseTool.Moment(record.createTime, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'code',
          'name',
          'type',
          'theme',
          'designData',
          'jsCode',
          'createTime'
        ])))
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        // 日期处理
        values.createTime = this.BaseTool.Date.formatter(values.createTime, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
        if (this.BaseTool.String.isBlank(values.id)) {
          addDesignForm(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        } else {
          updateDesignForm(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        }
      })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.form.resetFields()
      if (this.BaseTool.Object.isNotBlank(values)) {
        this.$emit('ok', values)
      } else {
        this.$emit('ok')
      }
    }
  }
}
</script>
