<template>
  <div :class="pageStyle">
    <Headside></Headside>
    <div class="tips">
      {{ totalTime }}
    </div>
    <div class="rankContainer">
      <el-row :gutter="20">
        <!--卡路里-->
        <el-col :span="12">
          <!--          只显示前8个-->
          <li v-for="(s,i) in students.CalSort" v-if="i <= 7">
            <div
                :class="['rows',{'row_0':i == 0},{'row_1':i == 1},{'row_2':i == 2},{'row_3':i == 3},{'row_4':i == 4},{'row_5':i == 5},{'row_6':i == 6},{'row_7':i == 7}]">
              <div class="head">
                <img :src="s.Head" class="headImg" v-if="s.Head">
                <img src="../static/img/people/flyhead.png" class="headImg" alt="" v-if="!s.Head">
                <img src="../static/img/rank/king1.svg" class="king" alt="" v-if="i == 0">
                <img src="../static/img/rank/king2.svg" class="king" alt="" v-if="i == 1">
                <img src="../static/img/rank/king3.svg" class="king" alt="" v-if="i == 2">
              </div>
              <div class="name">
                NO.{{ i + 1 }} {{ s.Name }}
              </div>
              <div class="score">
                {{ s.Cle|fmtInt }}
                <em>千卡</em>
              </div>
              <div class="record">
                <img src="../static/img/rank/recordIcon.svg" class="king" alt=""
                     v-if="s.IsNewUser ==0 && s.IsBreaking == 1">
              </div>
            </div>
          </li>
        </el-col>
        <!--CK-->
        <el-col :span="12">
          <li v-for="(s,i) in students.CkSort" v-if="i <= 7">
            <div
                :class="['rows',{'row_0':i == 0},{'row_1':i == 1},{'row_2':i == 2},{'row_3':i == 3},{'row_4':i == 4},{'row_5':i == 5},{'row_6':i == 6},{'row_7':i == 7}]">
              <div class="head">
                <img :src="s.Head" class="headImg" v-if="s.Head">
                <img src="../static/img/people/flyhead.png" class="headImg" alt="" v-if="!s.Head">
                <img src="../static/img/rank/king1.svg" class="king" alt="" v-if="i == 0">
                <img src="../static/img/rank/king2.svg" class="king" alt="" v-if="i == 1">
                <img src="../static/img/rank/king3.svg" class="king" alt="" v-if="i == 2">
              </div>
              <div class="name">
                NO.{{ i + 1 }} {{ s.Name }}
              </div>

              <div class="score">
                {{ s.Ck|fmtFloat }}
                <em>CK</em>
              </div>
              <div class="record">
                <img src="../static/img/rank/recordIcon.svg" class="king" alt=""
                     v-if="s.IsNewUser ==0 && s.IsBreaking == 1">
              </div>
            </div>
          </li>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import Headside from '@/components/Headside'
