<template>
  <a-modal
    :title="modalTitle"
    :width="1200"
    :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-select v-decorator="['type', {rules: [{required: true, message: '类别不能为空'}]}]" placeholder="请选择">
          <a-select-option
            v-for="(value,key) in typeMap"
            :key="key"
            :label="key"
            :value="key">{{ value }}</a-select-option>
        </a-select>
      </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="['introduction']" />
      </a-form-item>
      <a-form-item
        label="链接"
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
      >
        <a-input
          v-decorator="['url']" />
      </a-form-item>
      <a-form-item
        label="文件"
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
      >
        <!--        <upload-img
          ref="imageUpload"
          @catchImage="catchImage"
        ></upload-img>-->
        <upload-article-file
          ref="imageUpload"
          @catchImage="catchImage"
        ></upload-article-file>
      </a-form-item>
      <a-form-item
        label="是否置顶"
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
      >
        <a-radio-group
          :defaultValue="0"
          v-model="topFlag">
          <a-radio :value="1">是</a-radio>
          <a-radio :value="0">否</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item
        label="是否启用"
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
      >
        <a-radio-group
          :defaultValue="1"
          v-model="status">
          <a-radio :value="1">是</a-radio>
          <a-radio :value="0">否</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item
        label="排序"
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
      >
        <a-input
          type="number"
          v-decorator="['sort', {rules: [{required: true, message: '排序不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="内容"
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
      >
        <wang-editor
          :content="content"
          @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 { addArticle, updateArticle } from '@/api/operate/article'
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,
      content: '',
      picture: '',
      resultContent: ''
    }
  },
  props: {
    typeMap: {
      type: Object,
      default: () => ({})
    }
  },
  methods: {
    base (record) {
      this.visible = true
      // 如果是空标识添加
      if (this.BaseTool.Object.isBlank(record)) {
        this.modalTitle = '添加'
        this.content = ''
        this.$refs.imageUpload.base(1, [])
        return
      }
      this.modalTitle = '编辑'
      const { form: { setFieldsValue } } = this
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'type',
          'title',
          'sort',
          'url',
          'introduction'
        ]),
        { 'type': record.type + '' }))
        this.content = record.content
        this.topFlag = record.topFlag
        this.status = record.status
        this.picture = record.picture
        const fileList = []
        if (record.picture) {
          fileList.push({
            uid: '-1',
            name: record.title,
            status: 'done',
            url: this.BaseTool.Constant.FILE_URL + record.picture,
            originUrl: record.picture
          })
        }
        this.$refs.imageUpload.base(1, fileList)
      })
    },
    catchData (content) {
      this.resultContent = content
    },
    catchImage (fileList) {
      if (fileList.length !== 0) {
        this.picture = fileList[0].url
      }
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        values = {
          ...values,
          picture: this.picture,
          topFlag: this.topFlag,
          status: this.status,
          content: this.resultContent ? this.resultContent : this.content
        }
        if (this.BaseTool.String.isBlank(values.id)) {
          addArticle(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        } else {
          updateArticle(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)
      }
    }
  }
}
</script>
