<template>
  <div class="snapshot-detail">
    <div class="carousel">
      <cr-swipe class="carousel-swiper" @change="onChange">
        <cr-swipe-item v-for="(item, index) in imgList" :key="index" class="carousel-swiper-item">
          <cr-image :src="item | Img2Thumb" fit="contain" />
        </cr-swipe-item>
        <template #indicator>
          <div class="custom-indicator">{{ swiperCurrent + 1 }}/{{ imgList.length }}</div>
        </template>
      </cr-swipe>
    </div>
    <div class="skuName">
      {{ skuName }}
    </div>
    <div class="notice">{{ notice }}</div>
    <div class="gray" />
    <div class="attr">
      <span class="label">已选</span>
      <span class="val">{{ attsValue }};{{ count }}件</span>
    </div>
    <div class="gray" />
    <span v-if="detailImgList.length" id="goodDetail" class="goods-divider">宝贝详情</span>
    <div class="goods-detail-imgs">
      <cr-image
        v-for="(item, index) in detailImgList"
        :key="index"
        lazy-load
        style="width: 100%; display: block"
        :src="item"
      />
    </div>
    <!-- <div class="gray" />
    <div class="info">
      <div class="title">产品信息</div>
      <div class="info-item">
        <span class="desc">失效日期</span>
        <span class="value">2022年03月31日—2020年9月29日，具体日期以实际发货批次为准</span>
      </div>
      <div class="info-item">
        <span class="desc">失效日期</span>
        <span class="value">2022年03月31日—2020年9月29日，具体日期以实际发货批次为准</span>
      </div>
      <div class="info-item">
        <span class="desc">失效日期</span>
        <span class="value">2022年03月31日—2020年9月29日，具体日期以实际发货批次为准</span>
      </div>
    </div> -->
  </div>
</template>
<script>
import { Img2Thumb } from '@/filters/img2Thumb.filter';
import api from '@/api/snapshot';
import goods from '@/api/goods.api.js';
export default {
  filters: {
    Img2Thumb
  },
  data() {
    return {
      count: 1,
      attsValue: '',
      detailImgList: [],
      skuName: '',
      // 轮播图
      imgList: [],
      swiperCurrent: 0,
      notice:
        '当前页面内容为订单快照，包含订单创建时的商品描述和下单信息，买和平台在发生交易争议时，该页面作为判断依据'
    };
  },
  created() {
    this.getSkuSnapshot();
  },
  methods: {
    onChange(index) {
      this.swiperCurrent = index;
    },
    async getSkuSnapshot() {
      const { skuId, skuNo, auth } = this.$route.query;
      const [res, err] = await api.getSkuSnapshot(skuId, skuNo, auth);
      if (err) {
        this.$toast('获取订单快照失败');
        return;
      }
      this.count = res.count || 0;
      this.skuName = res.skuName || '';
      this.attsValue = res.attsValue || '';
      this.imgList = res.imageList || [];
      try {
        const detailImages = await goods.getDetailPic(res.detailContentUrl);
        // const imgReg = new RegExp('(?<=src=").[^"]*', 'g');
        // this.detailImgList = (detailImages || '').match(imgReg);
        let imgReg = /<img.*?(?:>|\/>)/gi;
        // eslint-disable-next-line no-useless-escape
        let srcReg = /src=[\'"]?([^\'"]*)[\'"]?/i;
        let arr = (detailImages || '').match(imgReg);
        let srcArr = [];
        for (let i = 0; i < arr.length; i++) {
          let src = arr[i].match(srcReg)[1].replace('http://', 'https://');
          srcArr.push(src);
        }
        this.detailImgList = srcArr;
      } catch (error) {
        console.log(error);
      }
    }
  }
};
</script>
<style lang="less" scoped>
.snapshot-detail {
  width: 100%;
  background: @white;
  .carousel {
    position: relative;
    width: 100%;
    height: 375px;
    overflow: hidden;
    &-swiper {
      height: 100%;

      &-item {
        width: 100%;
        height: 100%;

        &-img {
          width: 100%;
          height: 100%;
        }
      }
    }
    .custom-indicator {
      position: absolute;
      right: 10px;
      bottom: 13px;
      padding: 2px 5px;
      font-size: 12px;
      background-color: #000;
      opacity: 0.4;
      color: #fff;
      border-radius: 10px;
    }
  }
  .skuName {
    font-weight: bold;
    margin: 8px 0;
    padding: 0 10px;
    font-size: 16px;
    color: #333;
    line-height: 22px;
    overflow: hidden;
    text-overflow: ellipsis;
    word-break: break-all;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }
  .notice {
    box-sizing: border-box;
    width: 355px;
    height: 52px;
    margin: 0 auto 8px;
    padding: 8px;
    opacity: 0.7;
    background: #f7f8f9;
    border-radius: 8px;
    font-size: 13px;
    line-height: 18px;
    color: #666;
  }
  .gray {
    width: 100%;
    height: 8px;
    background: #f7f8fa;
  }
  .attr {
    width: 100%;
    height: 48px;
    align-items: center;
    padding-left: 10px;
    box-sizing: border-box;
    .label {
      font-weight: bold;
      font-size: 13px;
      color: #333;
      margin-right: 20px;
    }
    .val {
      font-size: 13px;
      color: #333;
    }
  }
  .info {
    width: 100%;
    padding: 0 10px;
    box-sizing: border-box;
    .title {
      height: 40px;
      display: flex;
      align-items: center;
      font-weight: bold;
      font-size: 16px;
      color: #333;
      padding-top: 10px;
      margin-bottom: 5px;
    }
    &-item {
      line-height: 17px;
      font-size: 12px;
      display: flex;
      padding-bottom: 15px;
      box-sizing: border-box;
      .desc {
        width: 90px;
        color: #999;
      }
      .value {
        color: #666;
      }
    }
  }
}
.goods-divider {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 15px 0;
  width: 100%;
  font-size: 12px;
  color: #999;
  text-align: center;
  &:before,
  &:after {
    display: inline-block;
    content: '';
    width: 50px;
    height: 0;
    border-top: 1px solid #d8d8d8;
    margin-right: 5px;
  }
  &:after {
    margin-left: 5px;
    margin-right: 0;
  }
}
</style>
