<template>
  <div>
    <div class="flex flex-wrap">
      <a-upload
        :action="UploadUrl()"
        list-type="picture-card"
        :file-list="fileList"
        @preview="handlePreview"
        :before-upload="beforeUpload"
        @change="handleChange"
        :headers="headers"
      >
        <div v-if="fileList.length < fileListLength">
          <a-icon type="plus" />
        </div>
      </a-upload>
      <div class="remarkTxt" v-if="remarkTxt">
        {{ remarkTxt }}
      </div>
    </div>
    <a-modal :visible="previewVisible" :footer="null" @cancel="handleCancel">
      <img alt="example" style="width: 100%" :src="previewImage" />
    </a-modal>
  </div>
</template>
<script>
// import homeApi from '@/api/homeApi'
import { setImgUrl } from '@/utils/img'

function getBase64 (file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader()
    reader.readAsDataURL(file)
    reader.onload = () => resolve(reader.result)
    reader.onerror = error => reject(error)
  })
}
export default {
  props: {
    remarkTxt: {
      type: String,
      default: ''
    },
    fileListLength: {
      type: Number,
      default: 1
    }
  },
  data () {
    return {
      loading: false,
      previewImage: '',
      previewVisible: false,
      fileList: [],
      headers: {
        Authorization: this.$store.getters.token
      }
    }
  },
  methods: {
    initData (imglist) {
      if (imglist && imglist.length) {
        this.fileList = imglist.map((item, index) => {
          return { url: setImgUrl(item) }
        })
      }
    },
    UploadUrl () {
      return (
         process.env.VUE_APP_API_BASE_URL + '/zjxl/banner/file'
      )
    },
    handleChange ({ file, fileList }) {
      // this.handleAvatarSuccess (file)
      this.fileList = fileList
      // this.$emit('uploadSuccess', { fileList: this.fileList })
    },
    async handlePreview (file) {
      if (!file.url && !file.preview) {
        file.preview = await getBase64(file.originFileObj)
      }
      this.previewImage = file.url || file.preview
      this.previewVisible = true
    },
    handleAvatarSuccess (res) {
      if (res.status === 200) {
        var imageUrl = process.env.imgServer + res.data
        this.currentImage = imageUrl
        this.$emit('imageUrlGet', res.data)
      } else {
        this.$message.error(res.msg)
      }
    },
    beforeUpload (file) {
      var $uptypes = ['image/jpg', 'image/png', 'image/jpeg']
      const isJPG = $uptypes.indexOf(file.type) > -1
      const isLt1M = file.size / 1024 / 1024 < 5

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 jpg、jpeg、png 格式!')
      }
      if (!isLt1M) {
        this.$message.error('上传头像图片大小不能超过 5MB!')
      }
      return isJPG && isLt1M
    },
    handleCancel () {
      this.previewVisible = false
    }
  }
}
</script>
<style lang="less" scoped>
.ant-upload-select-picture-card i {
  font-size: 32px;
  color: #999;
}

.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: #666;
}
.ant-upload-picture-card-wrapper{
  width:auto;
}
.remarkTxt{
  margin-top: 70px;
}
</style>
