<template>
  <div class="content" :class="[{'bg1': pages == 1}, {'bg2': pages == 2}]">
    <Headside></Headside>
    <!-- 训练排名 -->
    <div class="pageXlpm" v-show="pages == 1">
      <div class="sumTitle">
        <div class="lt">
          <img src="../assets/imgs/rank/fire.svg" alt="">
          <h5>卡路里排名</h5>
        </div>
        <div class="rt">
          <img src="../assets/imgs/rank/ck.svg" alt="">
          <h5>CK排名</h5>
        </div>
        <div class="md">
          <span>本次训练排名</span>
        </div>
      </div>
      <div class="listContainer">
        <div class="lt">
          <ul>
            <div class="ltTitle">
              <span>排名</span>
              <span>成员</span>
              <span>卡路里</span>
            </div>
            <li v-for="(s,i) in students.CalSort.slice(0, maxShowNum)" v-if="i <= maxShowNum-1" v-show="Listate[i].state"
                :class="[{ 'box animate__animated animate__flipInX': Listate[i].state }]">
              <div class="gary">
                <em>{{ i + 1 }}</em>
                <div class="head">
                  <img :src="s.Head" alt="" v-if="s.Head" :class="[{'boy':s.Sex == 1},{'girl':s.Sex == 2}]">
                  <!-- <img src="../static/img/people/flyhead.png" alt="" v-if="!s.Head"
                      :class="[{'boy':s.Sex == 1},{'girl':s.Sex == 2}]"> -->
                  <default-head v-if="!s.Head" :class="[{'boy':s.Sex == 1},{'girl':s.Sex == 2}]"></default-head>
                </div>
                <span class="names">{{ s.Name }}</span>
                <span class="glory">
                  <img src="../assets/imgs/rank/pb.png" v-if="s.IsBreaking == 1" alt="">
                  <!-- <img src="../assets/imgs/rank/cr.png" v-if="i == 0" alt="">
                  <img src="../assets/imgs/rank/mr.png" v-if="i == 1" alt=""> -->
                </span>
                <span class="score">{{ s.Cle|fmtInt }}</span>
              </div>
            </li>
          </ul>
        </div>
        <div class="rt">
          <ul>
            <div class="ltTitle">
              <span>排名</span>
              <span>成员</span>
              <span>CK</span>
            </div>
            <li v-for="(s,i) in students.CkSort.slice(0, maxShowNum)" v-if="i <= maxShowNum-1" v-show="Listate[i].state"
                :class="[{ 'box animate__animated animate__flipInX': Listate[i].state }]">
              <div class="gary">
                <em>{{ i + 1 }}</em>
                <div class="head">
                  <img :src="s.Head" alt="" v-if="s.Head" :class="[{'boy':s.Sex == 1},{'girl':s.Sex == 2}]">
                  <!-- <img src="../static/img/people/flyhead.png" alt="" v-if="!s.Head"
                      :class="[{'boy':s.Sex == 1},{'girl':s.Sex == 2}]"> -->
                  <default-head v-if="!s.Head" :class="[{'boy':s.Sex == 1},{'girl':s.Sex == 2}]"></default-head>
                </div>
                <span class="names">{{ s.Name }}</span>
                <span class="glory">
                  <img src="../assets/imgs/rank/pb.png" v-if="s.IsBreaking == 1" alt="">
                  <!-- <img src="../assets/imgs/rank/cr.png" v-if="i == 0" alt="">
                  <img src="../assets/imgs/rank/mr.png" v-if="i == 1" alt=""> -->
                </span>
                <span class="score">{{ s.Ck|fmtFloat }}</span>
              </div>
            </li>
          </ul>
        </div>
      </div>
      <div class="icons">
        <img src="../assets/imgs/rank/icons2.svg" alt="">
      </div>
    </div>
    <!-- 运动强度排名 -->
    <div class="pageYdqdpm" v-show="pages == 2">
      <div class="title">
        <span>运动强度排名</span>
      </div>
      <eiRank :students="students" ref="eiRank"></eiRank>
    </div>
    <div v-show="totalTime > 0" class="countdown">{{totalTime}}</div>
  </div>
