<template>
    <div class="exam-analysis-wrap">
		<div class="exam-analysis-echarts-item">
			<BaseTypePie :echartsData="testPaper"></BaseTypePie>
		</div>
		<div class="exam-analysis-echarts-item">
			<BaseObjectivePie :echartsData="testPaper"></BaseObjectivePie>
		</div>
		<div class="exam-analysis-echarts-item">
			<BaseDiffPie :echartsData="testPaper"></BaseDiffPie>
		</div>
		<div class="exam-analysis-echarts-item">
			<BasePointPie :echartsData="testPaper"></BasePointPie>
		</div>
    </div>
</template>
<script>
    export default {
        props: {
            //examAnalysisData: {
            //    type: Object,
            //    default: () => {
            //        return {
            //            whole: {},
            //            quetion: {},
            //            difficult: {},
            //            point: []
            //        }
            //    }
            //},
            testPaper: {
                type: Object,
                default: () => {
                    return {
                        item: []
                    }
                }
            }
        },
        data() {
            return {
                groupQuestion: {},
                examAnalysisData: {
                    whole: {},
                    quetion: {},
                    difficult: {},
                    point: []
                },
                myPie: undefined,
                columns1: [
                    {
                        title: '序号',
                        slot: 'index',
                        align: 'center'
                    },
                    {
                        title: '知识点',
                        key: 'point',
                        align: 'center'
                    },
                    {
                        title: '分值（占比）',
                        slot: 'score',
                        align: 'center'
                    },
                    {
                        title: '对应题号',
                        slot: 'questionIndex',
                        align: 'center'
                    }
                ],
                option: {
                    legend: {
                        orient: 'vertical', // 'vertical'
                        x: 'right', // 'center' | 'left' | {number},
                        y: '0', // 'center' | 'bottom' | {number}
                        padding: [10, 0, 10, 5], // [5, 10, 15, 20]
                        itemGap: 20,
                        textStyle: { color: '#000000' }
                    },
                    tooltip: {
                        trigger: 'item',
                        formatter: '{b} : {c} ({d}%)'
                    },

                    series: [{
                        name: '题目难度',
                        type: 'pie',
                        radius: '66',
                        selectedMode: 'single',
                        selectedOffset: 10,
                        clockwise: true,
                        center: ['45%', '50%'],
                        color: ['#10abe7', '#E8BE15', '#F19300', '#EB5E00', '#D30000'],
                        data: [
                            {
                                value: 0,
                                name: '容易'
                            }
                        ],
                        roseType: false,

                        label: {
                            show: false
                        },
                        itemStyle: {
                            borderWidth: '0',
                            color: 'pink',
                            borderColor: '#FFF',
                            normal: {
                                borderWidth: '0',
                                borderColor: 'rgba(0,0,0,2)'
                            }
                        }
                    }]
                }
            }
        },
        methods: {
            /**对试卷题型分组 */
            groupQuestionByType() {
                this.groupQuestion = {}
                let groupResult = this.$jsFn.groupBy(this.testPaper.item, 'type')
                for (let i = 0; i < groupResult.length; i++) {
                    this.$set(this.groupQuestion, groupResult[i][0].type, groupResult[i])
                }
                this.handleAnalysisData()
            },
            /**处理试卷分析数据 */
            handleAnalysisData() {
                //计算试卷整体分析数据
                let exmaData = this.getWholeAnalysis()
                this.examAnalysisData.whole = {}
                this.$set(this.examAnalysisData.whole, 'score', this.testPaper.score)
                this.examAnalysisData.whole.subjectiveScore = exmaData.subjectiveScore
                this.examAnalysisData.whole.objectiveScore = exmaData.objectiveScore
                this.examAnalysisData.whole.subjectiveNum = exmaData.subjectiveNum
                this.examAnalysisData.whole.objectiveNum = exmaData.objectiveNum
                this.examAnalysisData.whole.subjectiveScoreP = exmaData.subjectiveScoreP
                this.examAnalysisData.whole.objectiveScoreP = exmaData.objectiveScoreP
                this.examAnalysisData.whole.subjectiveNumP = exmaData.subjectiveNumP
                this.examAnalysisData.whole.objectiveNumP = exmaData.objectiveNumP
                //计算试卷题型分布数据
                this.examAnalysisData.question = this.getQuestionType()
                //计算试卷难度数据
                this.examAnalysisData.difficult = this.getQuestionDiff()
                //计算知识点分析
                this.examAnalysisData.point = this.getPointData()
            },
            /**试卷整体分析 */
            getWholeAnalysis() {
                let subjectiveScore = 0
                let objectiveScore = 0
                let subjectiveNum = 0
                let objectiveNum = 0
                let subjectiveScoreP = 0
                let objectiveScoreP = 0
                let subjectiveNumP = 0
                let objectiveNumP = 0
                for (let key in this.groupQuestion) {
                    if (key == 'complete' || key == 'subjective' || key == 'compose') {
                        subjectiveNum += this.groupQuestion[key].length
                        subjectiveScore += this.groupQuestion[key].reduce((total, item) => total + item.score, 0)
                    } else {
                        objectiveNum += this.groupQuestion[key].length
                        objectiveScore += this.groupQuestion[key].reduce((total, item) => total + item.score, 0)
                    }
                }
                subjectiveScoreP = ((subjectiveScore + objectiveScore) == 0 ? 0 : subjectiveScore * 100 / (subjectiveScore + objectiveScore)).toFixed(1)
                objectiveScoreP = (subjectiveScore + objectiveScore) == 0 ? 0 : objectiveScore == 0 ? 0 : 100 - subjectiveScoreP
                subjectiveNumP = ((subjectiveNum + objectiveNum) == 0 ? 0 : subjectiveNum * 100 / (subjectiveNum + objectiveNum)).toFixed(1)
                objectiveNumP = (subjectiveNum + objectiveNum) == 0 ? 0 : objectiveNum == 0 ? 0 : 100 - subjectiveNumP

                return { subjectiveScore, objectiveScore, subjectiveNum, objectiveNum, subjectiveScoreP, objectiveScoreP, subjectiveNumP, objectiveNumP }
            },
            /**题型分布分析 */
            getQuestionType() {
                let data = {}
                for (let key in this.groupQuestion) {
                    data[key] = {}
                    data[key]['count'] = this.groupQuestion[key].length
                    data[key]['countPercent'] = (this.groupQuestion[key].length * 100 / this.testPaper.item.length).toFixed(1)
                    data[key]['score'] = this.groupQuestion[key].reduce((total, item) => total + item.score, 0)
                    data[key]['scorePercent'] = (data[key]['score'] * 100 / this.testPaper.score).toFixed(1)
                }
                return data
            },
            /**试卷难度分析 */
            getQuestionDiff() {
                let count = []
                let score = []
                let group = this.$jsFn.groupBy(this.testPaper.item, 'level')
                for (let item of group) {
                    count.push(
                        {
                            value: item.length,
                            name: this.getDiffLabel(item[0].level)
                        }
                    )
                    score.push(
                        {
                            value: item.reduce((total, i) => total + i.score, 0),
                            name: this.getDiffLabel(item[0].level)
                        }
                    )
                }
                return { count, score }
            },
            /**
             * 获取试卷难度label
             * @param level
             */
            getDiffLabel(level) {
                switch (level) {
                    case 1:
                        return '容易'
                        break
                    case 2:
                        return '较易'
                        break
                    case 3:
                        return '一般'
                        break
                    case 4:
                        return '较难'
                        break
                    case 5:
                        return '困难'
                        break
                }
            },
            /**知识点分析 */
            getPointData() {
                let data = {}
                let str = ''
                for (let i = 0; i < this.testPaper.item.length; i++) {
                    if (this.testPaper.item[i].points.length > 0) {
                        let avgScore = parseInt((this.testPaper.item[i].score / this.testPaper.item[i].points.length).toFixed(0))
                        for (let j = 0; j < this.testPaper.item[i].points.length; j++) {
                            if (str.indexOf(this.testPaper.item[i].points[j]) == -1) {
                                str += this.testPaper.item[i].points[j]
                                data[this.testPaper.item[i].points[j]] = {
                                    questionIndex: [i + 1],
                                    score: avgScore
                                }
                            } else {
                                data[this.testPaper.item[i].points[j]].questionIndex.push(i + 1)
                                data[this.testPaper.item[i].points[j]].score += avgScore
                            }
                        }
                    }
                }
                let tableData = []
                for (let key in data) {
                    tableData.push(
                        {
                            point: key,
                            score: data[key].score,
                            questionIndex: data[key].questionIndex
                        }
                    )
                }
                return tableData
            },
            getScorePercent(score) {
                return (score * 100 / this.examAnalysisData.whole.score).toFixed(2)
            },
            drawDiffChart() {
                this.option.series[0].data = this.examAnalysisData.difficult.count
                this.myPie.setOption(this.option)
            },
            getQuestionLabel(key) {
                switch (key) {
                    case 'multiple':
                        return '多选题'
                        break
                    case 'subjective':
                        return '问答题'
                        break
                    case 'judge':
                        return '判断题'
                        break
                    case 'complete':
                        return '填空题'
                        break
                    case 'single':
                        return '单选题'
                        break
                    case 'compose':
                        return '综合题'
                        break
                }
            }
        },
        mounted() {
            // this.myPie = this.$echarts.init(document.getElementById('exam-diff-chart'))
            // this.groupQuestionByType()
        },
        watch: {
            testPaper: {
                handler(n, o) {
                    // this.groupQuestionByType()
                    // this.drawDiffChart()
                },
                deep: true
            }
        },
        create() {

        }
    }
</script>
<style lang="less" scoped>
    @import './ExamPaperAnalysis.less';
</style>
<style lang="less">
      /*.exam-analysis-wrap .ivu-table-header thead tr th {
          background: #303030;
      }

      .exam-analysis-wrap .ivu-table td {
          background: #353535;
      }*/

      /*#exam-diff-chart {
      width:400px;
      height:400px;
    }*/
</style>
