import {Component, ViewChild} from '@angular/core';
import {IonicPage, NavController, NavParams, Slides, ToastController} from 'ionic-angular';
import {AppService} from "../../../../service/http.service";
import {message} from "../../../../app/main";
import {LearnService} from "../learn.service";
import {CommonService} from "../../../../provide/common.service";


@IonicPage()
@Component({
  selector: 'page-review-learn',
  templateUrl: 'review-learn.html',
})
export class ReviewLearnPage {
    @ViewChild(Slides) slides: Slides;

    title;   //测试标题
    list = [];
    index = 0;  //当前题目的序号
    score;   //得分
    error;
    errorContent;
    constructor(public navCtrl: NavController, public navParams: NavParams,public learnSer:LearnService,
                public toastCtrl:ToastController,public appService:AppService,public commonSer:CommonService) {
    }

    ionViewDidLoad() {
        const recordId = this.navParams.get('recordId');
        this.score = this.navParams.get('score');
        this.title = this.navParams.get('title');
        const data = {
            recordId:recordId,
        }
        this.learnSer.viewAnswerDetail(data).subscribe(
            (res)=>{
                this.list = res.data;
                this.list.forEach(e=>{
                    if(e.questionType == 4){
                        e.options = [
                            {option:'A', comment:'是'},
                            {option:'B', comment:'否'},
                        ];
                        e.myAnswer = e.myAnswer == '1'?'A':'B';
                        e.correctAnswer = e.correctAnswer == '1'?'A':'B';
                    }
                    if(e.questionType == 1 || e.questionType == 2 || e.questionType == 4){
                        e.options.forEach(s=>{
                            if(e.correctAnswer.includes(s.option)){
                                s.answer = 'right';
                            }else if(!e.correctAnswer.includes(s.option) && e.myAnswer.includes(s.option)){
                                s.answer = 'error'
                            }else{
                                s.answer = 'normal'
                            }
                        })
                    }
                })

                console.log(this.list)
            }
        )
    }

    slideChanged(){
        this.index = this.slides.realIndex;
    }

    //上一题
    prev() {
        if (this.index == 0) {
            const toast = this.toastCtrl.create(message);
            toast.setMessage('已经是第一题哦');
            toast.present();
        } else {
            this.index -= 1;
            this.slides.slideTo(this.index)
        }
    }

    //下一题
    next() {
        if (this.index == this.list.length -1) {
            const toast = this.toastCtrl.create(message);
            toast.setMessage('已经是最后一题了哦');
            toast.present();
        } else {
            this.index += 1;
            this.slides.slideTo(this.index)
        }
    }

    //只看错题
    lookError(){
        this.index = 1;
        this.slides.slideTo(this.index-1)
        let arr = [];
        this.list.map(e=>{
            if(e.result == 0){
                arr.push(e);
            }
        });
        this.list = arr;
    }

    //纠错
    sure(){
        const data = {
            questionId:this.list[this.index].questionId,
            questionType:this.list[this.index].questionType,
            content:this.errorContent
        };
        this.commonSer.log(data);
        this.learnSer.questionCorrecting(data).subscribe(
            (res)=>{
                this.commonSer.toast("提交成功！");
                this.error = false;
            }
        )
    }

    cancel(){
        this.error = false;
        this.errorContent = "";
    }
}