</template>

<script>
import Headside from '@/components/Headside'
import defaultHead from '@/components/defaultHead'
import {ClassUserRank} from "@/api/getApiRes"
import eiRank from '@/components/eiRank'

let qs = require('qs');
export default {
  data() {
    return {
      pages: 1,
      eqSn: '30:9C:23:0C:8B:1E',
      pageStyle: RandomBg(),
      trueDate: true,//真实数据 true false
      autoJump: true,//开启自动跳走
      students: {
        CkSort: [],
        CalSort: [],
        EiSort: [],
      },
      rsCount: 0,       //获取到的记录数
      fakeCount: 0,     //记录不足，需要填充的记录数
      maxShowNum: 8,    //页面最大显示记录数
      rankTimer: null,
      page1ShowTime: 30000,   //页面1显示时长 毫秒
      page2ShowTime: 0,       //页面2显示时长 毫秒
      totalTime: '',
      Listate: [
        {state: false},{state: false},{state: false},{state: false},{state: false},{state: false},{state: false},{state: false},
      ],
    }
  },
  mounted() {
    this.init();    
  },
  watch: {
    '$route': function (val) {
      if (val.path == '/rank') {
        if (this.trueDate) {
          this.init();
        } else {
          // this.rankTimer  = setInterval(() => {
          this.students.CkSort = fakeNews(5);
          this.students.CalSort = fakeNews(5);
          // }, 5000);
        }
      } else {
        clearInterval(this.autoJump);
        clearInterval(this.rankTimer);
      }
    },
  },
  beforeDestroy() {
    clearInterval(this.rankTimer);
    this.rankTimer = null;
  },
  methods: {
    init() {
      this.getClassUserRank();
      this.pages = 1;
      // this.$refs.eiRank.pagePlay();
      this.cardPlay();
      this.execWait(this.page1ShowTime, "that.enterPage(2)");
      // N秒后翻页
      // this.rankTimer = setTimeout(() => {
      //   if (this.pages == 1) {
      //     this.pages = 2;
      //     this.$refs.eiRank.pagePlay();
      //   }
      //   else {
      //     this.pages = 1;
      //     this.cardPlay();
      //   }
      // }, this.page1ShowTime);
    },
    enterPage(page) {
      this.pages = page;
      if (page == 2) {
        this.$refs.eiRank.pagePlay();
        this.page2ShowTime = this.$refs.eiRank.showTimeTotal;
        // this.execWait(this.page2ShowTime);
        this.execWait(this.page2ShowTime, "that.$router.push({path: '/'});");
      }
    },
    //倒计时，并在计时结束后执行指定的语句
    execWait(waitTime, execStr="") {
      waitTime = parseInt(waitTime / 1000);
      let that = this;
      clearInterval(clock);
      let clock = window.setInterval(() => {        
        that.totalTime = waitTime;
        if (parseInt(waitTime) <= 0) {
          if (execStr != "") {
            eval(execStr);
          }
          // 计时器回收
          clearInterval(clock);
        }
        waitTime--;
      }, 1000)
    },
    getClassUserRank() {
      let that = this;
      let param = {
        eqSn: localStorage.eqSn
      };
      let postdata = qs.stringify(param);
      ClassUserRank(postdata).then(res => {
        let json = res;
        // console.log(json);
        if (json.Code == 0) {
          let CkSort = json.Rs;
          let CalSort = deepClone(json.Rs);
          let EiSort = deepClone(json.Rs);
          let Rs = json.Rs;
          if (Rs != '') {
            // 排序
            that.students.CkSort = CkSort.sort(function (a, b) {
              return b.Ck - a.Ck;
            });

            that.students.CalSort = CalSort.sort(function (a, b) {
              return b.Cle - a.Cle;
            });

            that.students.EiSort = EiSort.sort(function (a, b) {
              return b.EI - a.EI;
            });
          }

          // let totalTime = 120;
          // let Studenlength = 0;
          // if (json.Rs) {
          //   Studenlength = json.Rs.length;
          // }
          // // 根据人数多少显示停留时间
          // if (Studenlength > 3) {
          //   totalTime = 135
          // } else {
          //   totalTime = 125
          // }
          // this.jumpWait(totalTime);

        } else {
          this.students.CkSort = [];
          this.students.CalSort = [];
          this.students.EiSort = [];
          if (json.Code == '999') {
            // that.$router.push({path: '/'});
          } else {
            // 已出错
            that.$message.error(json.Memo);
          }
        }
      })
    },
    // jumpWait(totalTime) {      
    //   // 倒计时60秒自动关闭
    //   let that = this;
    //   clearInterval(clock);
    //   let clock = window.setInterval(() => {        
    //     that.totalTime = totalTime;
    //     if (parseInt(totalTime) <= 0) {
    //       // 前往等待页面
    //       that.$router.push({path: '/'});
    //       // 计时器回收
    //       clearInterval(clock);
    //     }
    //     totalTime--;
    //   }, 1000)
    // },
    cardPlay() {
      let that = this;
      // 重置一下状态
      that.Listate.map(function (t) {
        t.state = false;
      })
      let timeScope = 100;  //400
      let timeLong;
      that.timer = setTimeout(() => {   //设置延迟执行
        for (let i = 0; i < 8; i++) {
          timeLong = parseInt(i * timeScope);
          that.timer = setTimeout(() => {   //设置延迟执行
            that.Listate[i].state = true;
          }, timeLong)
        }
      }, 60);
    },
  },
  filters: {
    fmtNum(val) {
      if (val == 0) {
        return '--'
      } else {
        if (parseInt(val) < 0) return 0;
        if (parseInt(val) > 0) return val
      }
    },
    fmtFloat(val) {
      if (val == 0) {
        return '0.0'
      } else {
        return parseFloat(val).toFixed(1);
      }
    },
    fmtInt(val) {
      if (val == 0) {
        return '0'
      } else {
        return parseInt(val);
      }
    },
  },
  components: {
    Headside, defaultHead, eiRank
  }
}
</script>

