<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>测试等级:</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="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: "",
            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) {
                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 (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;
            }
        },
    },

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

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

    .colorfulTable {
        /deep/ .el-table__body {
            .el-table__row:nth-child(2) {
                td:nth-child(2) {
                    background-color: #c1db69;
                }
                td:nth-child(3) {
                    background-color: #faf54a;
                }
                td:nth-child(4) {
                    background-color: #ff9d33;
                }
                td:last-child {
                    background-color: #f56c6c;
                }
            }
        }
    }

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