<template>
  <div @touchend="moveEvent" @click="moveEvent">
    <!-- 切换栏 -->
    <div class="tabs">
      <v-card>
        <v-tabs
          background-color="#FFFFFF"
          center-active
          color="#FA7D22"
          height="120"
        >
          <v-tab
            @click="selectTab(item)"
            v-for="(item, index) in categoryList"
            :key="index"
            >{{ item.name }}</v-tab
          >
        </v-tabs>
      </v-card>
    </div>
    <!-- 商品轮播图 -->
    <div class="swiper-container">
      <div class="swiper-wrapper">
        <div
          @click="selectGood(item)"
          v-for="(item, index) in goodList"
          :key="index"
          class="swiper-slide"
        >
          <img :src="item.cover_url" alt="" />
        </div>
      </div>
    </div>
    <!-- 商品大图标题 -->
    <div class="goodsTop">
      <div class="img">
        <img :src="goodInfo.cover_url" alt="" />
      </div>
      <div class="goodsTop-right">
        <div class="goodsTopTitle">
          {{ goodInfo.goods_name }}
        </div>
        <div class="price">
          ￥<span>{{ goodInfo.price }}</span
          >.00
        </div>
      </div>
    </div>
    <!-- 功效适用皮肤 -->
    <div class="goodsExplain">
      <div class="effect">
        <img
          src="https://we-spa.oss-cn-shenzhen.aliyuncs.com/H5/shangcheng/%E5%8A%9F%E6%95%88%402x.png"
          alt=""
        />
        <div
          :class="{
            effectRight: true,
            infoOver: isOver,
          }"
        >
          <div class="effect-right-title">功效</div>
          <div class="effect-right-content">
            {{ goodInfo.effect
            }}{{ goodInfo.effect ? goodInfo.effect.lenght : "" }}
          </div>
        </div>
      </div>
      <div class="skin">
        <img
          src="https://we-spa.oss-cn-shenzhen.aliyuncs.com/H5/shangcheng/%E9%80%82%E7%94%A8%E7%9A%AE%E8%82%A4%402x.png"
          alt=""
        />
        <div
          :class="{
            skinRight: true,
            infoOver: isSkinOver,
          }"
        >
          <div class="skin-right-title">适用皮肤</div>
          <div class="skin-right-content">{{ goodInfo.skin_type }}</div>
        </div>
      </div>
    </div>
    <!-- 重要成分 -->
    <div class="ingredient">
      <div class="QRcode">
        <div id="qrcode" ref="qrcode"></div>
      </div>
      <div class="order">微信扫码下单</div>
      <img
        class="background"
        src="https://we-spa.oss-cn-shenzhen.aliyuncs.com/H5/shangcheng/%E9%87%8D%E8%A6%81%E6%88%90%E5%88%86%E8%83%8C%E6%99%AF%402x.png"
        alt=""
      />
      <div class="ingredient-top">
        <div class="icon"></div>
        <div class="ingredient-title">重要成分</div>
      </div>
      <!-- <div class="ingredient-content">
        <div class="ingredient-content-title">1.精纯珍贵二裂酵母</div>
        <div class="content">嫩滑细腻 紧实肌肤</div>
      </div>
      <div class="ingredient-content">
        <div class="ingredient-content-title">2.精纯珍贵二裂酵母</div>
        <div class="content">嫩滑细腻 紧实肌肤</div>
      </div> -->
      <div class="ingredient-content">
        <div class="content">{{ goodInfo.component }}</div>
      </div>
    </div>
  </div>
</template>

