<template>
    <div style="margin-right: 20px;width: 100%">
        <div v-if="status == 'select'">
            <el-row class="row-type">

                <div style="float: right;">
                    <el-button type="primary" :disabled="multipleSelection.length < 1" class="opration-btn"
                        @click="handlerTotal">查看汇总报表</el-button>
                </div>
            </el-row>
            <el-row class="row-table">
                <el-table class="default-table" :data="$props.list">
                    <el-table-column width="48">
                        <template slot="header">
                            <el-checkbox v-model="checkedAll" @change="_checkAll"></el-checkbox>
                        </template>
                        <template slot-scope="scope">
                            <el-checkbox v-model="multipleSelection" :label="scope.row.id"
                                :disabled="_checkboxDisabled(scope.row)"><span></span></el-checkbox>
                        </template>
                    </el-table-column>
                    <el-table-column prop="subjectName" label="科目"></el-table-column>
                    <el-table-column prop="className" label="班级"></el-table-column>
                    <el-table-column prop="title" label="试卷名称"></el-table-column>
                    <el-table-column prop="examPaperScore" label="卷面分"></el-table-column>
                    <el-table-column label="测验人数/班级人数">
                        <template slot-scope="scoped">
                            {{ `${scoped.row.answeredNum}/${scoped.row.classPersonNum}` }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="examStartTime" label="测验时长"></el-table-column>
                </el-table>
            </el-row>
        </div>
        <div v-if="status == 'selected'">
            <el-container style="height: 100%;width: 100%;">
                <el-main id="print-content">
                    <el-row class="row-type">
                        <div style="float: right;">
                            <el-button type="primary" @click="_import" class="opration-btn"
                                icon="el-icon-upload2">导出报表</el-button>
                            <el-button type="primary" @click="_print" class="opration-btn"
                                icon="el-icon-printer">打印报表</el-button>
                        </div>
                    </el-row>
                    <el-row class="row-table">
                        <el-table :maxHeight="660" :data="selectedList" style="width: 100%" class="default-table">
                            <el-table-column prop="studentCode" label="学号" fixed></el-table-column>

                            <el-table-column prop="studentName" label="姓名" fixed>
                                <template slot-scope="scoped"><span class="click-b" @click="toPortrait(scoped.row)">
                                        {{ scoped.row.studentName }}
                                    </span></template>
                            </el-table-column>
                            <el-table-column v-for="(item, index) in answerList" :key="index" :label="item">
                                <el-table-column :prop="'examCount' + item" label="测练数"
                                    :class-name="index % 2 == 0 ? 'bg' : ''">
                                    <template slot-scope="scoped">{{
            scoped.row["examCount" + item] ||
                Number(scoped.row["examCount" + item]) === 0
                ? scoped.row["examCount" + item]
                : "-"
        }}</template>
                                </el-table-column>
                                <el-table-column :prop="'participationCount' + item" label="参与数"
                                    :class-name="index % 2 == 0 ? 'bg' : ''">
                                    <template slot-scope="scoped">{{
            scoped.row["participationCount" + item] ||
                Number(scoped.row["participationCount" + item]) === 0
                ? scoped.row["participationCount" + item]
                : "-"
        }}</template>
                                </el-table-column>
                                <el-table-column :prop="'score' + item" label="总分"
                                    :class-name="index % 2 == 0 ? 'bg' : ''">
                                    <template slot-scope="scoped">{{
            scoped.row["score" + item] ||
                Number(scoped.row["score" + item]) === 0
                ? scoped.row["score" + item]
                : "-"
        }}</template>
                                </el-table-column>
                                <el-table-column :prop="'classRank' + item" label="班名"
                                    :class-name="index % 2 == 0 ? 'bg' : ''">
                                    <template slot-scope="scoped">{{
            scoped.row["classRank" + item] ||
                                        Number(scoped.row["classRank" + item]) === 0
                                        ? scoped.row["classRank" + item]
                                        : "-"
                                        }}</template>
                                </el-table-column>
                            </el-table-column>
                            <el-table-column label="查看雷达图">
                                <template slot-scope="scoped">
                                    <el-button type="text" @click="openRandarChart(scoped.row)">查看</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                    </el-row>
                </el-main>
                <el-footer>
                    <div style="float: right;margin-top: 10px;">
                        <el-button @click="_backa">返回</el-button>
                    </div>
                </el-footer>
            </el-container>

        </div>
        <el-dialog class="chart-dia" :visible.sync="radarChart.visible" :title="radarChart.title" width="800"
            :append-to-body="true">
            <div class="chart-box">
                <RadarChart id="askRadarChart" :params="radarChart" :tooltipFormatter="true" />
            </div>
        </el-dialog>
    </div>
</template>
<script>
import { formatDate } from "utils";
import { downloadFile, tablePrint } from "@/utils";
import RadarChart from "@/components/charts/radarChart";
export default {
    name: "askbzrMutli",
    components: {
        RadarChart
    },
    props: {
        askReportIds: Array,
        queryParams: Object,
        list: Array
    },

    watch: {
        currentType: {
            handler: async function (val) {

            },
            deep: false,
        },
        $props: {
            handler: async function (val) {

            },
            deep: false,
        }
    },
    data() {
        return {
            selectedList: [],
            status: "select",
            checkedAll: 0,
            answerList: [],
            multipleSelection: [],
            multipleSelectionObj: {},
            radarChart: {
                title: "",
                indicator: [
                    {
                        name: "",
                        max: 100,
                        axisLabel: {
                            show: true,
                            showMaxLabel: true,
                            formatter: "{value}%",
                        },
                    },
                ],
                seriesData: []
            },
            //题干数据对象
            stem: {
                visible: false,
                src: null
            }
        };
    },
    async created() {
    },
    methods: {
        _print() {


            tablePrint("print-content", this.currentType);
        },
        _checkAll() { },
        _checkboxDisabled(obj) {
            if (obj.examStartTime) {
                return false;
            } else {
                return true;
            }
        },
        async refresh() {

        },
        async handlerTotal() {
            if (this.multipleSelection.length == 0) {
                this.$message.warning("请选择试卷！");
                return;
            }
            this.status = 'selected'
            await this._loadData();
        },
        setSubPro(type) {

            let tit;
            switch (type) {
                case 2:
                    tit = "单选题";
                    break;
                case 3:
                    tit = "多选题";
                    break;
                case 4:
                    tit = "判断题";
                    break;
                case 5:
                    tit = "主观题";
                    break;
                default:
                    tit = "其他";
            }
            return tit;
        },
        setDuration(times) {
            let m = parseInt(times / 1000 / 60);
            let s = parseInt((times / 1000) % 60);
            let ms = times;
            let aTime;
            if (times == 0) {
                aTime = `0`;
            } else {
                if (m == 0 && s == 0) {
                    aTime = `${ms}毫秒`;
                } else if (m == 0 && s != 0) {
                    aTime = `${s}秒`;
                } else if (m != 0 && s != 0) {
                    aTime = `${m}分${s}秒`;
                }
            }
            return aTime;
        },
        _backa() {
            this.status = 'select'
            this.answerList = [];
            this.exportStudent = [];
        },
        openRandarChart(obj) {
            this.radarChart = {
                indicator: [
                    {
                        name: "",
                        max: 100,
                        axisLabel: {
                            show: true,
                            showMaxLabel: true,
                            formatter: "{value}%",
                        },
                    },
                ],
                seriesData: [],
            };
            this.radarChart.title = obj.studentName + "-多科-多课时作答表现图";
            let dataList = obj.dataList.slice(1, obj.dataList.length);
            let subjectList = dataList.map((item) => item.subjectName);
            subjectList.map((item, index) => {
                if (index < 1) {
                    this.radarChart.indicator[index].name = item;
                } else {
                    this.radarChart.indicator.push({ name: item, max: 100 });
                }
            });
            // 为了美观
            if (this.radarChart.indicator.length < 3) {
                let num = this.radarChart.indicator.length;
                for (let i = 0; i < 6; i++) {
                    if (i >= num) {
                        this.radarChart.indicator.push({ name: "", max: 100 });
                    }
                }
            }
            let participationRate = dataList.map((item) => item.participationRate);
            let correctRate = dataList.map((item) => item.correctRate);
            this.radarChart.seriesData = [
                {
                    value: participationRate,
                    name: "参与度",
                },
                {
                    value: correctRate,
                    name: "正确率",
                },
            ];

            this.radarChart.visible = true;
        },
        async _loadData() {
            var classIds = [];
            var subjects = [];
            var ids = [];

            this.$props.list?.map((item) => {
                if (this.multipleSelection.includes(item.id)) {
                    subjects.push(item.subjectName);
                    classIds.push(item.classId);
                }
            });

            ids = [...this.multipleSelection];

            const { data, status, info } = await this.$request.cTPhaseExamReport({
                classIds: classIds,
                examIds: ids,
                subjectNames: subjects
            });

            if (status != 0) {
                this.$message.error(info);
                return;
            }

            let subjectName = [];

            this.selectedList = data?.list.map((item) => {
                let params = {};
                item.dataList.map((items, index) => {
                    if (!subjectName.includes(items.subjectName)) {
                        subjectName.push(items.subjectName);
                    }
                    params["examCount" + items.subjectName] = items.examCount;
                    params["participationCount" + items.subjectName] =
                        items.participationCount;
                    params["score" + items.subjectName] = items.score;
                    params["classRank" + items.subjectName] = items.classRank;
                });
                return {
                    ...item,
                    ...params,
                };
            });

            this.answerList = [...subjectName];

            this.exportStudent = [...this.selectedList];
        }

    }
};
</script>
<style scoped lang="scss">
.chart-dia {
    .chart-box {
        width: 100%;
        height: 300px;
    }

    :deep(.el-dialog__body) {
        padding: 0 0 20px 0;
    }
}

.opration-btn {
    margin-right: 10px;
}

.opration-select {
    margin-left: 10px;
}

.row-line {
    width: calc(20% - 2px);
    border: 1px solid #ebeef5;
    background: #f5f7fa;
    display: inline-block;
    height: 40px;
    line-height: 40px;

    .line-subfix {
        margin-left: 10px;

    }
}

.row-table {
    margin-top: 20px;
}
</style>
