<template>
	<div class="auto-create-container">
		<p class="auto-create-title">{{$t('evaluation.createPaper.setAutoConditions')}}</p>
		<div class="auto-filter-wrap">
			<div class="filter-wrap-item" v-if="!isSchoolPaper">
				<span class="filter-title">{{$t('evaluation.createPaper.origin')}} : </span>
				<div class="filter-content">
					<CheckboxGroup v-model="filterOrigin">
						<Checkbox label="private" v-if="!isSchoolPaper" :disabled="filterOrigin.length === 1 && filterOrigin[0] === 'private'">{{$t('evaluation.filter.privateBank')}}</Checkbox>
						<Checkbox label="school" :disabled="filterOrigin.length === 1 && filterOrigin[0] === 'school'" v-if="hasSchool">{{$t('evaluation.filter.schoolBank')}}</Checkbox>
					</CheckboxGroup>
				</div>
			</div>
			<div class="filter-wrap-item" v-if="includeSchool && filterOrigin.length > 1">
				<span class="filter-title">{{$t('evaluation.createPaper.schoolRate')}} : </span>
				<div class="filter-content">
					<InputNumber :max="100" :min="0" :step="10" v-model="schoolRate"></InputNumber>
					<span style="margin-left: 10px;min-width: 300px;color: #d1d1d1;"> % ( {{$t('evaluation.createPaper.rateTip')}} )</span>
				</div>
			</div>
			<div class="filter-wrap-item" v-if="includeSchool && !isSchoolPaper">
				<span class="filter-title">{{$t('evaluation.newExercise.choosePeriod')}} : </span>
				<div class="filter-content">
					<Select v-model="periodCode" @on-change="onPeriodChange">
						<Option v-for="item in periodList" :value="item.id" :key="item.id">{{ item.name }}</Option>
					</Select>
				</div>
			</div>
			<div class="filter-wrap-item" v-if="includeSchool && !isSchoolPaper">
				<span class="filter-title">{{$t('evaluation.newExercise.chooseSubject')}} : </span>
				<div class="filter-content">
					<Select v-model="subjectCode">
						<Option v-for="item in subjectList" :value="item.id" :key="item.id">{{ item.name }}</Option>
					</Select>
				</div>
			</div>
			<div class="filter-wrap-item" v-for="(item,index) in quInfos" :key="index">
				<span class="filter-title">{{ item.label }} ：</span>
				<div class="filter-content">
					<span class="filter-content-text">{{$t('evaluation.filter.diff')}} </span>
					<Select v-model="item.policy">
						<Option v-for="item in diffList" :value="item.level" :key="item.level">{{ item.label }}</Option>
					</Select>
					<span class="filter-content-text">{{$t('evaluation.createPaper.total')}} </span>
					<InputNumber :max="10" :min="0" v-model="item.count"></InputNumber>
					<span class="filter-content-text"> {{$t('evaluation.createPaper.nums')}} </span>
				</div>
			</div>
			<div class="filter-wrap-item">
				<span class="filter-title">{{$t('evaluation.newExercise.knowledge')}} : </span>
				<div class="filter-content">
					<Tag v-for="(item,index) in relatePoints" color="success" :key="index" :name="item" closable @on-close="onClosePoint(index)">{{ item }}</Tag>
					<Icon type="md-add-circle" @click="onSelectPoint" size="18" style="cursor: pointer;"/>
				</div>
			</div>
		</div>
		<div class="auto-btn-wrap">
			<Button type="info" :loading="isLoading" @click="doAutoCreate">{{$t('evaluation.createPaper.doAutoCreate')}}</Button>
		</div>
		
		<Modal v-model="selectPointsModal" :title="$t('evaluation.newExercise.choosePoint')" ref="pointRef" width="600px" class="related-point-modal" style="z-index: 99999">
			<BasePoints v-if="selectPointsModal"  :period="period" :subject="subject"
			 @onCheckChange="onCheckChange" :points="relatePoints" :scope="curScope"></BasePoints>
			<div slot="footer">
				<Button type="text" @click="selectPointsModal = false">{{$t('evaluation.cancel')}}</Button>
				<Button type="primary" @click="selectPointsModal = false">{{$t('evaluation.confirm')}}</Button>
			</div>
		</Modal>
	</div>
</template>

