<template>
<div>
  <div class="image" @dblclick="showImage">
    <img :src="img.url" alt="">
    <p class="image-name">{{img.name}}</p>
  </div>
  <el-dialog title="图片详情" :visible.sync="show" width="900px" class="img-dialog" @closed="onClosed">
    <div class="img-detail-content">
      <div class="img-detail-left">
        <i class="el-icon-arrow-left" @click="lastImg"></i>
        <i class="el-icon-arrow-right" @click="nextImg"></i>
        <div class="big-img-box">
          <img :src="imgDetail.url" alt="" />
        </div>
      </div>
      <div class="img-detail-right">
        <h2>{{ imgDetail.name }}</h2>
        <div class="img-info">
          <h3>图片属性</h3>
          <div class="img-info-item" v-if="imgDetail.time">
            <span>编辑时间：</span><span>{{ imgDetail.time | formatDate }} {{ imgDetail.time | formatTime }}</span>
          </div>
          <div class="img-info-item" v-if="imgDetail.size">
            <span>图片大小：</span><span>{{ (imgDetail.size / 1024).toFixed(2) }}KB</span>
          </div>
          <div class="img-info-item" v-if="imgDetail.size1">
            <span>图片尺寸：</span><span>{{ imgDetail.size1 }}</span>
          </div>
          <div class="img-info-item" v-if="imgDetail.type">
            <span>图片类型：</span><span>{{ imgDetail.type }}</span>
          </div>
          <div class="img-info-item" v-if="imgDetail.shopper">
            <span>添加人：</span><span>{{imgDetail.shopper}}</span>
          </div>
        </div>
      </div>
    </div>
  </el-dialog>
</div>
</template>

<script>
export default {
  name: 'img-item',
  props: {
    img: Object,
    imgList: Array,
    index: Number,
  },
  data() {
    return {
      show: false,
      currentIndex: 0,
      imgDetail: {}
    }
  },
  filters: {
    formatDate(ms) {
      if (!ms) return '--';
      let date = new Date(ms);
      let dateArr = [];
      dateArr.push(date.getFullYear());
      dateArr.push(date.getMonth() + 1);
      dateArr.push(date.getDate());
      dateArr = dateArr.map(item => (item < 10 ? `0${item}` : item));
      return dateArr.join('-');
    },
    formatTime(ms) {
      if (!ms) return '--';
      let date = new Date(ms);
      let timeArr = [];
      timeArr.push(date.getHours());
      timeArr.push(date.getMinutes());
      timeArr.push(date.getSeconds());
      timeArr = timeArr.map(item => (item < 10 ? `0${item}` : item));
      return timeArr.join(':');
    }
  },
  created() {
    this.currentIndex = this.index;
  },
  methods: {
    // 上一张
    lastImg() {
      if (this.currentIndex === 0) {
        this.$message.error('已经是当前页的第一张了');
        return false;
      }
      this.currentIndex--;
      this.imgDetail = Object.assign({}, this.imgList[this.currentIndex]);
    },
    //下一张
    nextImg() {
      if (this.currentIndex === this.imgList.length - 1) {
        this.$message.error('已经是当前页的最后一张了');
        return false;
      }
      this.currentIndex++;
      this.imgDetail = Object.assign({}, this.imgList[this.currentIndex]);
    },
    onClosed() {
      this.currentIndex = this.index;
    },
    showImage() {
      this.imgDetail = Object.assign({}, this.img);
      this.currentIndex = this.index;
      this.show = true;
    }
  }
}
</script>

<style lang="scss" scoped>
.image {
  position: relative;
  margin: 0 15px 15px 0;
  padding-bottom: 37px;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 140px;
  height: 140px;
  cursor: pointer;
  border: 1px solid #e4e7ed;
  img {
    max-width: 100%;
    max-height: 100%;
    border-radius: 0;
  }
  
  .image-name {
    position: absolute;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 1;
    margin: 0;
    width: 100%;
    height: 37px;
    font-size: 14px;
    text-align: center;
    line-height: 37px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  &:hover .image-name {
    background-color: #f0f2f5;
  }
}
/deep/.img-dialog .el-dialog__body {
  padding: 0px;
}
.img-detail-content {
  font-size: 0;
  border-top: 1px solid #e4e7eb;
}
.img-detail-left {
  display: inline-block;
  background: #fff;
  position: relative;
  width: 640px;
  height: 610px;
  font-size: 14px;
}
.big-img-box {
  width: 492px;
  height: 492px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.big-img-box img {
  max-width: 100%;
  max-height: 100%;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  user-select: none;
}
.el-icon-arrow-left {
  position: absolute;
  left: 20px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
}
.el-icon-arrow-right {
  position: absolute;
  right: 20px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
}
.img-detail-right {
  display: inline-block;
  background: #f5f7fa;
  width: 260px;
  height: 612px;
  vertical-align: top;
  position: relative;
  font-size: 14px;
}
.img-detail-right h2 {
  font-size: 18px;
  margin: 25px 15px 60px 15px;
  word-break: break-all;
}
.img-info {
  padding-left: 15px;
}
.img-info h3 {
  font-size: 15px;
  padding-bottom: 12px;
  border-bottom: 1px dashed #c0c4cc;
  margin-bottom: 8px;
}
.img-info-item {
  padding: 6px 0;
}
</style>
