<template>
  <div class="swiper-container">
    <div class="swiper-wrapper">
      <div class="swiper-slide">
        <Card1 :anualData="anualData"></Card1>
      </div>
      <div class="swiper-slide">
        <Card2 :anualData="anualData"></Card2>
      </div>
      <div v-if="anualData.orderNum" class="swiper-slide">
        <Card3 :anualData="anualData"></Card3>
      </div>
      <div v-if="anualData.learnTime4Show" class="swiper-slide">
        <Card4 :anualData="anualData"></Card4>
      </div>
      <div class="swiper-slide">
        <Card5 :anualData="anualData"></Card5>
      </div>
      <div class="swiper-slide">
        <Card6 :anualData="anualData"></Card6>
      </div>
    </div>
    <Loading v-show="showLoading" />
  </div>
</template>
<script>
import Loading from "@/components/common/common-loading";
import { Progress, Toast } from "vant";

import Card1 from "@/components/annual-summary/card-1";
import Card1No from "@/components/annual-summary/card-1-no";
import Card2 from "@/components/annual-summary/card-2";
import Card3 from "@/components/annual-summary/card-3";
import Card4 from "@/components/annual-summary/card-4";
import Card5 from "@/components/annual-summary/card-5";
import Card6 from "@/components/annual-summary/card-6";
import { mapGetters, mapActions } from "vuex";
import Swiper from "swiper";

let _this = null;

export default {
  components: {
    Loading,
    Card1,
    Card1No,
    Card2,
    Card3,
    Card4,
    Card5,
    Card6
  },
  data() {
    return {
      token: "5EE5FA068BD646C687239ED119377185",
      isWeb: window.__isWeb,
      isInfresh: false,
      showLoading: false,
      anualData: {
        orderNum: 1,
        learnTime4Show: 1
      }
    };
  },

  computed: {
    ...mapGetters(["userInfo"])
  },

  created() {
    _this = this;

    window.__getUserInfoAS = function(param) {
      console.log("__getUserInfoAS", param);
      _this.token = param.userToken;
      _this.userMobile = param.userMobile;
      _this.setUserInfo(param);
      _this.checkToken();
      _this.initData();
    };
    if (_this.isWeb) {
      _this.initData();
    }
    window.__refresh = function() {
      _this.isInfresh = true;
      _this.getUserInfo();
    };

    _this.getUserInfo();
  },

  mounted() {},

  methods: {
    ...mapActions(["setUserInfo"]),

    // 获取数据
    initData() {
      let param = {
        token: this.token || "A0774D88168047CC9BE57B8BAB4F6F50",
        yearType: "2020",
        setEntry: true
      };
      this.showLoading = true;
      this.GET(`stats/year/end/report/app`, param).then(res => {
        if (res.code == "000000") {
          this.handlerDate(res.data || {});
          this.$forceUpdate();
          this.$nextTick(() => {
            setTimeout(() => {
              this.showLoading = false;
              this.initSwiper();
            }, 100);
          });
        } else {
          this.$toast(res.message);
        }
      });
    },

    // 对数据进行处理
    handlerDate(anualData) {
      let learnTime4Show = 0,
        loginTotalTime = 0,
        loginTotalTimeM = 0,
        loginTotalTimeH = 0,
        orderTotalAmount = 0,
        orderTotalAmountY = 0;
      if (anualData.learnEarliestTimeFlag == 1) {
        learnTime4Show = anualData.learnEarliestTime;
      } else if (anualData.learnLatestTimeFlag == 1) {
        learnTime4Show = anualData.learnLatestTime;
      } else {
        learnTime4Show =
          anualData.learnEarliestTime || anualData.learnLatestTime;
      }
      anualData.learnTime4Show = learnTime4Show;

      loginTotalTime = anualData.loginTotalTime || 0;
      if (loginTotalTime) {
        loginTotalTimeM = Math.floor(loginTotalTime / 1000);
        loginTotalTimeH = Math.floor(loginTotalTime / 1000 / 60);
      }
      anualData.loginTotalTimeM = loginTotalTimeM;
      anualData.loginTotalTimeH = loginTotalTimeH;

      orderTotalAmount = anualData.orderTotalAmount;
      if (orderTotalAmount) {
        orderTotalAmountY = Math.floor(orderTotalAmount / 100);
      }
      anualData.orderTotalAmountY = orderTotalAmountY;
      this.anualData = anualData;
    },

    // 初始化Swiper
    initSwiper() {
      new Swiper(".swiper-container", {
        direction: "vertical",
        speed: 300,
        slidesPerView: 1,
        // observer: true, //修改swiper自己或子元素时，自动初始化swiper
        // observeParents: true //修改swiper的父元素时，自动初始化swiper
        // // slidesPerGroup : 5,
        on: {
          init: function() {
            swiperAnimateCache(this);
            console.log("---111");
            swiperAnimate(this);
          },
          slideChangeTransitionEnd: function(se) {
            let activeIndex = se.activeIndex;
            swiperAnimate(this);
            console.log("---2323", se);
            if (activeIndex == 1) {
              document.getElementById("detail-content").className =
                "shareanimate";
            }
          }
        }
      });
    },

    //获取用户信息
    getUserInfo() {
      rocNative.getUserInfo({
        __funcName: "__getUserInfoAS"
      });
    },

    // token是否失效校验
    checkToken() {
      let param = {
        token: this.anualData.userToken || this.token,
        setEntry: true
      };
      this.GET("campaign/admin/task/checkToken", param).then(res => {
        if (res.code !== "000000") {
          console.log("跳登录", this.isInfresh);
          if (this.isInfresh) {
            rocNative.goBack();
          } else {
            rocNative.gotoLogin();
          }
          this.isInfresh = false;
        } else {
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
// .as-swipper-wrapper {
//   transform: translate3d(0, 0, 0);
//   // background: red;
//   .swiper-container {
//     transform: translate3d(0, 0, 0);
//     overflow: hidden;
//   }
//   .swiper-slide {
//     transform: translate3d(0, 0, 0);
//   }
// }

.swiper-container {
  height: 100vh;
  width: 100vm;
  /* height: 300px;
  margin: 20px auto; */
}
.card {
  height: 100vh;
}
</style>