<template>
	<div>
		<div class="exam-analysis-checklist">
			<p class="checklist-title">
				<span class="checklist-title-left">{{ $t('evaluation.checkList.title') }}</span> <span class="checklist-title-tip">{{ $t('evaluation.checkList.tip') }}</span> </p>
			<Table :columns="columns" :data="items"></Table>
		</div>
		<div class="exam-analysis-wrap" v-if="!hidePie">
			<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>
	</div>
</template>
<script>
	export default {
		props: {
			testPaper: {
				type: Object,
				default: () => {
					return {
						item: []
					}
				}
			},
			hidePie: {
				type: Boolean,
				default: false
			}
		},
		data(vm) {
			return {
				pointList: [],
				columns: [{
						title: vm.$t('evaluation.knowledgePoints'),
						key: 'name'
					},
					{
						title: vm.$t('evaluation.level1'),
						key: 'level1',
						render: (h, params) => {
							return h('span', params.row.level1.score + '（' + params.row.level1.count + '）')
						}
					},
					{
						title: vm.$t('evaluation.level2'),
						key: 'level2',
						render: (h, params) => {
							return h('span', params.row.level2.score + '（' + params.row.level2.count + '）')
						}
					},
					{
						title: vm.$t('evaluation.level3'),
						key: 'level3',
						render: (h, params) => {
							return h('span', params.row.level3.score + '（' + params.row.level3.count + '）')
						}
					},
					{
						title: vm.$t('evaluation.level4'),
						key: 'level4',
						render: (h, params) => {
							return h('span', params.row.level4.score + '（' + params.row.level4.count + '）')
						}
					},
					{
						title: vm.$t('evaluation.level5'),
						key: 'level5',
						render: (h, params) => {
							return h('span', params.row.level5.score + '（' + params.row.level5.count + '）')
						}
					},
					{
						title: vm.$t('evaluation.level6'),
						key: 'level6',
						render: (h, params) => {
							return h('span', params.row.level6.score + '（' + params.row.level6.count + '）')
						}
					},
					{
						title: vm.$t('evaluation.checkList.sum'),
						key: 'level6',
						render: (h, params) => {
							return h('span', {
								style:{
									fontWeight:'bold',
									color:'#37a2da'
								}
							}, this.sumScore(params.row) + '（' + this.sumCount(params.row) + '）')
						}
					}
				],
				items: []
			}
		},
		methods: {
			sumScore(row) {
				return row.level1.score + row.level2.score + row.level3.score + row.level4.score + row.level5.score + row
					.level6.score
			},
			sumCount(row) {
				return row.level1.count + row.level2.count + row.level3.count + row.level4.count + row.level5.count + row
					.level6.count
			}
		},
		mounted() {
			let items = []
			this.testPaper.item.forEach(i => {
				if (i.type !== 'compose') {
					items.push(i)
				} else {
					items.push(...i.children)
				}
			})
			if (items.length) {
				let pointList = [...new Set(items.map(i => i.knowledge).flat(1))]
				let result = []
				if (pointList.length) {
					result.push({
						name: this.$t('evaluation.checkList.none'),
						level1: {
							score: 0,
							count: 0
						},
						level2: {
							score: 0,
							count: 0
						},
						level3: {
							score: 0,
							count: 0
						},
						level4: {
							score: 0,
							count: 0
						},
						level5: {
							score: 0,
							count: 0
						},
						level6: {
							score: 0,
							count: 0
						},
					})
					pointList.forEach(i => {
						result.push({
							name: i,
							level1: {
								score: 0,
								count: 0
							},
							level2: {
								score: 0,
								count: 0
							},
							level3: {
								score: 0,
								count: 0
							},
							level4: {
								score: 0,
								count: 0
							},
							level5: {
								score: 0,
								count: 0
							},
							level6: {
								score: 0,
								count: 0
							},
						})
					})
					if (pointList.length) {
						pointList.forEach((point, pointIndex) => {
							items.forEach(item => {
								if (item.knowledge && item.knowledge.includes(point)) {
									result[pointIndex + 1]['level' + item.field].score += item.score
									result[pointIndex + 1]['level' + item.field].count++
								}
							})
						})
					}
					items.forEach(item => {
						if (item.knowledge && !item.knowledge.length) {
							result[0]['level' + item.field].score += item.score
							result[0]['level' + item.field].count++
						}
					})
					result.push({
						name: this.$t('evaluation.checkList.sum'),
						level1: {
							score: result.reduce((a, b) => a + b.level1.score, 0),
							count: result.reduce((a, b) => a + b.level1.count, 0)
						},
						level2: {
							score: result.reduce((a, b) => a + b.level2.score, 0),
							count: result.reduce((a, b) => a + b.level2.count, 0)
						},
						level3: {
							score: result.reduce((a, b) => a + b.level3.score, 0),
							count: result.reduce((a, b) => a + b.level3.count, 0)
						},
						level4: {
							score: result.reduce((a, b) => a + b.level4.score, 0),
							count: result.reduce((a, b) => a + b.level4.count, 0)
						},
						level5: {
							score: result.reduce((a, b) => a + b.level5.score, 0),
							count: result.reduce((a, b) => a + b.level5.count, 0)
						},
						level6: {
							score: result.reduce((a, b) => a + b.level6.score, 0),
							count: result.reduce((a, b) => a + b.level6.count, 0)
						}
					})
				} else {
					result.push({
						name: this.$t('evaluation.checkList.none'),
						level1: {
							score: items.filter(i => i.level === 1).reduce((a,b) => a + b.score,0),
							count: items.filter(i => i.level === 1).length
						},
						level2: {
							score: items.filter(i => i.level === 2).reduce((a,b) => a + b.score,0),
							count: items.filter(i => i.level === 2).length
						},
						level3: {
							score: items.filter(i => i.level === 3).reduce((a,b) => a + b.score,0),
							count: items.filter(i => i.level === 3).length
						},
						level4: {
							score: items.filter(i => i.level === 4).reduce((a,b) => a + b.score,0),
							count: items.filter(i => i.level === 4).length
						},
						level5: {
							score: items.filter(i => i.level === 5).reduce((a,b) => a + b.score,0),
							count: items.filter(i => i.level === 5).length
						},
						level6: {
							score: items.filter(i => i.level === 6).reduce((a,b) => a + b.score,0),
							count: items.filter(i => i.level === 6).length
						},
					})
				}

				this.items = result
			}
		},
		watch: {
			testPaper: {
				handler(n, o) {},
				deep: true
			}
		},
		create() {

		}
	}
</script>
<style lang="less" scoped>
	@import './ExamPaperAnalysis.less';
</style>
<style lang="less">
	.exam-analysis-checklist{
		.ivu-table-header thead tr th{
			padding: 15px 0 !important;
		}
	}
</style>
