<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2018-12-06 13:44:22
 * @LastEditors: 无尘
 * @LastEditTime: 2020-06-12 15:35:03
 -->
<!--
  单个上传组件
   <single-upload
    :imgSrc.sync=""
    :field="''">
  </single-upload>
 -->
<template>
  <div class="single-upload-wrap">
    <div class="imageList">
      <el-upload class="avatar-uploader" :action="uploadUrl()" multiple list-type="picture-card" :file-list="imgList" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
        <!-- <img v-if="imgSrc" :src="imgSrc" class="avatar" @mouseover="showImage(imgSrc)" /> -->
        <!-- <i class="el-icon-plus"></i> -->
        <video v-if="videoSrc != '' && videoFlag == false" :src="videoSrc" class="avatar">您的浏览器不支持视频播放</video>
        <div v-if="!videoSrc" class="flex-column">
          <i class="el-icon-plus vatar-uploader-icon"></i>
          <p class="font-12 color-606266" style="line-height: 22px;">{{ uploadType == 1 || uploadType == 2 || uploadType == 4 ? '上传图片' : '上传视频' }}</p>
        </div>
      </el-upload>
      <div v-for="item in imgList" :key="item" class="imageBox">
        <img :src="item.url" class="image" />
        <div class="imageMask">
          <span @click="handleRemove(item.url)" class="el-upload-list__item-delete">
            <i class="el-icon-error"></i>
          </span>
          <span @click="handlePreview(item.url)" class="el-upload-list__item-preview is-preview">
            <i class="el-icon-zoom-in"></i>
          </span>
        </div>
      </div>
    </div>
    <p v-if="uploadType == 1" class="upload-tip color-909399 font-12">
      图片支持png、jpg、jpeg格式，大小不超过2M，可上传1张
    </p>
    <p v-if="uploadType == 2" class="upload-tip color-909399 font-12">
      建议尺寸：160×160；支持jpg、jpeg、png格式，图片大小不超过1MB
    </p>
    <p v-if="uploadType == 3" class="upload-tip color-909399 font-12">
      视频支持MP4格式，大小不超过10M，上传企业微信后3天内有效
    </p>
    <p v-if="uploadType == 4" class="upload-tip color-909399 font-12">
      建议尺寸：520×416，支持png、jpg、jpeg格式，大小不超过1M
    </p>
    <!-- 图片预览 -->
    <vue-gic-img-preview :imgUrl="imgUrl" :imgShowFlag="imgShowFlag" @hideImage="hideImage"></vue-gic-img-preview>
  </div>
