import {Component, ViewChild} from '@angular/core';
import {IonicPage, Nav, Navbar, NavController, NavParams} from 'ionic-angular';
import {ReviewLearnPage} from "../review-learn/review-learn";
import {LearnService} from "../learn.service";

@IonicPage()
@Component({
    selector: 'page-learning-result',
    templateUrl: 'learning-result.html',
})
export class LearningResultPage {
    @ViewChild(Navbar) navbar:Navbar;
    testId;
    title;
    result;

    starList = [
        {class: 'star-yellow'},
        {class: 'star-yellow'},
        {class: 'star-yellow'},
    ];

    constructor(public navCtrl: NavController, public navParams: NavParams,
                public learnSer:LearnService) {
    }


    ionViewDidLoad() {
        this.testId = this.navParams.get('testId');
        this.title = this.navParams.get('title');
        const data = {
            testId:this.testId
        };
        this.learnSer.viewTestResult(data).subscribe(
            (res)=>{
                this.result = res.data;
                let c = this.result.score/this.result.fullmarks;
                if(this.result.score == 0){
                    this.starList = [{class: 'star-gray'}, {class: 'star-gray'}, {class: 'star-gray'},];
                }
                if( 0.6 > c && c > 0.3){
                    this.starList = [{class: 'star-yellow'}, {class: 'star-gray'}, {class: 'star-gray'},];
                }
                if(  0.3 > c && c > 0){
                    this.starList = [{class: 'star-yellow'}, {class: 'star-gray'}, {class: 'star-gray'},];
                }
                if( 1 > c && c > 0.6){
                    this.starList = [{class: 'star-yellow'}, {class: 'star-yellow'}, {class: 'star-gray'},];
                }
                if( this.result.score == this.result.fullmarks){
                    this.starList = [{class: 'star-yellow'}, {class: 'star-yellow'}, {class: 'star-yellow'},];
                }
            }
        )
        this.navbar.backButtonClick = (event)=>{
            let index = this.navCtrl.length() -2;
            this.navCtrl.remove(2,index)
        }
    }

    //测试回顾
    reviewTest() {
        this.navCtrl.push("ReviewLearnPage",{
            recordId:this.result.recordId,
            score:this.result.score,
            title:this.title,
        })
    }

    //重新测试
    resetTest() {
        this.navCtrl.push('LearningDoPage',{
            testId: this.testId,
            title:this.title,
            type:'reset'
        });
    }
}
