<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-form-item>
      <a-form-item
        label="标题"
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
      >
        <a-input
          v-decorator="['title', {rules: [{required: true, message: '标题不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="摘要"
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
      >
        <a-input
          v-decorator="['digest']" />
      </a-form-item>
      <a-form-item
        label="关键字"
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
      >
        <a-input
          v-decorator="['keywords', {rules: [{required: true, message: '排序不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="排序"
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
      >
        <a-input
          type="number"
          v-decorator="['ord', {rules: [{required: true, message: '排序不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="内容"
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
      >
        <wang-editor
          :content="descr"
          @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 { addInformationHelp, updateInformationHelp } from '@/api/qykh/informationhelp'
import WangEditor from '@/components/Editor/WangEditor'
import UploadImg from '@/components/Upload/UploadImg'
import UploadArticleFile from '@/components/Upload/UploadArticleFile'
export default {
  name: 'BaseArticle',
  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,
      descr: '',
      resultContent: '',
      type: null
    }
  },
  props: {
  },
  created () {
  },
  methods: {
    base (record) {
      this.visible = true
      // 如果是空标识添加
      if (this.BaseTool.Object.isBlank(record)) {
        this.modalTitle = '添加'
        this.descr = ''
        return
      }
      this.modalTitle = '编辑'
      this.type = record.type
      const { form: { setFieldsValue } } = this
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'title',
          'pid',
          'shortdesc',
          'ord',
          'digest',
          'time',
          'keywords'
        ])))
        this.descr = record.descr
      })
    },
    catchData (content) {
      this.resultContent = content
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        values = {
          ...values,
          descr: this.resultContent ? this.resultContent : this.descr
        }
        /* if (this.type === 2) {
          if (values.parentId == null) {
            this.$message.error('请选择父文章')
            return
          }
        } */
        if (this.BaseTool.String.isBlank(values.id)) {
          addInformationHelp(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        } else {
          updateInformationHelp(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        }
      })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.descr = ''
      this.resultContent = ''
      this.form.resetFields()
      if (this.BaseTool.Object.isNotBlank(values)) {
        this.$emit('ok', values)
      }
    }
  }
}
</script>