import '../libs/rem';
import '../Global';
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: [],
      },
      rankTimer: null,
      totalTime: ''
    }
  },
  mounted() {
    if (this.trueDate) {
      this.init();
    } else {
      this.students.CkSort = fakeNews(5);
      this.students.CalSort = fakeNews(5);
      this.students.CkSort = this.students.CkSort.sort(function (a, b) {
        return b.Ck - a.Ck;
      });
      this.students.CalSort = this.students.CalSort.sort(function (a, b) {
        return b.Cle - a.Cle;
      });
    }
  },
  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();
    },
    jumpWait(totalTime) {
      // 倒计时60秒自动关闭
      let that = this;
      let clock = window.setInterval(() => {
        totalTime--;
        that.totalTime = totalTime;
        if (parseInt(totalTime) <= 0) {
          // 前往等待页面
          that.$router.push({path: '/'});
          // 计时器回收
          clearInterval(clock);
        }
      }, 1000)
    },
    getClassUserRank() {
      let that = this;
      let param = {
        eqSn: localStorage.eqSn
      };
      let postdata = qs.stringify(param);
      ClassUserRank(postdata).then(res => {
        let json = res;
        if (json.Code == 0) {
          let totalTime = 40;
          let Studenlength = 0;
          if (json.Rs) {
            Studenlength = json.Rs.length;
          }
          // 根据人数多少显示停留时间
          if (Studenlength > 3) {
            totalTime = 60
          } else {
            totalTime = 40
          }
          this.jumpWait(totalTime);
          let CkSort = json.Rs;
          let CalSort = deepClone(json.Rs);
          let Rs = json.Rs;
          if (Rs != '') {
            // 排序
            that.students.CkSort = json.Rs.sort(function (a, b) {
              return b.Ck - a.Ck;
            });

            that.students.CalSort = CalSort.sort(function (a, b) {
              return b.Cle - a.Cle;
            });
          }
        } else {
          this.students.CkSort = [];
          this.students.CalSort = [];
          if (json.Code == '999') {
            // that.$router.push({path: '/'});
          } else {
            // 已出错
            that.$message.error(json.Memo);
          }
        }
      })
    },
  },
  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);
        // return parseFloat(val).toFixed(3);
      }
    },
  },
  components: {
    Headside
  }
}
</script>

<style scoped>
@import "../assets/css/bg.css";

@font-face {
  font-family: vista;
  src: url('../assets/font/vista.otf');
}

* {
  font-family: vista;
}


.pages {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  overflow: hidden;
  display: block;
  margin: 0 auto;
  font-family: vista;
  overflow-y: scroll;
}

.rankContainer {
  width: 96%;
  overflow: visible;
  display: block;
  margin: 0 auto;
  padding-top: 0.2rem;
  overflow-y: scroll;
}

li {
  width: 100%;
  height: 1.2rem;
  display: block;
  margin: 0 auto;
  overflow: hidden;
}

li .rows {
  width: 100%;
  height: 1rem;
  /*background: #ee191d;*/
  border-radius: 250rem;
  overflow: visible;
  display: block;
  margin: 0 auto;
  margin-top: 0.15rem;
}

li .head {
  width: 0.8rem;
  height: 0.8rem;
  float: left;
  margin-left: 0.1rem;
  margin-top: 0.05rem;
  overflow: visible;
}

li .head .headImg {
  width: 0.8rem;
  height: 0.8rem;
  overflow: hidden;
  display: block;
  margin: 0 auto;
  border: 5px solid #fff;
  border-radius: 33rem;
}

li .head .king {
  position: relative;
  top: -1.2rem;
  left: 0rem;
  width: 0.6rem;
  height: 0.6rem;
  float: left;
}

li .name {
  font-weight: normal;
  font-size: 0.4rem;
  text-align: left;
  color: #fff;
  float: left;
  margin-left: 0.2rem;
  line-height: 1rem;
}

li .record {
  float: right;
}

li .record img {
  position: relative;
  top: -0rem;
  width: 1.2rem;
}

li .score {
  font-weight: normal;
  font-size: 0.4rem;
  color: #fff;
  float: right;
  margin-right: 0.2rem;
  line-height: 1rem;
  text-align: right;
}

li .score em {
  font-size: 0.2rem;
}

.row_0 {
  background: rgba(238, 25, 29, 0.75);
}

.row_1 {
  background: rgba(244, 76, 31, 0.75);
}

.row_2 {
  background: rgba(242, 182, 23, 0.75);
}

.row_3 {
  background: rgba(112, 238, 21, 0.75);
}

.row_4 {
  background: rgba(11, 253, 45, 0.75);
}

.row_5 {
  background: rgba(1, 228, 182, 0.75);
}

.row_6 {
  background: rgba(0, 207, 230, 0.75);
}

.row_7 {
  background: rgba(4, 209, 252, 0.75);
}

.tips {
  position: absolute;
  right: 3%;
  top: 1rem;
  font-size: 0.4rem;
  color: #fff;
}

</style>
