<template>
    <div :class="pageStyle">
        <Headside></Headside>
        <div class="tips">
            {{totalTime}}
        </div>
        <div class="rankContainer">
            <div class="sumScore">
                <div class="sumlt">
                    <img src="../assets/img/3pkRank/rank1.svg" alt="" class="rankIcon" v-if="red.rank == 1">
                    <img src="../assets/img/3pkRank/rank2.svg" alt="" class="rankIcon" v-if="red.rank == 2">
                    <img src="../assets/img/3pkRank/rank3.svg" alt="" class="rankIcon" v-if="red.rank == 3">
                    <span class="mineUniteName">
                          <em>
                            红队
                        </em>
                        <span class="sumCK">
                            <em>{{redSum}}</em><i>Ck</i>
                        </span>
                    </span>
                    <span class="flagContainer">
                            <img src="../assets/img/2pkRank/flag.svg" alt="" v-if="red.rank == 1">
                        </span>
                </div>
                <div class="pkIconContainer">
                    <img src="../assets/img/3pkRank/pk.svg" alt="" class="pkIcon">
                </div>
                <div class="summd">
                    <img src="../assets/img/3pkRank/rank1.svg" alt="" class="rankIcon" v-if="blue.rank == 1">
                    <img src="../assets/img/3pkRank/rank2.svg" alt="" class="rankIcon" v-if="blue.rank == 2">
                    <img src="../assets/img/3pkRank/rank3.svg" alt="" class="rankIcon" v-if="blue.rank == 3">
                    <span class="mineUniteName">

                         <em>
                            蓝队
                        </em>

                        <span class="sumCK">
                            <em>{{blueSum}}</em><i>Ck</i>
                        </span>
                    </span>
                    <span class="flagContainer">
                            <img src="../assets/img/2pkRank/flag.svg" alt="" v-if="blue.rank == 1">
                        </span>
                </div>
                <div class="pkIconContainer">
                    <img src="../assets/img/3pkRank/pk.svg" alt="" class="pkIcon">
                </div>
                <div class="sumrt">
                    <img src="../assets/img/3pkRank/rank1.svg" alt="" class="rankIcon" v-if="yellow.rank == 1">
                    <img src="../assets/img/3pkRank/rank2.svg" alt="" class="rankIcon" v-if="yellow.rank == 2">
                    <img src="../assets/img/3pkRank/rank3.svg" alt="" class="rankIcon" v-if="yellow.rank == 3">
                    <span class="mineUniteName">
                        <em>
                            黄队
                        </em>
                        <span class="sumCK">
                            <em>{{yellowSum}}</em><i>Ck</i>
                        </span>
                    </span>
                    <span class="flagContainer">
                            <img src="../assets/img/2pkRank/flag.svg" alt="" v-if="yellow.rank == 1">
                        </span>
                </div>
            </div>
            <div class="list">
                <div class="part">
                    <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="red.rank == 1">
                            </div>
                            <div class="name">
                                {{s.Name}}
                            </div>
                            <div class="CLEscore">
                                <!--                                <em>Cal</em>-->
                                <!--                                {{s.Cle|fmtInt}}-->
                                <em>胜率</em>
                                <span>{{s.success }}%</span>
                            </div>
                            <div class="CKscore">
                                <em>CK</em>
                                {{s.Ck|fmtFloat}}
                            </div>
                            <div class="record">
                                <img src="../assets/img/3pkRank/redMvp.svg" class="mvpImg" alt=""
                                     v-if="i == 0">
                            </div>
                        </div>
                    </li>
                </div>
                <div class="part">
                    <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="blue.rank == 1">
                            </div>
                            <div class="name">
                                {{s.Name}}
                            </div>
                            <div class="CLEscore">
                                <em>胜率</em>
                                <span>{{s.success }}%</span>
                            </div>
                            <div class="CKscore">
                                <em>CK</em>
                                {{s.Ck|fmtFloat}}
                            </div>
                            <div class="record">
                                <img src="../assets/img/3pkRank/blueMvp.svg" class="mvpImg" alt=""
                                     v-if="i == 0">
                            </div>
                        </div>
                    </li>
                </div>
                <div class="part">
                    <li v-for="(s,i) in students.yellowUnite" :class="[{'yellowUnite':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="yellow.rank == 1">
                            </div>
                            <div class="name">
                                {{s.Name}}
                            </div>
                            <div class="CLEscore">
                                <em>胜率</em>
                                <span>{{s.success }}%</span>
                            </div>
                            <div class="CKscore">
                                <em>CK</em>
                                {{s.Ck|fmtFloat}}
                            </div>
                            <div class="record">
                                <img src="../assets/img/3pkRank/yellowMvp.svg" class="mvpImg" alt=""
                                     v-if="i == 0">
                            </div>
                        </div>
                    </li>
                </div>
            </div>

            <!--blue-->
            <!--yellow-->
        </div>
    </div>
</template>

