<template>
    <div class="evaluationReportPanel">
        <h1>评价报告</h1>
        <div class="evaluationInfo">
            <div class="evaluationBox">
                <div class="box">
                    <span>算法名称:</span>
                    <b>{{ info.algorithmName }}</b>
                </div>
                <div class="box">
                    <span>算法得分:</span>
                    <b>{{ info.algorithmScore }}</b>
                </div>
                <div class="box">
                    <span class="evaluationLevelInfoBox"
                        >测试等级
                        <el-popover
                            placement="top"
                            width="600"
                            trigger="hover"
                            popper-class="elPopoverReport"
                        >
                            <tableList
                                :border="true"
                                :columns="columnsA"
                                :getDataWay="getDataWayA"
                                class="colorfulTable"
                            >
                            </tableList>
                            <i
                                class="el-icon-info cursor"
                                title=""
                                slot="reference"
                            ></i>
                        </el-popover>
                        :
                    </span>
                    <b>{{ info.evaluationLevel }}</b>
                </div>
            </div>
            <div class="desBox">
                <span>算法描述:</span>
                <b>{{ info.algorithmDescribe }}</b>
            </div>
            <div class="evaBox">
                <span>算法简评:</span>
                <b>{{ info.algorithmEvaluation }}</b>
            </div>
        </div>

        <div class="tableBox tableBoxA">
            <div class="title">测试得分表</div>
            <tableList
                :border="true"
                :columns="columns"
                :getDataWay="getDataWay"
            >
            </tableList>
        </div>

        <!-- <div class="tableBox tableBoxB">
            <div class="title">评价等级</div>
            <tableList :border="true" :columns="columnsA" :getDataWay="getDataWayA" class="colorfulTable">
            </tableList>
        </div> -->

        <div class="tableBox tableBoxC">
            <div class="title">算法测试评分细则</div>
            <tableList
                :border="true"
                :columns="columnsB"
                :getDataWay="getDataWayB"
                :span-method="arraySpanMethod"
            >
            </tableList>
        </div>

        <div class="tableBoxCTip">
            <span>注：</span>
            <b
                >场景数量=仿真异常场景个数+未参与评分场景个数+未达标场景个数+达标场景个数<br />
                仿真异常场景：在仿真过程中报aborted错误的场景，参与得分权重，测试结果均赋0分<br />
                评分失败场景：因为场景与评分规则不匹配导致程序报错无法对仿真结果进行打分的场景，参与得分权重，测试结果均赋0分<br />
                未达标场景：仿真测试未得到满分的场景<br />
                达标场景个数：仿真测试得到满分的场景</b
            >
        </div>

        <div class="tableBox tableBoxD">
            <div class="title">详情得分情况</div>
            <tableList
                :border="true"
                :columns="columnsC"
                :getDataWay="getDataWayC"
                :row-class-name="errRow"
                :span-method="arraySpanMethodA"
            >
            </tableList>
        </div>
    </div>
</template>

<script>
import tableList from "@/components/grid/TableList";

