<template>
<div>
  <div class="image" @dblclick="show = true">
    <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="img.url" alt="" />
        </div>
      </div>
      <div class="img-detail-right">
        <h2>{{ img.name }}</h2>
        <div class="img-info">
          <h3>图片属性</h3>
          <div class="img-info-item">
            <span>编辑时间：</span><span>{{ img.time | formatDate }} {{ img.time | formatTime }}</span>
          </div>
          <div class="img-info-item">
            <span>图片大小：</span><span>{{ (img.size / 1024).toFixed(2) }}KB</span>
          </div>
          <div class="img-info-item">
            <span>图片尺寸：</span><span>{{ img.size1 }}</span>
          </div>
          <div class="img-info-item">
            <span>图片类型：</span><span>{{ img.type }}</span>
          </div>
          <div class="img-info-item">
            <span>添加人：</span><span>{{img.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) {
      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) {
      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 ? `${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;
    }
  }
}
</script>

<style lang="scss" scoped>
.image {
  margin-right: 10px;
  margin-bottom: 15px;
  width: 160px;
  cursor: pointer;
  border: 1px solid #e4e7ed;
  img {
    vertical-align: middle;
    width: 158px;
    height: 158px;
  }
  .image-name {
    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 {
  width: 640px;
  display: inline-block;
  background: #fff;
  position: relative;
  height: 610px;
  font-size: 14px;
}
.big-img-box {
  width: 348px;
  height: 492px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.big-img-box img {
  max-width: 100%;
  height: 100%;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.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>
