<template>
	<div class="achievement-container">
		<!-- 进线率统计 -->
		<Row class-name="base-table-row">
			<BaseTable :columns="entryRateColumns" :tableName="isAllSubject ? $t('totalAnalysis.ach_title5') : $t('totalAnalysis.ach_title5_2')"
				:tableDatas="entryBarData" tableRef="entryRateTable" :isScroll="false"
				:tableTitle="isAllSubject ? $t('totalAnalysis.ach_title5') : $t('totalAnalysis.ach_title5_2')" ref="rateTable"></BaseTable>
		</Row>
		<!-- 进线情况统计 -->
		<Row class-name="base-table-row">
			<BaseTable ref="entryTable" :columns="entryNumberColumns" :tableName="$t('totalAnalysis.ach_title4')"
				:tableDatas="entryTableData" tableRef="entryTable" :pageSize="10"
				:tableTitle="$t('totalAnalysis.ach_title4')" :tips="isAllSubject ? $t('totalAnalysis.myTable.tip') : ''"></BaseTable>
		</Row>
	</div>
</template>

<script>
	import BaseTable from '@/components/student-analysis/total/BaseMyTable.vue'
	export default {
		props: {
			subjectIndex: {
				type: Number,
				default: NaN
			}
		},
		components: {
			BaseTable
		},
		data() {
			return {
				entryBarData: [],
				entryTableData: [],
				entryData: [],
				footfaultData: [],
				tipContent: '',
				classList: [],
				joinNum: 0,
				isAllSubject: true,
				curSubjectIndex: 0,
				analysisJson: '',
				originNumberColumns: [],
				originRateColumns: [],
				entryNumberColumns: [{
						title: this.$t('totalAnalysis.base_name'),
						key: 'name',
						minWidth: 100,
						fixed: 'left'
					},
					{
						title: this.$t('totalAnalysis.base_class'),
						key: 'className',
						filters: [],
						filterMultiple: false,
					},
					{
						title: this.$t('totalAnalysis.ach_table_text1'),
						key: 'gradeRank',
						renderType: 'renderRank'
					},
					{
						title: this.$t('totalAnalysis.ach_table_text2'),
						sortable: 'custom',
						key: 'score',
						renderType: 'renderEntry',
						filters: [{
								label: this.$t('totalAnalysis.ach_table_text6'),
								value: 1
							},
							{
								label: this.$t('totalAnalysis.ach_table_text7'),
								value: 2
							}
						],
						filterMultiple: false,
						// filterRemote(value, row) {}
					}
				],
				entryRateColumns: [{
						title: this.$t('totalAnalysis.ach_table_text1'),
						key: 'gradeRank',
						sortable: 'custom'
					},
					{
						title: this.$t('totalAnalysis.base_class'),
						key: 'className',
						filterMultiple: false,
					},
					{
						title: this.$t('totalAnalysis.sca_chart_text1'),
						key: 'csRate',
						sortable: 'custom',
						minWidth: 50,
						renderType:'renderCsRate',
					},
					{
						title: this.$t('totalAnalysis.ach_text7'),
						key: 'totalNum',
						sortable: true
					},
					{
						title: this.$t('totalAnalysis.ach_text8'),
						key: 'entryNum',
						sortable: true,
					},
					{
						title: this.$t('totalAnalysis.ach_text9'),
						key: 'overAverageRate',
						sortable: 'custom',
						renderType: 'renderOverRate'
					},
					{
						title: this.$t('totalAnalysis.ach_table_text3'),
						key: 'average',
						renderType: function(h, params) {
							return h('span', (Number(params.row.average)).toFixed(1))
						},
						sortable: 'custom'
					},
					{
						title: this.$t('totalAnalysis.ach_table_text4'),
						key: 'standardDeviation',
						sortable: 'custom'
					},
				]
			}
		},

		created() {
			if(this.getAnalysisJson){
				let filterArr = []
				this.getAnalysisJson.classes.forEach(i => {
					filterArr.push({
						label: i.className,
						value: i.className
					})
				})
				this.entryRateColumns[1].filters = filterArr
				this.entryNumberColumns[1].filters = filterArr
			}
			this.originNumberColumns = JSON.parse(JSON.stringify(this.entryNumberColumns))
			this.originRateColumns = JSON.parse(JSON.stringify(this.entryRateColumns))
			let scatter = this.$store.state.totalAnalysis.scatter
			this.classList = this.$store.state.totalAnalysis.classList

			this.analysisJson = this.getAnalysisJson
			console.log(this.analysisJson)
			
		},

		methods: {

			/* 添加科目的表头 */
			renderColumns(data) {
				// 渲染进线表格
				let subjectList = data.classes[0].subjects
				let subjectColumns = []
				subjectList.forEach((item, index) => {
					let subjectColumn = {
						title: item.name,
						sortable: 'custom',
						key: item.name
					}
					if (this.isAllSubject || (!this.isAllSubject && this.curSubjectIndex === index)) {
						subjectColumns.push(subjectColumn)
					}
				})
				// 清除之前的科目columns 添加当前评测的科目columns
				let fixColumns = this.isAllSubject ? 4 : 3
				if (!this.isAllSubject) {
					this.entryRateColumns = this.entryRateColumns.filter(i => i.key !== 'entryNum' && i.key !== 'overAverageRate')
					this.entryNumberColumns = this.entryNumberColumns.filter(i => i.key !== 'score')
				} else {
					this.entryRateColumns = this.originRateColumns
					this.entryNumberColumns = this.originNumberColumns
				}
				this.entryNumberColumns.splice(fixColumns, this.entryNumberColumns.length - fixColumns, ...subjectColumns)

			},

			/* 获取进线情况统计表格数据 */
			getTableDatas(analysisJson) {
				let result = []
				analysisJson.students.forEach((stu, stuIndex) => {
					result.push({
						name: stu.name,
						className: stu.className,
						gradeRank: 0,
						score: this.isAllSubject ? stu.total : stu.subjects[this.curSubjectIndex].score,
					})
					// 动态添加学生每个科目的得分
					stu.subjects.forEach((subject, subjectIndex) => {
						if (this.isAllSubject || (!this.isAllSubject && this.curSubjectIndex ===
								subjectIndex)) {
							result[stuIndex][subject.name] = subject.score
						}
					})
				})
				/* 根据表格每个学生的总分来进行排序 */
				let rateRanks = result.sort((a, b) => {
					return Number(b.score) - Number(a.score)
				})
				result.forEach(item => {
					item.gradeRank = rateRanks.map(i => i.name).indexOf(item.name) + 1
				})
				return result
			},

			/* 获取进线率统计数据 */
			getEntryBarData(analysisJson) {
				let result = []
				analysisJson.classes.forEach((classItem, classIndex) => {
					result.push({
						gradeRank: 0,
						className: classItem.className,
						entryNum: classItem.lineCount,
						totalNum: classItem.stuCount,
						csRate: this.isAllSubject ? classItem.csRate : classItem.subjects[this
							.curSubjectIndex].sRate,
						overAverageRate: classItem.stuCount > 0 ? ((classItem.lineCount / classItem
							.stuCount) * 100).toFixed(2) : 0.00,
						average: this.isAllSubject ? classItem.totalAverage.toFixed(1) : classItem
							.subjects[this.curSubjectIndex].average.toFixed(1),
						standardDeviation: this.isAllSubject ? classItem.standardDeviation.toFixed(1) :
							classItem.subjects[this.curSubjectIndex].standard.toFixed(1),
					})
				})
				/* 根据超均率 来对班级进行年级排名 */
				let rateRanks = result.sort((a, b) => {
					return Number(b.average) - Number(a.average)
				})
				result.forEach(item => {
					item.gradeRank = rateRanks.map(i => i.className).indexOf(item.className) + 1
				})
				return result
			}
		},
		mounted() {
			this.$refs.rateTable.$el.childNodes[1].style.borderRight = '0'
			this.$refs.entryTable.$el.childNodes[1].style.borderRight = '0'

			if (this.getAnalysisJson) {
				this.entryTableData = this.getTableDatas(this.getAnalysisJson)
				this.entryBarData = this.getEntryBarData(this.getAnalysisJson)

				let filterArr = []
				this.getAnalysisJson.classes.forEach(i => {
					filterArr.push({
						label: i.className,
						value: i.className
					})
				})
				this.entryRateColumns[1].filters = filterArr
				this.entryNumberColumns[1].filters = filterArr
				this.renderColumns(this.getAnalysisJson)
			}

		},
		computed: {
			getAnalysisJson() {
				return this.$store.state.totalAnalysis.analysisJson
			}
		},
		watch: {
			getAnalysisJson(val) {
				if (!val) return
				this.renderColumns(val)
				this.entryTableData = this.getTableDatas(val)
				this.entryBarData = this.getEntryBarData(val)
			},
			subjectIndex: {
				handler(n, o) {
					this.isAllSubject = isNaN(n)
					this.curSubjectIndex = n
					let val = this.$store.state.totalAnalysis.analysisJson
					this.renderColumns(val)
					this.entryTableData = this.getTableDatas(val)
					this.entryBarData = this.getEntryBarData(val)
				},
			}
		}
	}
</script>
<style src="./AchievementAnalysis.less" lang="less" scoped></style>
