<template>
  <div>

    <file-pond
      ref="pond"
      name="file"
      label-idle="选择图片或拖动图片到此处"
      label-file-processing="图片正在上传，请稍后"
      label-file-processing-aborted="图片上传被取消"
      label-tap-to-retry="尝试重试"
      label-file-processing-complete="图片上传成功！"
      accepted-file-types="image/png, image/jpeg, image/jpg, image/gif"
      :imagePreviewHeight="32"
      :allow-multiple="true"
      :max-files="9"
      :server="server"
      :instant-upload="true"
      @init="handleFilePondInit"
      @processfile="success"
    />
  </div>
</template>

<script>
import vueFilePond from 'vue-filepond'
import 'filepond/dist/filepond.min.css'
import FilePondPluginImagePreview from 'filepond-plugin-image-preview'
import 'filepond-plugin-image-preview/dist/filepond-plugin-image-preview.min.css'
import FilePondPluginFileValidateType from 'filepond-plugin-file-validate-type'
import FilePondPluginFileValidateSize from 'filepond-plugin-file-validate-size'

// Create component
const FilePond = vueFilePond(
  FilePondPluginFileValidateType,
  FilePondPluginImagePreview,
  FilePondPluginFileValidateSize
)

var resp = ''

export default {
  name: 'FilePondImage',
  components: {
    FilePond
  },
  resp,
  data() {
    return {
      server: {
        url: '//file.reghao.cn/api/file/upload/image',
        process: {
          headers: {
            'Authorization': this.$store.getters.token
          },
          onload(response) {
            resp = JSON.parse(response)
          }
        }
      }
    }
  },
  created() {
  },
  methods: {
    handleFilePondInit() {
      // FilePond instance methods are available on `this.$refs.pond`
    },
    success() {
      this.$emit('resp', resp)
    }
  }
}
</script>
