<template ref="uploadImage">
  <div style="display: flex;flex-wrap: wrap">
    <viewer style="display: flex;flex-wrap: wrap" :images="baseList">
      <div :key="index" v-for="(base,index) in baseList" class='finish_room2'>
        <div class='room_img'>
          <img v-lazy="base">
          <div @click="deleteImg(index)" class="im-button">
            <div class="im-close"></div>
            <div class="im-close1"></div>
          </div>
        </div>
      </div>
    </viewer>
    <div v-loading="uploadLoading" class='finish_room2'>
      <div class='room_add_img'>
        <span><img src="../assets/img/add_img.png"></span>
        <span>点击或文件拖拽</span>
        <input multiple="multiple" @change='add_img' type="file">
      </div>
    </div>
  </div>

</template>

<script>
  import userApi from '@/api/user/user'

  export default {
    props: [
      'baseList'
    ],
    data: function () {
      return {
        photo: [],
        uploadLoading: false
      }
    },
    props: {
      baseList: {
        type: Array,
        default() {
          return [];
        }
      }
    },
    mounted() {
      if (this.img) {
        this.photo.push(this.img)
      }
      this.limit = this.limit ? parseInt(this.limit) : 1
    },
    methods: {
      deleteImg(index) {
        this.$emit('removeImage', index)
      },
      add_img(event) {
        let isCheck = true
        let files = event.target.files
        for (let i = 0; i < files.length; i++) {
          if (/\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(files[i].name)) {
          } else {
            this.$notify.error({
              title: '上传图片错误',
              message: '请上传gif|jpg|jpeg|png|GIF|JPG|PNG格式图片'
            })
            isCheck = false
            break;
          }
        }
        if (isCheck) {
          this.uploadLoading = true
          let baseList = JSON.parse(JSON.stringify(this.baseList))
          for (let i = 0; i < files.length; i++) {
            let reader = new FileReader()
            let self = this
            reader.readAsDataURL(files[i])
            this.initImg(reader, event, files[i], i + 1, baseList.length+files.length)
          }
        }
      },
      initImg(reader, event, fileItem, index, length) {
        let formdata = new window.FormData()
        formdata.append('file', fileItem, "image.png")
        formdata.append('action', 'test')
        this.formatImg(reader, formdata,length)
      },
      formatImg(reader, formdata,length) {
        let that = this
        reader.onload = function (e) {
          let img = new Image;
          img.src = this.result;
          img.onload = function () {
            let base = e.target.result
            userApi.upload(formdata).then(res => {
              that.$emit('addImage', res.data.url, base,length)
            }).catch(e => {
              that.$emit('addImage', '', '',length)
            })
          }
        }
      }
    }
  }
</script>
<style scoped>
  img {
    height: 100%;
    width: 100%;
  }

  .finish_room2 {
    width: 180px;
    height: 100px;
    display: flex;
    align-items: center;
    margin-left: 10px;
    margin-top: 10px;
  }

  .finish_room2 .margeImg {
    margin-left: 10px;
  }

  .finish_room2 .room_img {
    width: 100%;
    height: 100%;
    cursor: pointer;
    overflow: hidden;
    position: relative;
    text-align: center;
    background-color: rgba(0, 0, 0, .5);
  }

  .finish_room2 .room_img:before {
    /*content: "";*/
    /*width: 0;*/
    /*height: 100%;*/
    /*!*background: #000;*!*/
    /*padding: 14px 18px;*/
    /*position: absolute;*/
    /*top: 0;*/
    /*left: 50%;*/
    /*opacity: 0;*/
  }

  .finish_room2 .room_img:hover:before {
    width: 100%;
    left: 0;
    opacity: 0.5;
  }

  .finish_room2 .room_img .box-content {
    width: 100%;
    padding: 14px 18px;
    color: #fff;
    position: absolute;
    top: 38%;
    left: 0;
  }

  .finish_room2 .room_img .icon {
    padding: 0;
    margin: 0;
    list-style: none;
    margin-top: -20px;
  }

  .finish_room2 .room_img .icon li {
    display: inline-block;
  }

  .finish_room2 .room_img .icon li i {
    display: block;
    width: 40px;
    height: 40px;
    line-height: 40px;
    border-radius: 50%;
    font-size: 20px;
    font-weight: 700;
    color: #fff;
    margin-right: 25px;
    opacity: 0;
    transform: translateY(50px);
    transition: all 0.5s ease 0s;
  }


  @media only screen and (max-width: 990px) {
    .finish_room2 .room_img {
    }
  }

  .finish_room2 .deleteImg {
    color: red;
    position: absolute;
    top: 0px;
    left: 80px;
    cursor: pointer;
  }

  .finish_room2 .room_img img {
    cursor: pointer;
    width: 100%;
    height: 100%;
  }

  .finish_room2 > .room_img span {
    position: absolute;
    width: auto;
    height: auto;
    right: 5px;
    bottom: 3px;
  }

  .finish_room2 .im-button {
    position: absolute;
    top: -14px;
    right: -18px;
    width: 40px;
    height: 40px;
    cursor: pointer;
    border-radius: 50%;
    background-color: rgba(0, 0, 0, .5);
    opacity: 0;
  }

  .finish_room2 .room_img:hover .icon li i {
    opacity: 0.5;
  }

  .finish_room2 .room_img:hover img {
    opacity: 0.5;
  }


  .finish_room2 .room_img:hover .im-button {
    opacity: 1;
  }

  .finish_room2 .im-close {
    transform: rotate(-45deg);
    line-height: 0px;
    left: 6px;
    bottom: 13px;
    display: inline-block;
    width: 15px;
    height: 2px;
    background: #fff;
    position: absolute;
  }

  .finish_room2 .im-close1 {
    left: 5px;
    bottom: 13px;
    display: inline-block;
    width: 15px;
    height: 2px;
    background: #fff;
    position: absolute;
    transform: rotate(45deg);
  }

  .room_add_img {
    background-color: #eeeeee;
    width: 100%;
    height: 100%;
    cursor: pointer;
    border: 1px solid #e1e1e1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
    position: relative;
    z-index: 10;
  }

  .room_add_img > span:nth-child(1) {
    margin-top: 20px;
    width: 40px;
    height: 40px;
    overflow: hidden;
  }

  .room_add_img > span:nth-child(2) {
    margin-bottom: 10px;
  }

  .room_add_img input {
    cursor: pointer;
    position: absolute;
    top: 0px;
    left: 0px;
    width: 100%;
    height: 100%;
    z-index: 99999;
    opacity: 0;
  }
</style>
