<template>
  <a-modal
    :title="modalTitle"
    :width="1400"
    :visible="visible"
    class="ant-modal2"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
  >
    <a-form :form="form">

      <a-form-item v-show="false" >
        <a-input v-decorator="['id']" type="hidden"/>
        <a-input v-decorator="['planid']" type="hidden"/>
      </a-form-item>
      <a-form-item
        label="标题"
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
      >
        <a-input
          v-decorator="['plantitle', {rules: [{required: true, message: '标题不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="摘要"
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
      >
        <a-input
          v-decorator="['digest', {rules: [{required: true, message: '摘要不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="简介"
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
      >
        <a-input
          v-decorator="['shortdesc']" />
      </a-form-item>
      <a-form-item
        label="关键字"
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
      >
        <a-input
          v-decorator="['keywords']" />
      </a-form-item>
      <a-form-item
        label="排序"
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
      >
        <a-input
          type="number"
          v-decorator="['orderid', {rules: [{required: true, message: '排序不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="seoid"
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
      >
        <a-input
          type="number"
          v-decorator="['seoid', {rules: [{required: true, message: 'seoid不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="内容"
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
      >
        <wang-editor
          :content="plancontext"
          @catchData="catchData"
        ></wang-editor>
      </a-form-item>
    </a-form>
    <template slot="footer">
      <a-button :loading="confirmLoading" type="primary" @click="save()">保存</a-button>
    </template>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { addPlanDetail, updatePlanDetail } from '@/api/qykh/plandetails'
import WangEditor from '@/components/Editor/WangEditor'
import UploadImg from '@/components/Upload/UploadImg'
import UploadArticleFile from '@/components/Upload/UploadArticleFile'
export default {
  name: 'BaseForm',
  components: {
    WangEditor,
    UploadImg,
    UploadArticleFile
  },
  data () {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 4 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 20 }
      },
      topFlag: 0,
      status: 1,
      confirmLoading: false,
      mdl: {},
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      plancontext: '',
      picture: '',
      resultContent: '',
      type: null
    }
  },
  props: {
  },
  created () {
  },
  methods: {
    base (record) {
      this.visible = true
      // 如果是空标识添加
      if (this.BaseTool.Object.isBlank(record)) {
        this.modalTitle = '添加'
        this.plancontext = ''
        return
      }
      this.modalTitle = '编辑'
      this.type = record.type
      const { form: { setFieldsValue } } = this
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'plantitle',
          'shortdesc',
          'planid',
          'orderid',
          'planid',
          'seoid',
          'time',
          'digest',
          'keywords'
        ])))
        this.plancontext = record.plancontext
      })
    },
    catchData (content) {
      this.resultContent = content
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        values = {
          ...values,
          plancontext: this.resultContent ? this.resultContent : this.plancontext
        }
        /* if (this.type === 2) {
          if (values.parentId == null) {
            this.$message.error('请选择父文章')
            return
          }
        } */
        if (this.BaseTool.String.isBlank(values.id)) {
          addPlanDetail(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        } else {
          updatePlanDetail(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        }
      })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.plancontext = ''
      this.resultContent = ''
      this.topFlag = ''
      this.status = 1
      this.picture = ''
      this.form.resetFields()
      if (this.BaseTool.Object.isNotBlank(values)) {
        this.$emit('ok', values)
      }
    }
  }
}
</script>
