<template>
    <div class="board-container">
        <div class="header">
            <img src="../assets/logo.png" style="margin-left: 10px; margin-top: 10px;" >
        </div>
        <div class="title_info">
            <el-card shadow="always" style="background-color: rgb(201,235,251); border-radius: 5px;">
                <p class="item_name">{{ itemName }}</p>
                <p class="scope_cycleName">{{type}} {{ scope }} 类 {{ cycleName }} 专家评分公示</p>
            </el-card>
        </div>
        <div class="analysis_collect">
            <span class="name"><i class="el-icon-s-data"></i>评分项平均得分</span>
            <el-table :data="analysisData" border style="width: 100% ;" stripe highlight-current-row>
                <el-table-column prop="questionName" label="评分项" align="center">
                </el-table-column>
                <el-table-column prop="avgScore" label="平均得分" align="center">
                </el-table-column>
            </el-table>
        </div>
        <div class="origin_collect">
            <span class="name"><i class="el-icon-s-data"></i>专家评分值</span>
            <el-table :data="originData" border style="width: 100%" stripe highlight-current-row>
                <el-table-column prop="professorNo" label="专家编号" align="center">
                </el-table-column>
                <el-table-column prop="questionName" label="评分项" align="center">
                </el-table-column>
                <el-table-column prop="score" label="专家评分值" align="center">
                </el-table-column>
                <el-table-column prop="created" label="专家评分时间" align="center">
                </el-table-column>
            </el-table>
        </div>
        <div class="user_size">
           <span v-for="(u,index) in userSize">
                <i class="el-icon-s-custom" style="color: green; font-size: xx-large;"></i>
           </span>
        </div>
    </div>
</template>

<script>
import YPageListLayout from '@/components/custom/YPageListLayout'
import WebSocketClient from '../utils/WebSocketClient.js'

export default {
    name: 'Dashboard',
    components: {
        YPageListLayout,
    },
    data() {
        return {
            analysisData: [],
            originData: [],
            listLoading: false,
            itemName: "",
            scope: "",
            cycleName: "",
            type:null,
            messages: [],
            error: null,
            client: {
                messages:[
                    {data:"*"},
                    {userSize:0}
                ]
            },
            userSize:0

        }
    },
    watch:{
        client:{
            deep: true,
            handler(newValue,oldValue){
                let data = newValue.messages[(newValue.messages.length)-1]
                if (data != undefined){
                    if (data.data !=null){
                        this.getAnalysisList();
                        this.getOriginList();
                    }
                    if (data.userSize != undefined){
                        this.userSize = (data.userSize)-1;
                    }
                }
               
            }
        }
    },
    mounted(){
        this.init();
    },
    created() {
        this.getAnalysisList();
        this.getOriginList();
    },
    methods: {
        init(){
            if (typeof(WebSocket) === "undefined"){
                console.log("您的浏览器不支持socket")
            }else{
                // 创建 WebSocketClient 实例并连接到服务端 API 接口
                this.client = new WebSocketClient('wss://kps.scdayou.com/ws/apo/ws')
                this.client.connect()
            }
        },
        getAnalysisList() {
            const that = this
            this.$api.dashboard.getAnalysisList()
                .then((res) => {
                    let tScore = res.data[0].avgScore
                    if (tScore==null){
                        that.analysisData = null;
                    }else{
                        that.analysisData = res.data
                    }
                    setTimeout(() => {
                    }, 200)
                })
                .catch(() => {
                    that.listLoading = false
                })
        },
        getOriginList() {
            const that = this
            this.$api.dashboard.getOriginList()
                .then((res) => {
                    that.itemName = res.data[0].itemName;
                    that.scope = res.data[0].scope;
                    that.cycleName = res.data[0].cycleName;
                    that.type = res.data[0].type;
                    if (res.data[0].score ==null){
                        that.originData = null;
                    }else{
                        that.originData = res.data;
                    
                    }
                    setTimeout(() => {
                    }, 200)
                })
                .catch(() => {
                    that.listLoading = false
                })
        },
    },
    destoryed() {
    // 在组件销毁前断开 WebSocket 连接
        this.client.disconnect()
        console.log("websocket 断开")
    },
    beforeDestory(){
        this.client.disconnect()
        console.log("websocket 断开")
    }

}
</script>
<style lang="css" scoped>
.header {
    height: 80px;
    padding: 0;
    background-color:  #ffffff;
    vertical-align: middle;
    display: flex;
    position: relative;
    width: 100%;
}

.left {
    margin: 0;
    padding-left: 10px;
    width: 30%;
    height: 100%;
}

.board-container {
    /* display: flex;
    width: 100%;
    justify-content: center;
    overflow: auto; */
    top: -10;
    background-image: linear-gradient(to top, #ace0f9 0%, #ffffff 100%);
    height: 1200px;
    overflow:auto;
}

.item_name {
    font-size: xx-large;
    letter-spacing: 1px;
    font-weight: 900;
}

.scope_cycleName {
    font-size: large;
    letter-spacing: 1px;
    font-weight: bold;
    color: red;
   
}

.title_info {
    margin-top: 1%;
    width: 70%;
    margin-left: 15%;
    text-align: center;
}

.analysis_collect {
    margin-top: 2%;
    width: 70%;
    margin-left: 15%;
}

.origin_collect {
    margin-top: 2%;
    width: 70%;
    margin-bottom: 2%;
    margin-left: 15%;
}
.name{
    /* font-family: 'Courier New', Courier, monospace; */
    margin-bottom: 5px;
    letter-spacing: 1px;
    font-weight: bold;
    font-size: small;
}
.user_size{
    position:absolute;
    right:30px;
    top:1%;
    height: 40px;
}

</style>