<script>
var Swiper = require("../../utils/swiper-bundle.min");
import { getBanner, getCategory, getGoodList, getGoodInfo } from "../../api";
import QRCode from "qrcodejs2";
export default {
  components: {
    QRCode,
  },
  data() {
    return {
      timmer: null, //倒计时函数
      categoryList: [], //商品分类列表
      goodList: [], //商品列表
      goodInfo: [], //商品详情
      qrcode: "",
      isOver: false,
      isSkinOver: false,
      goods_id: "", //商品ID
    };
  },
  created() {
    // 获取banner传过来的商品ID
    this.goods_id = this.$route.query.goods_id;
    // 获取商品分类列表
    this.getCategory();
  },
  mounted() {
    this.swiper();
    this.init();
  },
  methods: {
    /**
      监听鼠标滑动和点击事件，重置超时时间
     */
    moveEvent() {
      clearTimeout(this.timmer);
      this.init();
    },
    /**
      用户超时定时器 5分钟
     */
    init() {
      this.timmer = setTimeout(() => {
        // 执行事件
        this.$router.replace({
          path: "/",
        });
      }, 300000);
    },
    // 解析富文本
    showHtml(str) {
      return str
        .replace(str ? /&(?!#?\w+;)/g : /&/g, "&amp;")
        .replace(/&lt;/g, "<")
        .replace(/&gt;/g, ">")
        .replace(/&quot;/g, '"')
        .replace(/&#39;/g, "'")
        .replace(/&amp;nbsp;/g, "\u3000");
    },
    // swiper轮播图
    swiper() {
      let effect = 1;
      let that = this;
      var swiper = new Swiper(".swiper-container", {
        speed: 2500,
        slidesPerView: 5,
        spaceBetween: 30,
        centeredSlides: true,
        watchSlidesProgress: true,
        slideToClickedSlide: true,
        preloadImages: true,
        on: {
          setTranslate: function () {
            let slides = this.slides;
            for (let i = 0; i < slides.length; i++) {
              let slide = slides.eq(i);
              let progress = slides[i].progress;
              //slide.html(progress.toFixed(2)); 看清楚progress是怎么变化的
              slide.css({ opacity: "", background: "" });
              slide.transform(""); //清除样式
              if (effect == 1) {
                slide.transform("scale(" + (1 - Math.abs(progress) / 8) + ")");
              }
            }
          },
          setTransition: function (transition) {
            for (var i = 0; i < this.slides.length; i++) {
              var slide = this.slides.eq(i);
              slide.transition(transition);
            }
          },
          transitionStart() {
            // 如果是第一张或者第二张就不居中
            if (this.activeIndex == 0 || this.activeIndex == 1) {
              this.setTranslate(0);
            }
            // 最后一张或者两张不动
            if (
              this.activeIndex == this.slides.length - 2 ||
              this.activeIndex == this.slides.length - 1
            ) {
              let slideW = (this.width / 5).toFixed(5); //每个轮播的宽度
              let keepW = slideW * (this.slides.length - 5); //需要设置的setTranslate的宽度
              this.setTranslate(-keepW);
            }
          },

          // 滑动切换商品详情
          touchEnd: function (swiper, event) {
            //你的事件
            let id = that.goodList[swiper.activeIndex].id;
            that.getGoodInfo(id);
          },
        },
      });
    },
    // 二维码
    qrcodeScan() {
      //生成二维码
      document.getElementById("qrcode").innerHTML = "";
      new QRCode(document.getElementById("qrcode"), {
        width: 117, // 二维码宽度
        height: 117, // 二维码高度
        text: this.goodInfo.qrcode_content, // 浏览器地址url
        colorDark: "#000000",
        colorLight: "#ffffff",
        correctLevel: QRCode.CorrectLevel.H,
      });
    },
    // 获取商品分类列表
    getCategory() {
      getCategory().then((res) => {
        if (res.status == 200) {
          this.categoryList = res.data.data.list;
          this.getGoodList();
        }
      });
    },
    // 获取商品列表
    getGoodList(id) {
      let params = {
        page: 1,
        limit: 10,
        category_id: id ? id : this.categoryList[0].id, //分类ID
      };
      getGoodList(params).then((res) => {
        if (res.status == 200) {
          this.goodList = res.data.data.list;
          this.getGoodInfo();
        }
      });
    },
    // 获取商品详情
    getGoodInfo(id) {
      this.isOver = false;
      this.isSkinOver = false;
      getGoodInfo({
        id: this.goods_id
          ? this.goods_id
          : id
          ? id
          : this.goodList[0]
          ? this.goodList[0].id
          : "",
      }).then((res) => {
        if (res.status == 200) {
          if (this.goodInfo.id == res.data.data.id) {
            return;
          } else {
            if (res.data.data.goods_name.length > 20) {
              let c = res.data.data.goods_name.substring(
                20,
                res.data.data.goods_name.length
              );
              let b = res.data.data.goods_name.slice(0, 20);
              let a = c.replace(c, "...");
              res.data.data.goods_name = b + a;
            }
            if (res.data.data.price) {
              let priceA = res.data.data.price.indexOf(".");
              res.data.data.price = res.data.data.price.slice(0, priceA);
            }
            if (res.data.data.effect.length > 7) {
              this.isOver = true;
            }
            if (res.data.data.skin_type > 7) {
              this.isSkinOver = true;
            }
            this.goodInfo = res.data.data;
            this.qrcodeScan();
          }
        }
      });
    },
    // 选择商品分类
    selectTab(e) {
      // 重置banner传过来的商品
      this.goods_id = "";
      let id = e.id;
      this.getGoodList(id);
    },
    // 选择商品
    selectGood(e) {
      // 重置banner传过来的商品
      this.goods_id = "";
      let id = e.id;
      this.getGoodInfo(id);
    },
  },
};
</script>

<style scoped lang='less'>
@import "../../utils/swiper-bundle.min.css";
// 切换栏
/deep/.v-tab {
  font-size: 40px;
  color: #333333;
  padding: 40px;
}
/deep/.swiper-slide.swiper-slide-visible.swiper-slide-active {
  border: 2px solid #fa7d22;
}
/deep/.v-tabs:not(.v-tabs--vertical):not(.v-tabs--right)
  > .v-slide-group--is-overflowing.v-tabs-bar--is-mobile:not(.v-slide-group--has-affixes)
  .v-slide-group__prev {
  display: none;
  visibility: hidden;
}
// 商品轮播
body {
  color: #000;
  margin: 0;
  padding: 0;
}
.swiper-container {
  width: 100%;
  height: auto;
  margin-left: auto;
  margin-right: auto;
  margin-top: 40px;
}
.swiper-slide {
  text-align: center;
  font-size: 18px;
  background: #fff;
  height: 240px;
  border-radius: 20px;
  /* Center slide text vertically */
  display: -webkit-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  -webkit-justify-content: center;
  justify-content: center;
  -webkit-box-align: center;
  -ms-flex-align: center;
  -webkit-align-items: center;
  align-items: center;
  transition-property: all;
  img {
    width: 100%;
    height: 100%;
  }
}
#progressEffect {
  position: absolute;
  z-index: 10;
  top: 20px;
  left: 20px;
}
// 商品大图
.goodsTop {
  width: 980px;
  height: 239px;
  background-color: #fff;
  border-radius: 20px;
  margin-top: 160px;
  margin-left: 50px;
  position: relative;
  .img {
    width: 280px;
    height: 360px;
    position: absolute;
    left: 0;
    top: -122px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .goodsTop-right {
    margin-left: 314px;
    padding: 45px 20px 20px;
    height: 100%;
    position: relative;
    .goodsTopTitle {
      color: #333333;
      font-size: 45px;
      font-weight: 700;
    }
    .price {
      position: absolute;
      font-size: 39px;
      color: #fa4622;
      bottom: 20px;
      right: 20px;
      span {
        font-size: 65px;
      }
    }
  }
}
// 功效适用皮肤
.goodsExplain {
  margin-top: 30px;
  display: flex;
  .effect {
    height: 240px;
    width: 465px;
    margin-left: 50px;
    border-radius: 20px;
    background-color: #fff;
    padding: 45px 20px 45px 40px;
    display: flex;
    align-items: center;
    img {
      width: 150px;
      height: 150px;
    }
    .effectRight {
      padding: 24px 0;
      height: 150px;
      margin-left: 20px;
      font-size: 30px;
      color: #333;
      .effect-right-content {
        font-weight: 600;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
      }
    }
    .infoOver {
      padding: 0;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      overflow: hidden;
      text-overflow: ellipsis;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;
    }
  }
  .skin {
    height: 240px;
    width: 465px;
    margin-left: 50px;
    border-radius: 20px;
    background-color: #fff;
    padding: 45px 20px 45px 40px;
    display: flex;
    img {
      width: 150px;
      height: 150px;
    }
    .skinRight {
      padding: 24px 0;
      height: 150px;
      margin-left: 20px;
      font-size: 30px;
      color: #333;
      .skin-right-content {
        font-weight: 600;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
      }
    }
    .infoOver {
      padding: 0;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      overflow: hidden;
      text-overflow: ellipsis;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;
    }
  }
}
// 重要成分
.ingredient {
  height: 700px;
  width: 980px;
  background-color: #fff;
  border-radius: 20px;
  margin-left: 50px;
  margin-top: 30px;
  position: relative;
  .QRcode {
    position: absolute;
    width: 200px;
    height: 200px;
    border-radius: 20px;
    border: 2px solid #fa7d22;
    top: 59px;
    right: 54px;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 10px;
  }
  .order {
    font-size: 26px;
    color: #666666;
    position: absolute;
    top: 274px;
    right: 76px;
  }
  .background {
    position: absolute;
    right: 0;
    bottom: 0;
    width: 456px;
    height: 417px;
  }
  .ingredient-top {
    display: flex;
    .icon {
      width: 10px;
      height: 50px;
      background-color: #fa7d22;
      border-radius: 5px;
      margin-left: 51px;
      margin-top: 50px;
    }
    .ingredient-title {
      font-size: 40px;
      font-weight: 700;
      color: #333333;
      margin-left: 20px;
      margin-top: 46px;
    }
  }
  .ingredient-content {
    width: 600px;
    margin-left: 70px;
    .ingredient-content-title {
      margin-top: 50px;
      font-size: 40px;
      color: #333;
    }
    .content {
      margin-top: 30px;
      font-size: 30px;
      color: #666666;
      margin-left: 32px;
    }
  }
}
</style>