<style lang="scss" scoped>

$font-color: white;
$yellow: #FFEB50;

@mixin bg {
  height: auto;
  min-height: 100%;
  background-color: #333;
  background-repeat: no-repeat;
  background-position: top center;
  background-size: 100% 100%;
}

@mixin cube {
  width: 100%;
  overflow: hidden;
  display: block;
  margin: 0 auto;
}

.bg1 {
  background: url("../../src/assets/imgs/rank/Training.png");
  @include bg;
}

.bg2 {
  background: url("../../src/assets/imgs/rank/bg_ydqdpm.png");
  @include bg;
}

.content {  

  .pageXlpm {
    
    .sumTitle {
      @include cube;
      width: 80%;
      height: 0.75rem;
      margin-top: 0.7rem;
      overflow: visible;

      h5 {
        float: left;
        width: 80%;
        margin: 0;
        text-align: center;
        color: #fff;
        font-size: 0.5rem;
        font-weight: bold;
        line-height: 0.65rem;
      }

      .lt {
        width: 50%;
        height: 0.75rem;
        float: left;
        background: url("../../src/assets/imgs/rank/redTitel.png");
        background-repeat: no-repeat;
        background-position: top center;
        background-size: 100%;
        border-radius: 250rem;

        img {
          width: 0.51rem;
          float: left;
        }
      }

      .rt {
        width: 50%;
        height: 0.75rem;
        float: right;
        background: url("../../src/assets/imgs/rank/redTitel.png");
        background-repeat: no-repeat;
        background-position: top center;
        background-size: 100%;
        border-radius: 250rem;

        img {
          width: 0.8rem;
          float: right;
        }

        h5 {
          float: right;
        }
      }

      .md {
        position: relative;
        width: 3rem;
        height: 0.9rem;
        margin: 0 auto;
        bottom: 0.12rem;
        background: url("../../src/assets/imgs/rank/titleBg.png");
        background-repeat: no-repeat;
        background-position: top center;
        background-size: 100% 100%;
        line-height: 0.7rem;
        span {
          @include cube;
          position: relative;
          bottom: 0.65rem;
          color: #fff;
          text-align: center;
          font-size: 0.35rem;
          font-weight: bold;
        }
      }
    }
  
    .listContainer {
      @include cube;
      width: 80%;
      min-height: 6rem;
      padding-top: 0.5rem;
      padding-bottom: 0.5rem;

      .lt {
        width: 47%;
        float: left;
      }

      .rt {
        width: 47%;
        float: right;
        .ltTitle span {
          margin-right: 0.3rem;
        }
      }

      .ltTitle {
        @include cube;

        span {
          float: left;
          color: #fff;
          font-size: 0.25rem;

          &:nth-child(1) {
            margin-right: 1.5rem;
          }

          &:last-child {
            float: right;
            margin-right: 0.2rem;
          }
        }
      }

      ul {
        @include cube;

        li {
          @include cube;
          height: 0.7rem;
          padding-top: 0.2rem;
          .gary {
            @include cube;
            height: 0.6rem;
            margin-bottom: 0.3rem;
            background: url("../assets/imgs/rank/row.png");
            background-repeat: no-repeat;
            background-position: top center;
            background-size: 100% 100%;
            overflow: visible;
            border: 1px solid rgba(255, 255, 255, 0.1);
            border-radius: 250px;
            border-left: 0;
            border-top-left-radius: 0;
            border-bottom-left-radius: 0;
          }

          em {
            width: 0.6rem;
            height: 0.6rem;
            float: left;
            background: url("../assets/imgs/rank/rowTitle.png");
            background-repeat: no-repeat;
            background-position: top center;
            background-size: 100% 100%;
            text-align: left;
            text-indent: 0.1rem;
            font-size: 0.3rem;
            font-weight: bold;
            text-decoration: none;
            font-style: normal;
            color: white;
            line-height: 0.6rem;
          }

          .head {
            position: relative;
            bottom: 0.5rem;
            float: left;
            margin-left: 0.3rem;

            img {
              width: 0.65rem;
              height: 0.65rem;
              border-radius: 250rem;

              &.girl {
                border: 1px solid #EA26EA;
              }

              &.boy {
                border: 1px solid #39B6FF;
              }
            }
          }

          .names {
            float: left;
            color: white;
            font-size: 0.3rem;
            line-height: 0.6rem;
            margin-left: 0.4rem;
            min-width: 1rem;
            text-align: left;
          }

          .glory {
            float: left;
            margin-left: 0.4rem;

            img {
              float: right;
              width: 0.6rem;
              margin-left: 0.2rem;
              margin-top: 0.1rem;
            }
          }

          .score {
            float: right;
            color: #FFE61F;
            font-size: 0.3rem;
            line-height: 0.6rem;
            font-family: Arial;
            font-weight: bold;
            font-style: italic;
            text-align: center;
            padding-right: 0.2rem;
          }
        }
      }
    }
  }

  .pageYdqdpm {
    
    .title {
      // position: relative;
      width: 3.6rem;
      height: 0.75rem;
      margin: 0 auto;
      margin-top: 0.03rem;
      bottom: 0.1rem;
      background: url("../../src/assets/imgs/rank/titleBg2.png");
      background-repeat: no-repeat;
      background-position: top center;
      background-size: 100% 100%;
      line-height: 0.7rem;
      span {
        @include cube;
        // position: relative;
        bottom: 0.65rem;
        color: #fff;
        text-align: center;
        font-size: 0.35rem;
        font-weight: bold;
      }
    }
  }
}

.icons {
  @include cube;
  position: static;
  padding-bottom: 0.2rem;

  img {
    @include cube;
    // width: 40%;
    width: 18%;
  }
}

.countdown {
  width: 1rem;
  position: fixed;
  font-size: 0.25rem;
  color: white;
  right: 0rem;
  bottom: 0.2rem;
}

/*响应式调整*/
@media (max-aspect-ratio: 1920/1080) {  //max-aspect-ratio: 小于等于   min-aspect-ratio: 大于等于
  .icons {
    position: fixed;
    padding-bottom: 0rem;
    bottom: 0.2rem;
  }    
}
</style>