<template>
    <div :class="pageStyle">
        <Headside></Headside>
        <div class="rankContainer">
            <div class="sumScore">
                <div class="sumlt">
                    <span class="sumCK">
                        <em>{{redSum}}</em><i>Ck</i>
                    </span>
                    <span class="flagContainer">
                        <img src="../assets/img/2pkRank/flag.svg" alt="" v-if="redSum >= blueSum">
                    </span>
                    <span class="mineUniteName">
                        红队
                    </span>
                </div>
                <div class="summd"></div>
                <div class="sumrt">
                    <span class="mineUniteName">
                        蓝队
                    </span>
                    <span class="flagContainer">
                        <img src="../assets/img/2pkRank/flag.svg" alt="" v-if="redSum <= blueSum">
                    </span>
                    <span class="sumCK">
                        <em>{{blueSum}}</em><i>Ck</i>
                    </span>
                </div>
            </div>
            <el-row :gutter="20">
                <el-col :span="12">
                    <li v-for="(s,i) in students.redUnite" :class="[{'redUnite':true},{'mvpli':i == 0}]">
                        <div>
                            <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="../assets/img/2pkRank/king.svg" class="king" alt="" v-if="redSum >= blueSum">
                            </div>
                            <div class="name">
                                {{s.Name}}
                            </div>
                            <div class="CLEscore">
<!--                                <em>Cal</em>-->
                                <em>胜率</em>
                                <span v-if="redSum >= blueSum">100%</span>
                                <span v-else>0%</span>