<script>
    import Headside from '@/components/Headside'
    import '../libs/rem';
    import '../Global';
    import {
        ClassUserRank,
        AddPkInfo
    } 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: [],
                    yellowUnite: [],
                },
                red: {
                    rank: 0,
                },
                blue: {
                    rank: 0,
                },
                yellow: {
                    rank: 0,
                },
                rankTimer: null,
                redSum: 0,
                blueSum: 0,
                yellowSum: 0,
                totalTime: ''
            }
        },
        mounted() {
            let that = this;
            if (this.trueDate) {
                that.init();
            } else {
                let Rs = fakeNews(27, 3);
                that.UniteBreak(Rs);
            }

        },
        watch: {
            '$route': function (val) {
                let that = this;
                if (val.path == '/3pkRank') {
                    if (this.trueDate) {
                        that.init();
                    } else {
                        let Rs = fakeNews(18, 3);
                        that.UniteBreak(Rs);
                    }

                } 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)
            },
            // 分队展示
            UniteBreak(Rs, PlanId) {
                let that = this;
                that.students.redUnite = [];
                that.students.blueUnite = [];
                that.students.yellowUnite = [];
                console.log(!Rs);

                Rs.map(function (item, t) {
                    if (item.GroupNo == 1) {
                        that.students.redUnite.push(item);
                    }
                    if (item.GroupNo == 2) {
                        that.students.blueUnite.push(item);
                    }
                    if (item.GroupNo == 3) {
                        that.students.yellowUnite.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.students.yellowUnite = that.students.yellowUnite.sort(function (a, b) {
                    // return b.Cle - a.Cle;
                    return b.Ck - a.Ck;
                });

                that.calcSumCK(that.students, PlanId);

            },
            // 计算各队总分
            calcSumCK(Rs, PlanId) {
                let that = this;
                let redSum = 0;
                let blueSum = 0;
                let yellowSum = 0;
                Rs.redUnite.map(function (item, t) {
                    redSum += parseFloat(item.Ck);
                    that.successCalc(item);
                });
                Rs.blueUnite.map(function (item, t) {
                    blueSum += parseFloat(item.Ck);
                    that.successCalc(item);
                });
                Rs.yellowUnite.map(function (item, t) {
                    yellowSum += parseFloat(item.Ck);
                    that.successCalc(item);
                });
                that.redSum = redSum.toFixed(1);
                that.blueSum = blueSum.toFixed(1);
                that.yellowSum = yellowSum.toFixed(1);
                let res = [
                    {name: "red", val: that.redSum},
                    {name: "blue", val: that.blueSum},
                    {name: "yellow", val: that.yellowSum},
                ];
                that.getUnitRank(res, PlanId, Rs);
            },
            getWhoWin(redUnite, blueUnite, yellowUnite, groupNo) {
                let that = this;
                if (groupNo == 1) {
                    redUnite.map(function (item, t) {
                        that.successCalc(item, 1);
                    });
                    blueUnite.map(function (item, t) {
                        that.successCalc(item, 0);
                    });
                    yellowUnite.map(function (item, t) {
                        that.successCalc(item, 0);
                    });
                } else if (groupNo == 2) {
                    redUnite.map(function (item, t) {
                        that.successCalc(item, 0);
                    });
                    blueUnite.map(function (item, t) {
                        that.successCalc(item, 1);
                    });
                    yellowUnite.map(function (item, t) {
                        that.successCalc(item, 0);
                    });
                } else if (groupNo == 3) {
                    redUnite.map(function (item, t) {
                        that.successCalc(item, 0);
                    });
                    blueUnite.map(function (item, t) {
                        that.successCalc(item, 0);
                    });
                    yellowUnite.map(function (item, t) {
                        that.successCalc(item, 1);
                    });
                }
            },
            // 插入胜率结果
            successCalc(item, Victorl) {
                // item.success = '胜' + (parseInt(item.PkSucessnum) + parseInt(Victorl)) + ' 总:' + (parseInt(item.PkTotalnum) + 1) + '结' + (parseFloat(parseInt(item.PkSucessnum) + parseInt(Victorl)) / (parseInt(item.PkTotalnum) + 1)) * 100

                if (parseInt(item.PkTotalnum + Victorl) == 0) {
                    item.success = 0;
                } else {
                    let res = (parseFloat(parseInt(item.PkSucessnum) + parseInt(Victorl)) / (parseInt(item.PkTotalnum) + 1)) * 100;
                    if (res >= 100) {
                        item.success = 100;
                    } else {
                        item.success = res.toFixed(1);
                    }
                }
            },
            // 结果排序
            getUnitRank(res, PlanId, Rs) {
                let that = this;//这个that不要删
                let sum = res.sort(function (a, b) {
                    return b.val - a.val;
                });
                sum.map(function (item, i) {
                    eval("that." + item['name'] + ".rank = i+1");
                });

                // 上报胜率结果

                let groupNo = 0;
                console.log('name' + sum[0].name);
                switch (sum[0].name) {
                    case "red":
                        groupNo = 1;
                        break;
                    case "blue":
                        groupNo = 2;
                        break;
                    case "yellow":
                        groupNo = 3;
                        break
                }
                // 上报本场胜率
                that.PostAddPkInfo(PlanId, groupNo);
                // 计算本场胜率
                that.getWhoWin(Rs.redUnite, Rs.blueUnite, Rs.yellowUnite, groupNo);
            },
            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 = [];
                    that.students.yellowUnite = [];
                    if (json.Code == 0) {
                        that.UniteBreak(json.Rs, json.Dp.PlanId);

                        let totalTime = 40;
                        let Studenlength = 0;
                        if (json.Rs) {
                            Studenlength = json.Rs.length;
                        }
                        // 根据人数多少显示停留时间
                        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.jumpWait(10);
                        if (json.Code == '999') {
                            // that.$router.push({path: '/'});
                        } else {
                            // 已出错
                            that.$message.error(json.Memo);
                        }
                    }
                })
            },
            // 竞技课程结果上传
            PostAddPkInfo(dpId, groupNo) {
                console.log(dpId, groupNo);
                let that = this;
                let param = {
                    dpId: dpId,
                    groupNo: groupNo,
                };
                let postdata = qs.stringify(param);
                AddPkInfo(postdata).then(res => {
                    let json = res;
                    if (json.Code == 0) {
                        console.log('竞技课程结果上传成功dpid:' + dpId + ' groupNo:' + groupNo);
                    } else {
                        console.log(json.Memo);
                        // 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;
        background: url("../assets/img/threepk/threepkbg.png");
        background-position: top center;
        background-repeat: no-repeat;
        background-size: 100% 100%;
    }

    .rankContainer {
        width: 96%;
        overflow: visible;
        display: block;
        margin: 0 auto;
    }

    .sumScore {
        width: 90%;
        overflow: visible;
        display: block;
        margin: 0 auto;
        height: 1.4rem;
        background-size: 100%;
        background-position: top center;
        background-repeat: no-repeat;
    }

    .pkIconContainer {
        width: 20%;
        float: left;
    }

    .pkIconContainer img {
        width: 1.5rem;
        height: 1.5rem;
        overflow: hidden;
        display: block;
        margin: 0 auto;
    }

    li {
        width: 90%;
        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: 33rem;
    }


    li .name {
        width: 1.2rem;
        font-weight: normal;
        font-size: 0.3rem;
        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: 20%;
        float: left;
        color: #fff;
        height: 1.2rem;
        background: url("../assets/img/3pkRank/redtit.svg");
        background-size: 100%;
        background-position: top center;
        background-repeat: no-repeat;
        margin-top: 0.5rem;
        overflow: visible;
    }

    .summd {
        width: 20%;
        float: left;
        color: #fff;
        height: 1.2rem;
        background: url("../assets/img/3pkRank/bluetit.svg");
        background-size: 100%;
        background-position: top center;
        background-repeat: no-repeat;
        margin-top: 0.5rem;
        overflow: visible;
    }

    .sumrt {
        width: 20%;
        float: left;
        color: #fff;
        height: 1.2rem;
        background: url("../assets/img/3pkRank/yellowtit.svg");
        background-size: 100%;
        background-position: top center;
        background-repeat: no-repeat;
        margin-top: 0.5rem;
        overflow: visible;
    }

    .sumCK {
        float: right;
        font-size: 0.3rem;
        text-align: right;
    }

    .mineUniteName {
        width: 69%;
        height: 0.6rem;
        overflow: visible;
        display: block;
        margin: 0 auto;
        text-align: left;
        font-size: 0.35rem;
    }
    .mineUniteName em {
        float: left;
    }

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

    .sumCK i {
        float: right;
        font-size: 0.24rem;
        margin-top: 0.1rem;
    }

    .flagContainer {
        position: relative;
        top: -1rem;
        float: none;
        right: -1.6rem;
        display: inline-block;
    }

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

    .rankIcon {
        position: relative;
        float: left;
        width: 0.3rem;
        top: -0.1rem;
        left: 0.3rem;
    }

    .list {
        width: 100%;
        overflow: hidden;
        display: block;
        margin: 0 auto;
    }

    .list .part {
        width: 33.333%;
        height: 100%;
        min-height: 1rem;
        float: left;
    }


    li.redUnite {
        width: 96%;
        float: left;
        background: rgba(207, 17, 34, 0.75);
        color: #fff;
        margin-bottom: 0.07rem;
        overflow: hidden;
    }

    li.blueUnite {
        width: 96%;
        margin: 0 auto;
        margin-bottom: 0.05rem;
        background: rgba(24, 15, 170, 0.75);
        color: #fff;
    }

    li.yellowUnite {
        width: 96%;
        float: right;
        background: rgba(228, 196, 25, 0.75);
        color: #fff;
    }


    li .CLEscore {
        width: 1.65rem;
        float: left;
        font-size: 0.4rem;
        margin-left: 1%;
        line-height: 0.8rem;
        text-align: left;
    }

    li .CLEscore em {
        font-size: 0.3rem;
        /*font-size: 0;*/
    }

    li .CLEscore span {
        padding-left: 0.1rem;
        font-size: 0.3rem;
        /*font-size: 12px;*/
    }

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

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

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

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

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

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

    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.3rem;
    }

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

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