<script>
	export default {
		name: 'AutoCreate',
		props: {
			subject:{
				type:String,
				default:''
			},
			period:{
				type:String,
				default:''
			}
		},
		data() {
			return {
				isLoading: false,
				selectPointsModal:false,
				relatePoints:[],
				filterOrigin: ['private'],
				periodList: [],
				periodCode: '',
				curScope:this.$route.name === 'newPrivatePaper' ? 'private' : 'school',
				subjectList: [],
				subjectCode: '',
				schoolRate: 50,
				quInfos: [{
					type: 'single',
					label: this.$t('evaluation.single'),
					count: 0,
					policy: 'random'
				}, {
					type: 'multiple',
					label: this.$t('evaluation.multiple'),
					count: 0,
					policy: 'random'
				}, {
					type: 'judge',
					label: this.$t('evaluation.judge'),
					count: 0,
					policy: 'random'
				}, {
					type: 'complete',
					label: this.$t('evaluation.complete'),
					count: 0,
					policy: 'random'
				}, {
					type: 'subjective',
					label: this.$t('evaluation.subjective'),
					count: 0,
					policy: 'random'
				},
				{
					type: 'connector',
					label: this.$t('evaluation.connector'),
					count: 0,
					policy: 'random'
				},
				{
					type: 'correct',
					label: this.$t('evaluation.correct'),
					count: 0,
					policy: 'random'
				},{
					type: 'compose',
					label: this.$t('evaluation.compose'),
					count: 0,
					policy: 'random'
				}, ],
				diffList: [{
						level: 'random',
						label: this.$t('evaluation.createPaper.random'),
						value: -1
					},
					{
						level: 'average',
						label: this.$t('evaluation.createPaper.average'),
						value: 0
					},
					{
						level: 'relativeEasy',
						label: this.$t('evaluation.diff1'),
						value: 1
					},
					{
						level: 'easy',
						label: this.$t('evaluation.diff2'),
						value: 2
					},
					{
						level: 'common',
						label: this.$t('evaluation.diff3'),
						value: 3
					},
					{
						level: 'difficulty',
						label: this.$t('evaluation.diff4'),
						value: 4
					},
					{
						level: 'relativeDifficulty',
						label: this.$t('evaluation.diff5'),
						value: 5
					}
				]
			}
		},

		methods: {

			/** 获取当前学校基础数据 */
			getSchoolBaseInfo() {
				return new Promise((r, j) => {
					this.$store.dispatch("user/getSchoolProfile").then((res) => {
						let schoolBaseInfo = res.school_base;
						if (schoolBaseInfo) {
							r(schoolBaseInfo)
						} else {
							r(null)
						}
					});
				})
			},
			
			/* 学段切换 */
			onPeriodChange(val){
				let curPeriod = this.periodList.filter(i => i.id === val)[0]
				this.subjectList = curPeriod.subjects
				this.subjectCode = this.subjectList.length ? this.subjectList[0].id : ''
			},

			/* 开始组题 */
			doAutoCreate() {
				if(this.quInfos.map(i => i.count).filter(j => j === null).length){
					this.$Message.warning(this.$t('evaluation.createPaper.noItemTip'))
					return
				}
				this.isLoading = true
				let params = []
				let copyInfos = JSON.parse(JSON.stringify(this.quInfos))
				let copyInfos2 = JSON.parse(JSON.stringify(this.quInfos))
				this.filterOrigin.forEach(scope => {
					if (scope === 'private') {
						params.push({
							code: 'Item-' + this.$store.state.userInfo.TEAMModelId,
							scope: scope,
							period: '',
							subject:'',
							points:this.relatePoints,
							quInfos: this.filterOrigin.length === 1 ? copyInfos : copyInfos.map(i => {
								i.count = (i.count - Math.round(i.count * this.schoolRate * 0.01))
								return i
							})
						})
					} else {
						params.push({
							code: 'Item-' + this.$store.state.userInfo.schoolCode,
							scope: scope,
							period: this.periodCode,
							subject:this.subjectCode,
							points: this.relatePoints,
							quInfos: this.filterOrigin.length === 1 ? copyInfos2 : copyInfos2.map((i, index) => {
								i.count = Math.round(i.count * this.schoolRate * 0.01)
								return i
							})
						})
					}
				})
				// 访问API获取随机题目
				this.$api.learnActivity.Automatic(params).then(
					res => {
						if (res.length > 0) {
							let result = []
							res.forEach(i => {
								result = result.concat(i.item)
							})
							console.log('组题成功', result)
							this.$emit('autoQuestions', result)
							this.isLoading = false
						} else {
							this.$Message.error(this.$t('evaluation.createPaper.noResultTip'))
							setTimeout(() => {
								this.isLoading = false
							}, 1000)
						}
					},
					err => {
						this.$Message.error('API ERROR!')
						setTimeout(() => {
							this.isLoading = false
						}, 1000)
					}
				)

				console.log(params)
			},
			
			onClosePoint(index){
				this.relatePoints.splice(index,1)
			},
			
			onSelectPoint(){
				this.selectPointsModal = true
			},
			
			/* 知识点勾选变动事件 */
			onCheckChange(val, list) {
				this.relatePoints = val;
			},
		},
		
		curScope() {
			return this.$route.name === 'newPrivatePaper' ? 'private' : 'school'
		},
		mounted() {
			if (this.isSchoolPaper) {
				this.filterOrigin = ['school']
				this.schoolRate = 100
			}
			
			this.getSchoolBaseInfo().then(res => {
				if (!res) return
				if (res.period.length) {
					this.periodList = res.period
					this.periodCode = this.period || res.period[0].id
					this.subjectList = res.period.filter(i => i.id === this.periodCode)[0].subjects
					this.subjectCode = this.subject ? this.subject : (this.subjectList.length ? this.subjectList[0].id : '')
				}
			})
		},
		computed: {
			includeSchool() {
				return this.filterOrigin.indexOf('school') > -1
			},
			isSchoolPaper() {
				return this.$route.name === 'newSchoolPaper'
			},
			hasSchool() {
				return this.$store.state.userInfo.hasSchool;
			},
		},
		watch:{
			period:{
				handler(n){
					console.log('自动组题学段',n)
					this.periodCode = n
					this.subjectList = this.periodList.filter(i => i.id === n)[0].subjects
				}
			},
			subject:{
				handler(n){
					console.log('自动组题科目',n)
					this.subjectCode = n
				}
			}
		}
	}
</script>

<style lang="less">
	@import "./AutoCreateNew.less";
</style>