export default {
    name: "evaluationReport", // 评价报告
    components: { tableList },
    data() {
        return {
            id: "",
            projectType: "",
            evaluationLevelInfo: "",
            columns: [
                {
                    label: "测试项目",
                    prop: "projectName",
                },
                {
                    label: "场景数量",
                    prop: "sceneNum",
                },
                {
                    label: "测试权重%",
                    prop: "weight",
                },
                {
                    label: "测试得分",
                    prop: "score",
                },
                {
                    label: "得分率%",
                    prop: "scoreRatio",
                },
            ],
            pagination: {
                //分页使用
                currentPage: 1,
                pageSize: 10,
                position: "right",
                pageSizes: [10, 30, 50, 100, 200],
                layout: "sizes, total, prev, pager, next, jumper",
            },
            getDataWay: {
                //加载表格数据
                dataType: "data",
                type: "post",
                firstRequest: false,
                data: [],
                param: {},
            },
            columnsA: [
                {
                    label: "测试项目",
                    prop: "name",
                },
                {
                    label: "90<总分<100",
                    prop: "levelA",
                },
                {
                    label: "80<总分<90",
                    prop: "levelB",
                },
                {
                    label: "70<总分<80",
                    prop: "levelC",
                },
                {
                    label: "0<总分<70",
                    prop: "levelD",
                },
            ],
            getDataWayA: {
                //加载表格数据
                dataType: "data",
                type: "post",
                firstRequest: false,
                data: [
                    {
                        index: 1,
                        name: "评价等级",
                        levelA: "优秀(G)",
                        levelB: "良好(A)",
                        levelC: "一般(M)",
                        levelD: "较差(P)",
                    },
                    {
                        index: 2,
                        name: "评价等级",
                        levelA: "++++++",
                        levelB: "+++++",
                        levelC: "++++",
                        levelD: "+++",
                    },
                ],
                param: {},
            },
            columnsB: [],
            getDataWayB: {
                //加载表格数据
                dataType: "data",
                type: "post",
                data: [],
                param: {},
            },
            columnsC: [],
            getDataWayC: {
                //加载表格数据
                dataType: "data",
                type: "post",
                data: [],
                param: {},
            },
            info: {},
            sublistNameObj: [], // 存储各级指标名称对应的次数,可对应相应的rowspan
            sublistNameObjA: [],
            subIndexArr: [], // 存储各级指标名称第一次出现对应的下标,可对应相应的rowIndex
            subIndexArrA: [],
        };
    },

    computed: {},

    methods: {
        arraySpanMethod({ row, column, rowIndex, columnIndex }) {
            if (columnIndex === 0 || columnIndex === this.columnsB.length - 1) {
                if (this.subIndexArr[0].includes(rowIndex)) {
                    return [
                        this.sublistNameObj[0][row.sublistName1 + rowIndex],
                        1,
                    ];
                } else {
                    return [0, 0];
                }
            } else if (columnIndex === 1) {
                if (this.subIndexArr[1].includes(rowIndex)) {
                    return [
                        this.sublistNameObj[1][row.sublistName2 + rowIndex],
                        1,
                    ];
                } else {
                    if (row.sublistName2) {
                        return [0, 0];
                    } else {
                        return [1, 1];
                    }
                }
            } else if (columnIndex === 2) {
                if (this.subIndexArr[2].includes(rowIndex)) {
                    return [
                        this.sublistNameObj[2][row.sublistName3 + rowIndex],
                        1,
                    ];
                } else {
                    if (row.sublistName3) {
                        return [0, 0];
                    } else {
                        return [1, 1];
                    }
                }
            } else if (columnIndex === 3) {
                if (this.subIndexArr[3].includes(rowIndex)) {
                    return [
                        this.sublistNameObj[3][row.sublistName4 + rowIndex],
                        1,
                    ];
                } else {
                    if (row.sublistName4) {
                        return [0, 0];
                    } else {
                        return [1, 1];
                    }
                }
            } else if (columnIndex === 4) {
                if (this.subIndexArr[4].includes(rowIndex)) {
                    return [
                        this.sublistNameObj[4][row.sublistName5 + rowIndex],
                        1,
                    ];
                } else {
                    if (row.sublistName5) {
                        return [0, 0];
                    } else {
                        return [1, 1];
                    }
                }
            } else if (columnIndex === 5) {
                if (this.subIndexArr[5].includes(rowIndex)) {
                    return [
                        this.sublistNameObj[5][row.sublistName6 + rowIndex],
                        1,
                    ];
                } else {
                    if (row.sublistName6) {
                        return [0, 0];
                    } else {
                        return [1, 1];
                    }
                }
            }
        },
        arraySpanMethodA({ row, column, rowIndex, columnIndex }) {
            // if (columnIndex === 0 || columnIndex === this.columnsC.length - 1) {
            if (columnIndex === 0) {
                if (this.subIndexArrA[0].includes(rowIndex)) {
                    return [
                        this.sublistNameObjA[0][row.sublistName1 + rowIndex],
                        1,
                    ];
                } else {
                    return [0, 0];
                }
            } else if (columnIndex === 1) {
                if (this.subIndexArrA[1].includes(rowIndex)) {
                    return [
                        this.sublistNameObjA[1][row.sublistName2 + rowIndex],
                        1,
                    ];
                } else {
                    if (row.sublistName2) {
                        return [0, 0];
                    } else {
                        return [1, 1];
                    }
                }
            } else if (columnIndex === 2) {
                if (this.subIndexArrA[2].includes(rowIndex)) {
                    return [
                        this.sublistNameObjA[2][row.sublistName3 + rowIndex],
                        1,
                    ];
                } else {
                    if (row.sublistName3) {
                        return [0, 0];
                    } else {
                        return [1, 1];
                    }
                }
            } else if (columnIndex === 3) {
                if (this.subIndexArrA[3].includes(rowIndex)) {
                    return [
                        this.sublistNameObjA[3][row.sublistName4 + rowIndex],
                        1,
                    ];
                } else {
                    if (row.sublistName4) {
                        return [0, 0];
                    } else {
                        return [1, 1];
                    }
                }
            } else if (columnIndex === 4) {
                if (this.subIndexArrA[4].includes(rowIndex)) {
                    return [
                        this.sublistNameObjA[4][row.sublistName5 + rowIndex],
                        1,
                    ];
                } else {
                    if (row.sublistName5) {
                        return [0, 0];
                    } else {
                        return [1, 1];
                    }
                }
            } else if (columnIndex === 5) {
                // 说明只有一级指标
                if (columnIndex === this.info.length - 1) {
                    if (this.subIndexArrA[0].includes(rowIndex)) {
                        return [
                            this.sublistNameObjA[0][
                                row.sublistName1 + rowIndex
                            ],
                            1,
                        ];
                    } else {
                        return [0, 0];
                    }
                } else {
                    if (this.subIndexArrA[5].includes(rowIndex)) {
                        return [
                            this.sublistNameObjA[5][
                                row.sublistName6 + rowIndex
                            ],
                            1,
                        ];
                    } else {
                        if (row.sublistName6) {
                            return [0, 0];
                        } else {
                            return [1, 1];
                        }
                    }
                }
            } else if (columnIndex === this.columnsC.length - 1) {
                // 说明至少有二级指标
                if (this.info.maxIndex === 2) {
                    // 只到二级
                    if (this.subIndexArrA[1].includes(rowIndex)) {
                        return [
                            this.sublistNameObjA[1][
                                row.sublistName2 + rowIndex
                            ],
                            1,
                        ];
                    } else {
                        if (row.sublistName2) {
                            return [0, 0];
                        } else {
                            return [1, 1];
                        }
                    }
                } else if (this.info.maxIndex === 3) {
                    // 只到三级
                    if (this.subIndexArrA[2].includes(rowIndex)) {
                        return [
                            this.sublistNameObjA[2][
                                row.sublistName3 + rowIndex
                            ],
                            1,
                        ];
                    } else {
                        if (row.sublistName3) {
                            return [0, 0];
                        } else {
                            return [1, 1];
                        }
                    }
                } else if (this.info.maxIndex === 4) {
                    // 只到四级
                    if (this.subIndexArrA[3].includes(rowIndex)) {
                        return [
                            this.sublistNameObjA[3][
                                row.sublistName4 + rowIndex
                            ],
                            1,
                        ];
                    } else {
                        if (row.sublistName4) {
                            return [0, 0];
                        } else {
                            return [1, 1];
                        }
                    }
                } else if (this.info.maxIndex === 5) {
                    // 只到五级
                    if (this.subIndexArrA[4].includes(rowIndex)) {
                        return [
                            this.sublistNameObjA[4][
                                row.sublistName5 + rowIndex
                            ],
                            1,
                        ];
                    } else {
                        if (row.sublistName5) {
                            return [0, 0];
                        } else {
                            return [1, 1];
                        }
                    }
                } else if (this.info.maxIndex === 6) {
                    // 到六级
                    if (this.subIndexArrA[5].includes(rowIndex)) {
                        return [
                            this.sublistNameObjA[5][
                                row.sublistName6 + rowIndex
                            ],
                            1,
                        ];
                    } else {
                        if (row.sublistName6) {
                            return [0, 0];
                        } else {
                            return [1, 1];
                        }
                    }
                }
            }
        },
        errRow({ row, rowIndex }) {
            if (row.runState === "Failed") {
                return "errColor";
            }
        },
        // 处理数据，通关于配合表格进行合并单元行
        dataHandle(data, type) {
            let arr = data;

            let lastRowSublistName1 = "";
            let lastRowSublistName2 = "";
            let lastRowSublistName3 = "";
            let lastRowSublistName4 = "";
            let lastRowSublistName5 = "";
            let lastRowSublistName6 = "";

            let sublistNameObj1 = {};
            let sublistNameObj2 = {};
            let sublistNameObj3 = {};
            let sublistNameObj4 = {};
            let sublistNameObj5 = {};
            let sublistNameObj6 = {};

            let subIndexArr1 = [];
            let subIndexArr2 = [];
            let subIndexArr3 = [];
            let subIndexArr4 = [];
            let subIndexArr5 = [];
            let subIndexArr6 = [];

            let lastI1 = 0;
            let lastI2 = 0;
            let lastI3 = 0;
            let lastI4 = 0;
            let lastI5 = 0;
            let lastI6 = 0;

            arr.forEach((item, i) => {
                let sublistName1 = item.sublistName1;
                if (sublistName1 === lastRowSublistName1) {
                    sublistNameObj1[sublistName1 + lastI1]++;
                } else {
                    lastRowSublistName1 = sublistName1;
                    subIndexArr1.push(i);
                    lastI1 = i;
                    sublistNameObj1[sublistName1 + lastI1] = 1;
                }

                if (item.sublistName2) {
                    // 一级后的指标名称可能不存在
                    let sublistName2 = item.sublistName2;
                    if (sublistName2 === lastRowSublistName2) {
                        sublistNameObj2[sublistName2 + lastI2]++;
                    } else {
                        lastRowSublistName2 = sublistName2;
                        subIndexArr2.push(i);
                        lastI2 = i;
                        sublistNameObj2[sublistName2 + lastI2] = 1;
                    }
                } else {
                    lastRowSublistName2 = "";
                }

                if (item.sublistName3) {
                    let sublistName3 = item.sublistName3;
                    if (sublistName3 === lastRowSublistName3) {
                        sublistNameObj3[sublistName3 + lastI3]++;
                    } else {
                        lastRowSublistName3 = sublistName3;
                        subIndexArr3.push(i);
                        lastI3 = i;
                        sublistNameObj3[sublistName3 + lastI3] = 1;
                    }
                } else {
                    lastRowSublistName3 = "";
                }

                if (item.sublistName4) {
                    let sublistName4 = item.sublistName4;
                    if (sublistName4 === lastRowSublistName4) {
                        sublistNameObj4[sublistName4 + lastI4]++;
                    } else {
                        lastRowSublistName4 = sublistName4;
                        subIndexArr4.push(i);
                        lastI4 = i;
                        sublistNameObj4[sublistName4 + lastI4] = 1;
                    }
                } else {
                    lastRowSublistName4 = "";
                }

                if (item.sublistName5) {
                    let sublistName5 = item.sublistName5;
                    if (sublistName5 === lastRowSublistName5) {
                        sublistNameObj5[sublistName5 + lastI5]++;
                    } else {
                        lastRowSublistName5 = sublistName5;
                        subIndexArr5.push(i);
                        lastI5 = i;
                        sublistNameObj5[sublistName5 + lastI5] = 1;
                    }
                } else {
                    lastRowSublistName5 = "";
                }

                if (item.sublistName6) {
                    let sublistName6 = item.sublistName6;
                    if (sublistName6 === lastRowSublistName6) {
                        sublistNameObj6[sublistName6 + lastI6]++;
                    } else {
                        lastRowSublistName6 = sublistName6;
                        subIndexArr6.push(i);
                        lastI6 = i;
                        sublistNameObj6[sublistName6 + lastI6] = 1;
                    }
                } else {
                    lastRowSublistName6 = "";
                }
            });

            let arr1 = [
                subIndexArr1,
                subIndexArr2,
                subIndexArr3,
                subIndexArr4,
                subIndexArr5,
                subIndexArr6,
            ];

            let arr2 = [
                sublistNameObj1,
                sublistNameObj2,
                sublistNameObj3,
                sublistNameObj4,
                sublistNameObj5,
                sublistNameObj6,
            ];

            if (type === 1) {
                this.subIndexArr = arr1;
                this.sublistNameObj = arr2;
            } else if (type === 2) {
                this.subIndexArrA = arr1;
                this.sublistNameObjA = arr2;
            }
        },
        // 处理测试等级展示
        evaluationLevelInfoShow(evaluationLevel) {
            switch (evaluationLevel) {
                case "G":
                    this.evaluationLevelInfo = "优秀(G)";
                    break;
                case "A":
                    this.evaluationLevelInfo = "良好(A)";
                    break;
                case "M":
                    this.evaluationLevelInfo = "一般(M)";
                    break;
                case "P":
                    this.evaluationLevelInfo = "较差(P)";
                    break;
                default:
                    break;
            }
        },
    },

    mounted() {
        if (this.$route.query.id) {
            this.id = this.$route.query.id;
            this.projectType = this.$route.query.projectType || "1";

            if (this.id) {
                this.$axios({
                    method: "post",
                    url: this.$api.workManagement.selectProjectReportById,
                    data: {
                        id: this.id,
                        projectType: this.projectType,
                    },
                }).then((res) => {
                    if (res.code == 200 && res.info) {
                        this.info = res.info;
                        // this.evaluationLevelInfoShow(res.info.evaluationLevel);
                        this.getDataWay.data = res.info.algorithmScoreList;

                        this.columnsB = res.info.subListScoreLiTitle;
                        this.dataHandle(res.info.subListScoreLi, 1);
                        this.getDataWayB.data = res.info.subListScoreLi;

                        this.columnsC = res.info.sceneScoreLiTitle;
                        this.dataHandle(res.info.sceneScoreLi, 2);
                        this.getDataWayC.data = res.info.sceneScoreLi;
                        console.log(this.getDataWayC.data);
                    } else {
                        this.$message.error(res.message || "获取信息失败");
                    }
                });
            }
        }
    },
};
</script>