</template>
<script>
// import strLength from '@/common/js/strlen';
// import showMsg from '@/common/js/showmsg';
import errMsg from '@/common/js/error';
// import { _debounce } from '@/common/js/public';
// import { getRequest } from '@/api/api';
export default {
  name: 'single-upload',
  props: {
    videoSrc: {
      // 传入 input value
      type: String,
      default: ''
    },
    imgSrc: {
      // 传入 input value
      type: String,
      default: ''
    },
    field: {
      type: String,
      default: ''
    },
    uploadLimit: {
      type: [Number, String],
      default: 2
    },
    uploadType: {
      type: [Number, String],
      default: 1 // 1.图片 2.网页 3.视频 4小程序
    }
  },
  data() {
    return {
      projectName: '', // 当前项目名
      imgShowFlag: false, // 是否弹框显示,true: 显示；false: 不显示
      imgUrl: '', // 传递的图片 src
      videoFlag: false,
      imgList: [],
      fileType: this.uploadType == 1 || this.uploadType == 2 ? 'jpg' : 'mp4'
    };
  },
  beforeMount() {
    const that = this;
    let host = window.location.origin;
    if (host.indexOf('localhost') != -1) {
      that.baseUrl = 'http://www.gicdev.com';
    } else {
      that.baseUrl = host;
    }
    // that.upUrl = that.baseUrl + '/api-plug/upload-img?wxFlag=1'
  },
  methods: {
    /**
     * 上传图片/视频
     */
    uploadUrl() {
      const that = this;
      let host = window.location.origin;
      let baseUrl;
      if (host.indexOf('localhost') != '-1') {
        baseUrl = 'https://www.gicdev.com';
      } else {
        baseUrl = host;
      }
      that.upUrl = baseUrl + `/haoban-manage3-web/upload-file?fileType=${that.fileType}`;
      return that.upUrl;
    },

    /**
     * 上传图片
     */
    handleAvatarSuccess(res, file, type) {
      const that = this;
      if (res.errorCode == 1) {
        that.videoFlag = false;
        if (that.uploadType == 3) {
          that.$emit('update:videoSrc', res.result.url);
        } else {
          this.imgList.push({ uid: res.result.key, url: res.result.url });
          that.$emit('update:imgSrc', this.imgList);
        }
      } else {
        errMsg.errorMsg(res);
      }
    },
    /* eslint-disable */
    beforeAvatarUpload(file) {
      const that = this;
      // console.log(file);
      that.fileType = file.type === 'image/jpeg' ? 'jpeg' : file.type === 'image/jpg' ? 'jpg' : file.type === 'image/png' ? 'png' : file.type == 'video/mp4' ? 'mp4' : '';
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/jpg' || file.type === 'image/png';
      const isMP4 = file.type == 'video/mp4';
      const isLt2M = file.size / 1024 / 1024 < that.uploadLimit;
      if ((that.uploadType == 1 || that.uploadType == 2 || that.uploadType == 4) && !isJPG) {
        that.$message.error('上传图片支持png、jpg、jpeg格式!');
      }
      if ((that.uploadType == 1 || that.uploadType == 2 || that.uploadType == 4) && !isLt2M) {
        that.$message.error(`上传图片大小不能超过 ${that.uploadLimit}MB!`);
      }
      if (that.uploadType == 3 && !isMP4) {
        that.$message.error('上传视频支持MP4格式!');
      }
      if (that.uploadType == 3 && !isLt2M) {
        that.$message.error(`上传视频大小不能超过 ${that.uploadLimit}MB!`);
      }
      if (that.uploadType == 3) {
        return isMP4 && isLt2M;
      }
      return (
        isJPG &&
        isLt2M &&
        new Promise(function (resolve, reject) {
          let reader = new FileReader();
          reader.onload = function (event) {
            let image = new Image();
            image.onload = function () {
              /* eslint-disable */
              let width = this.width;
              let height = this.height;
              // if (that.uploadType == 2 && width !== 160 && height !== 160){
              //   that.$message.error('图片尺寸必须为160px*160px!');
              //   reject();
              // }
              resolve();
            };
            image.src = event.target.result;
            // that.$emit('update:imgSrc',event.target.result)
          };
          reader.readAsDataURL(file);
        })
      );
    },

    /**
     * 上传图片预览
     */
    handlePreview(url) {
      this.imgShowFlag = true;
      this.imgUrl = url;
    },
    handleRemove(item){
      console.log(item)
      this.imgList = this.imgList.filter(i=>i.url!=item);
    },
    hideImage(val) {
      this.imgShowFlag = val;
      this.imgUrl = '';
    }
  }
  // mounted() {}
};
</script>
<style lang="scss" scoped>
.w-500 {
  width: 500px;
}
.avatar-uploader {
  >>> .el-upload {
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    -webkit-align-items: center;
    -ms-flex-align: center;
    align-items: center;
    -webkit-justify-content: center;
    justify-content: center;
    width: 100px;
    height: 100px;
    border: 1px dashed #c4c6cf;
    border-radius: 0px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
}

/* flex */
.flex {
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
}

.flex-1 {
  -webkit-box-flex: 1;
  -webkit-flex: 1;
  -ms-flex: 1;
  flex: 1;
}

.flex-column {
  -webkit-flex-direction: column;
  -moz-flex-direction: column;
  -ms-flex-direction: column;
  -o-flex-direction: column;
  flex-direction: column;
}

.flex-row {
  -webkit-flex-direction: row;
  -moz-flex-direction: row;
  -ms-flex-direction: row;
  -o-flex-direction: row;
  flex-direction: row;
}

.flex-align-center {
  -webkit-box-align: center;
  -webkit-align-items: center;
  -ms-flex-align: center;
  align-items: center;
}

.flex-pack-center {
  -webkit-box-pack: center;
  -webkit-justify-content: center;
  -ms-flex-pack: center;
  justify-content: center;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  // height: 100px;
  // line-height: 100px;
  text-align: center;
}
.avatar {
  max-width: 100px;
  max-height: 100px;
  display: block;
}

.upload-tip {
  margin-top: 8px;
  font-size: 12px;
  line-height: 20px;
}

.color-1890ff {
  color: #2f54eb;
}
.flex-column{
  display: flex;
  flex-direction: column;
}
.imageList{
  display: flex;
  flex-wrap: wrap;
}
.imageBox{
  position: relative;
  max-width: 102px;
  height: 102px;
  margin: 0 0 5px 5px;
  img{
    width: 100%;
    height: 100%;
    display: block;
  
  }
  .imageMask{
      opacity:0;
      position: absolute;
      width: 100%;
      height: 100%;
      left: 0;
      top: 0;
      cursor: default;
      &:hover{
           opacity:1;
      }
      span {
        display: inline-block;
        cursor: pointer;
      }
      .el-upload-list__item-delete {
        z-index: 1;
        line-height: 1;
        top: 0px;
        right: 0px;
        transform: translate(40%, -40%);
        position: absolute;
        font-size: inherit;
        color: inherit;
        .el-icon-error {
          color: rgba(0, 0, 0, 0.4);
          font-size: 16px;
          &:hover {
            color: rgba(0, 0, 0, 0.8);
          }
        }
      }

      .el-upload-list__item-preview {
        z-index: 2;
        position: absolute;
        line-height: 1px;
        border-radius: 100%;
        right:-3px;
        bottom:-3px;
        height: 25px;
        width: 25px;
        background-color: rgba(0, 0, 0, 0.4);
        .el-icon-zoom-in {
          font-size: 14px;
          margin-top: 50%;
          margin-left: 50%;
          transform: translate(-50%, -50%);
          color: #fff;
        }
        &:hover {
          background-color: rgba(0, 0, 0, 0.8);
          transform: scale(1.3);
          right: -2px;
          bottom: -2px;
        }
      }
  }
}
</style>