<!--                                {{s.Cle|fmtInt}}-->
                            </div>
                            <div class="CKscore">
                                <em>CK</em>
                                {{s.Ck|fmtFloat}}
                            </div>
                            <div class="record">
                                <img src="../assets/img/2pkRank/redMvp.svg" class="mvpImg" alt=""
                                     v-if="i == 0">
                            </div>
                        </div>
                    </li>
                </el-col>
                <!--CK-->
                <el-col :span="12">
                    <li v-for="(s,i) in students.blueUnite"
                        :class="[{'blueUnite':true},{'mvpli':i == 0}]">
                        <div>
                            <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="../assets/img/2pkRank/king.svg" class="king" alt="" v-if="redSum <= blueSum">
                            </div>
                            <div class="name">
                                {{s.Name}}
                            </div>
                            <div class="CLEscore">
                              <em>胜率</em>
                              <span v-if="redSum <= blueSum">100%</span>
                              <span v-else>0%</span>
                            </div>
                            <div class="CKscore">
                                <em>CK</em>
                                {{s.Ck|fmtFloat}}
                            </div>
                            <div class="record">
                                <img src="../assets/img/2pkRank/blueMvp.svg" class="mvpImg" alt=""
                                     v-if="i == 0">
                            </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: {
                    redUnite: [],
                    blueUnite: [],
                },
                rankTimer: null,
                redSum: 0,
                blueSum: 0,
                totalTime:60
            }
        },
        mounted() {
            let that = this;
            if (this.trueDate) {
                that.init();
                // this.rankTimer = setInterval(() => {
                //     this.getClassUserRank();
                // }, 5000);
            } else {
                let Rs = fakeNews(18,2);
                that.UniteBreak(Rs);
            }
        },
        watch: {
            '$route': function (val) {
                if (val.path == '/2pkRank') {
                    if (this.trueDate) {
                        that.init();
                        // this.rankTimer = setInterval(() => {
                        //     this.getClassUserRank();
                        // }, 5000);

                    } else {
                        let Rs = fakeNews(18,2);
                        this.UniteBreak(Rs);
                    }

                } else {
                    clearInterval(this.rankTimer);
                }
            }
        },
        beforeDestroy() {
            clearInterval(this.rankTimer);
            this.rankTimer = null;
        },
        methods: {
            init(){
                this.getClassUserRank();
            },
            // 分队展示
            UniteBreak(Rs) {
                let that = this;
                that.students.redUnite = [];
                that.students.blueUnite = [];
                Rs.map(function (item, t) {
                    if (item.GroupNo == 1) {
                        that.students.redUnite.push(item);
                    }
                    if (item.GroupNo == 2) {
                        that.students.blueUnite.push(item);
                    }
                });
                // 排序
                that.students.redUnite = that.students.redUnite.sort(function (a, b) {
                    // return b.Cle - a.Cle;
                    return b.Ck - a.Ck;
                });
                that.students.blueUnite = that.students.blueUnite.sort(function (a, b) {
                    // return b.Cle - a.Cle;
                    return b.Ck - a.Ck;
                });
                that.calcSumCK(that.students);
            },
            jumpWait(totalTime) {
                // 倒计时60秒自动关闭
                let that = this;
                let clock = window.setInterval(() => {
                    totalTime--;
                    console.log(totalTime--);
                    if (parseInt(totalTime) < 0) {
                        // 前往等待页面
                        that.$router.push({path: '/'});
                        // 计时器回收
                        clearInterval(clock);
                    }
                }, 1000)
            },
            // 计算各队总分
            calcSumCK(Rs) {
                let that = this;
                let redSum = 0;
                let blueSum = 0;
                Rs.redUnite.map(function (item, t) {
                    redSum += parseFloat(item.Ck.toFixed(1))
                });
                Rs.blueUnite.map(function (item, t) {
                    blueSum += parseFloat(item.Ck.toFixed(1))
                });
                that.redSum = redSum.toFixed(1);
                that.blueSum = blueSum.toFixed(1);
            },
            getClassUserRank() {
                let that = this;
                let param = {
                    eqSn: localStorage.eqSn
                };
                let postdata = qs.stringify(param);
                ClassUserRank(postdata).then(res => {
                    let json = res;
                    that.students.redUnite = [];
                    that.students.blueUnite = [];
                    if (json.Code == 0) {
                        that.UniteBreak(json.Rs);

                        let Studenlength = json.Rs.length;
                        let totalTime = 0;
                        // 根据人数多少显示停留时间
                        if (Studenlength > 3) {
                            totalTime = 60
                        } else {
                            totalTime = 40
                        }
                        this.jumpWait(totalTime);
                        // this.students.CkSort = json.Rs.CkSort;
                        // this.students.CalSort = json.Rs.CalSort;
                    } 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;
    }

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

    .sumScore {
        width: 100%;
        overflow: hidden;
        display: block;
        margin: 0 auto;
        height: 1.4rem;
        background: url("../assets/img/2pkRank/2pkTitle.svg");
        background-size: 100%;
        background-position: top center;
        background-repeat: no-repeat;
    }

    li {
        width: 100%;
        height: 0.8rem;
        display: block;
        margin: 0 auto;
        overflow: hidden;
        margin-bottom: 0.06rem;
    }

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

    li .head {
        width: 0.6rem;
        height: 0.7rem;
        float: left;
        margin-left: 0.1rem;
        margin-top: 0.07rem;
        overflow: visible;
    }

    li .head .headImg {
        width: 100%;
        height: 0.6rem;
        overflow: hidden;
        display: block;
        margin: 0 auto;
        border: 0.04rem solid #fff;
        border-radius: 250px;
    }


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

    li .record {
        position: relative;
        top: -0.2rem;
        float: right;
        margin-right: 2%;
        /*line-height: 0.8rem;*/
    }

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

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

    .sumlt {
        width: 45%;
        float: left;
        color: #E4FF00;
    }

    .sumrt {
        width: 45%;
        float: right;
        color: #fff;
    }

    .summd {
        width: 10%;
        float: left;
        height: 1rem;
    }

    .sumlt .sumCK {
        width: 30%;
        float: left;
        margin-left: 30%;
    }

    .sumCK em {
        font-size: 0.6rem;
    }

    .sumCK i {
        font-size: 0.24rem;
    }

    .sumlt .flagContainer {
        width: 10%;
        float: left;
        margin-left: 5%;
    }

    .flagContainer img {
        width: 1rem;
        height: 1rem;
    }

    .sumlt .mineUniteName {
        width: 20%;
        float: right;
        text-align: left;
        font-size: 0.5rem;
        line-height: 1.4rem;
    }


    .sumrt .sumCK {
        width: 30%;
        float: left;
        margin-left: 0%;
    }

    .sumrt em {
        font-size: 0.6rem;
    }

    .sumrt i {
        font-size: 0.24rem;
    }

    .sumrt .flagContainer {
        width: 10%;
        float: left;
        margin-right: 5%;
    }

    .sumrt .mineUniteName {
        width: 18%;
        float: left;
        text-align: left;
        font-size: 0.5rem;
        line-height: 1.4rem;
        margin-left: 8%;
    }

    li.redUnite {
        width: 100%;
        float: left;
        background: rgba(232, 32, 62, 0.75);
        color: #fff;

    }

    li .CLEscore {
        width: 2.6rem;
        float: left;
        font-size: 0.6rem;
        margin-left: 5%;
        line-height: 0.8rem;
        text-align: left;
    }

    li .CLEscore em {
        font-size: 0.3rem;
    }
    li .CLEscore span {
      padding-left: 0.1rem;
      font-size: 0.7rem;
    }

    li .CKscore {
        /*width: 2rem;*/
        float: left;
        font-size: 0.6rem;
        margin-left: 5%;
        line-height: 0.8rem;
        text-align: left;
    }

    li .CKscore em {
        font-size: 0.3rem;
    }

    li .record img {
        width: 0.7rem;
        float: left;
    }

    li.blueUnite {
        width: 99%;
        float: left;
        background: rgba(24, 15, 170, 0.75);
        color: #fff;
    }

    li.mvpli {
        padding-top: 0.15rem;
        padding-bottom: 0.15rem;
    }

    li.mvpli .CLEscore {
        font-size: 0.7rem;
    }

    li.mvpli .CKscore {
        font-size: 0.7rem;
    }

    li.mvpli .head {
        width: 0.7rem;
        height: 0.8rem;
    }

    li.mvpli .head img {
        height: 0.7rem;
        margin-top: -0.05rem;
    }

    li.mvpli .name {
        font-size: 0.5rem;
    }

    li .king {
        width: 0.4rem;
        height: 0.2rem;
        float: left;
        position: relative;
        top: -0.7rem;
        left: -0.1rem;
    }

    li.mvpli .head img.king {
        top: -1rem;
    }
</style>