<style lang='less' scoped>
.evaluationReportPanel {
    min-width: 900px;
    width: 60%;
    margin: 0 auto;

    h1 {
        padding: 40px 0 60px;
        font-size: 36px;
        color: @themeColor;
        text-align: center;
    }

    .evaluationInfo {
        font-size: 16px;

        > span,
        b {
            display: block;
            font-weight: normal;
        }

        span {
            width: 80px;
            color: @gray;

            &.evaluationLevelInfoBox {
                width: 111px;

                > span {
                    padding-right: 0;
                }
            }
        }

        b {
            flex: 1;
        }
    }

    .evaluationBox {
        display: flex;
        justify-content: space-between;

        .box {
            display: flex;
            width: 30%;

            span {
                padding-right: 10px;
                // color: @color;
            }

            b {
                padding-bottom: 3px;
                border-bottom: 1px solid @gray;
                text-align: center;
            }
        }
    }

    .desBox,
    .evaBox {
        display: flex;

        b {
            padding: 12px;
            border: 1px solid @gray;
        }
    }

    .desBox {
        padding: 40px 0;
    }

    .tableBox {
        padding: 60px 0;

        .title {
            padding-bottom: 20px;
            font-size: 18px;
            text-align: center;
            font-weight: bold;
        }
    }

    .tableBoxB,
    .tableBoxC,
    .tableBoxD {
        padding: 0 0 60px;
    }

    .tableBoxC {
        padding-bottom: 30px;
    }

    .tableBoxCTip {
        display: flex;
        padding-bottom: 60px;

        > span,
        b {
            display: block;
            font-weight: normal;
        }
    }

    /deep/ .el-table .errColor td {
        color: red;
    }
}
</style>