<template>
  <div class="content">
    <Headside></Headside>
    <div class="title">
      <span>本次训练排名</span>
    </div>
    <div class="sumTitle">
      <div class="lt">
        <img src="../assets/imgs/rank/fire.svg" alt="">
        <h5>卡路里排名</h5>
      </div>
      <div class="md">
        <img src="../assets/imgs/rank/ck.svg" alt="">
        <h5>CK 排名</h5>
      </div>
      <div class="rt">
        <img src="../assets/imgs/rank/ei.svg" alt="">
        <h5>Ei 排名</h5>
      </div>
    </div>
    <div class="listContainer">
      <div class="lt">
        <ul>
          <div class="ltTitle">
            <span>排名</span>
            <span>成员</span>
            <span class="titleCle">卡路里</span>
          </div>
          <li v-for="(s,i) in students.CalSort" v-if="i <= 7" 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="md">
        <ul>
          <div class="ltTitle">
            <span>排名</span>
            <span>成员</span>
            <span class="titleCK">CK</span>
          </div>
          <li v-for="(s,i) in students.CkSort" v-if="i <= 7" 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 class="rt">
        <ul>
          <div class="ltTitle">
            <span>排名</span>
            <span>成员</span>
            <span class="titleEI">Ei</span>
          </div>
          <li v-for="(s,i) in students.EiSort" v-if="i <= 7" 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.EI|fmtInt }}
              </span>
            </div>
          </li>
        </ul>
      </div>
    </div>
    <div class="icons">
      <img src="../assets/imgs/rank/icons2.svg" alt="">
    </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"

let qs = require('qs');
export default {
  data() {
    return {
      eqSn: '30:9C:23:0C:8B:1E',
      pageStyle: RandomBg(),
      trueDate: true,//真实数据 true false
      autoJump: true,//开启自动跳走
      students: {
        CkSort: [],
        CalSort: [],
        EiSort: [],
      },
      rankTimer: null,
      totalTime: '',
      Listate: [
        {state: false},
        {state: false},
        {state: false},
        {state: false},
        {state: false},
        {state: false},
        {state: false},
        {state: false},
      ],
    }
  },
  mounted() {
    // console.log("[Rank] 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);
          this.students.EiSort = fakeNews(5);
          // }, 5000);
        }
      } else {
        clearInterval(this.autoJump);
        clearInterval(this.rankTimer);
      }
    }
  },
  beforeDestroy() {
    clearInterval(this.rankTimer);
    this.rankTimer = null;
  },
  methods: {
    init() {
      this.getClassUserRank();
      this.cardPlay();
    },
    getClassUserRank() {
      // console.log("getClassUserRank");
      let that = this;
      let param = {
        eqSn: localStorage.eqSn
      };
      let postdata = qs.stringify(param);
      ClassUserRank(postdata).then(res => {
        let json = res;
        // console.log("ClassUserRank: " + JSON.stringify(json));
        if (json.Code == 0) {
          let totalTime = 40;
          let Studenlength = 0;
          if (json.Rs) {
            Studenlength = json.Rs.length;
          }
          // 根据人数多少显示停留时间
          if (Studenlength > 3) {
            totalTime = 40 
          } else {
            totalTime = 30
          }
          this.jumpWait(totalTime);  //tmp del 调试时关闭跳走

          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;
            });
          }
        } 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
  }
}
</script>

<style lang="scss" scoped>
@mixin bg {
  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;
}

.content {
  background: url("../../src/assets/imgs/rank/pageBg.png");
  @include bg;

  .title {
    // position: relative;
    width: 3rem;
    height: 0.75rem;
    margin: 0 auto;
    margin-top: -0.38rem;
    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;
    }
  }
  
  .sumTitle {
    @include cube;
    width: 92%;
    height: 0.75rem;
    margin-top: 0.3rem;
    overflow: visible;

    h5 {
      float: left;
      width: 80%;
      margin: 0;
      margin-top: 0.04rem;
      text-align: center;
      color: #fff;
      font-size: 0.46rem;
      font-weight: bold;
    }

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

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

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

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

      h5 {
        margin-left: -0.3rem;
      }
    }

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

      img {
        width: 0.95rem;
        float: left;
      }
      
      h5 {
        margin-left: -0.3rem;
      }
    }
  }
}

.listContainer {
  @include cube;
  width: 92%;
  padding-top: 0.5rem;

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

  .md {
    width: 30%;
    float: left;
    margin-left: 0.92rem;
  }

  .rt {
    width: 30%;
    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;
      }
    }

    .titleCle {
      margin-right: 0.2rem;
    }
    .titleCK {
      margin-right: 0.3rem;
    }
    .titleEI {
      margin-right: 0.5rem;
    }
  }

  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;
      }
    }
  }
}

.icons {
  @include cube;
  position: absolute;
  bottom: 0.2rem;

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

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

</style>