<template>
  <div>
    <div class="demo-upload-list" v-for="item in uploadList">
      <template v-if="item.status === 'finished'">
        <img :src="item.url">
        <div class="demo-upload-list-cover">
          <Icon type="ios-eye-outline" @click.native="handleView(item.url)"></Icon>
          <Icon type="ios-trash-outline" @click.native="handleRemove(item)"></Icon>
        </div>
      </template>
      <template v-else>
        <Progress v-if="item.showProgress" :percent="item.percentage" hide-info></Progress>
      </template>
    </div>
    <Upload
      ref="upload"
      :show-upload-list="false"
      :default-file-list="defaultList"
      :on-success="handleSuccess"
      :format="['jpg','png']"
      :max-size="2048"
      :on-format-error="handleFormatError"
      :on-exceeded-size="handleMaxSize"
      :before-upload="handleBeforeUpload"
      type="drag"
      :data="{manager_access_token}"
      :action="this.$api.uploadIcon()"
      style="display: inline-block;width:80px;"
    >
      <div style="height:28px;line-height: 28px;text-align:center">{{uploadicontext}}</div>
    </Upload>
    <Modal title="图片预览" v-model="visible" style="text-align:center">
      <img :src="imgUrl" v-if="visible" style="max-width:520px;margin:30px 0px;">
    </Modal>
  </div>
</template>
<script>
import { getStore } from "@/utils/utils";
export default {
  data() {
    return {
      defaultList: [],
      imgUrl: "",
      visible: false,
      uploadList: []
    };
  },
  props: {
    covers: String,
    uploadicontext: String
  },
  computed: {
    manager_access_token() {
      return getStore("userInfo").manager_access_token;
    }
  },
  watch: {
    covers(i) {
      this.defaultList = [];
      this.defaultList.push({
        name: Math.random()
          .toString(36)
          .substr(2),
        status: "finished",
        url: i
      });
      this.$nextTick(() => {
        this.uploadList = this.$refs.upload.fileList;
      });
    }
  },
  methods: {
    handleView(url) {
      this.imgUrl = url;
      this.visible = true;
    },

    handleRemove(file) {
      const fileList = this.$refs.upload.fileList;
      this.$refs.upload.fileList.splice(0, 1);
      let arr = [{ url: "", savename: "" }];
      this.$emit("uploadSuc", arr);
    },
    handleSuccess(data, file) {
      if (data.code == 200) {
        file.url = data.data.thumbnail;
        file.status = "finished";
        let arr = [];
        for (var item of this.uploadList) {
          let savename = item.response.data.saveName
            ? item.response.data.saveName
            : "";
          if (item) {
            arr.push({ url: item.url, savename: savename });
          } else {
            arr.push("");
          }
        }
        this.$emit("uploadSuc", arr);
      }
    },
    handleFormatError(file) {
      this.$Message.error("上传的图片只允许jpg、png");
    },
    handleMaxSize(file) {
      this.$Message.error("上传的图片太大了，不能超过2M");
    },
    handleBeforeUpload() {
      const check = this.uploadList.length < 1;
      if (!check) {
        this.$Message.info("只能上传一张图片");
      }
      return check;
    }
  },
  mounted() {
    this.uploadList = this.$refs.upload.fileList;
  }
};
</script>
<style>
.demo-upload-list {
  display: inline-block;
  width: 60px;
  height: 60px;
  text-align: center;
  line-height: 60px;
  border: 1px solid transparent;
  border-radius: 4px;
  overflow: hidden;
  background: #fff;
  position: relative;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
  margin-right: 4px;
}
.demo-upload-list img {
  width: 100%;
  height: 100%;
}
.demo-upload-list-cover {
  display: none;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.6);
}
.demo-upload-list:hover .demo-upload-list-cover {
  display: block;
}
.demo-upload-list-cover i {
  color: #fff;
  font-size: 20px;
  cursor: pointer;
  margin: 0 2px;
}
</style>
