<template>
  <div>
    <div class="flex flex-wrap">
      <a-upload
        class="flex f_wrap"
        :action="UploadUrl()"
        list-type="picture-card"
        :file-list="fileList"
        @preview="handlePreview"
        :before-upload="beforeUpload"
        :customRequest="fileUpload"
        :remove="handleFileRemove"
      >
        <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" title="预览">
      <img alt="example" style="width: 100%" :src="previewImage" />
    </a-modal>
  </div>
</template>
<script>
import homeApi from '@/api/homeApi'
import { setImgUrl } from '@/utils/img'
import { setTimeout } from 'timers'

const _ = require('lodash')

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 {
            uid: '-' + (index + 1),
            name: 'image.png',
            status: 'done',
            url: setImgUrl(item)
          }
        })
      } else {
        this.fileList = []
      }
    },
    UploadUrl () {
      return (
         process.env.VUE_APP_API_BASE_URL + '/zjxl/banner/file'
      )
    },
    fileUpload (file) {
      const formData = new FormData()
      formData.append('file', file.file)
      homeApi.fileUpload(formData).then(res => {
        if (res.status === 10000) {
          var length = this.fileList.length
          const citem = {
            uid: '-' + (length + 1),
            name: res.result.fileName,
            status: 'done',
            uri: res.result.fullUri,
            url: res.result.fullUrl
          }
          this.fileList.push(citem)
          this.$emit('uploadSuccess', { fileList: this.fileList })
        } else {
          this.$message.error(res.message)
        }
      })
    },
    handleChange ({ file, fileList }) {
      setTimeout(() => {
        this.fileList = []
        fileList.forEach((item) => {
          if (item.response.status === 10000) {
            const citem = item.response.result
            citem.url = citem.fullUrl
            this.fileList.push(citem)
          } else {
            this.$message.error(item.message)
          }
        })
        this.$emit('uploadSuccess', { fileList: this.fileList })
      }, 3000)
    },
    async handlePreview (file) {
      this.previewImage = file.url || file.preview
      this.previewVisible = true
    },
    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
    },
    handleFileRemove (file) {
      const hasIndex = _.indexOf(this.fileList, file)
      if (hasIndex > -1) {
        this.fileList.splice(hasIndex, 1)
      }
    }
  }
}
</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>
