import {Component, ViewChild} from '@angular/core';
import {IonicPage, Navbar, NavController, NavParams, Slides} from 'ionic-angular';
import {AppService} from "../../../../service/http.service";
import {LearnManageService} from "../learnManage.service";
import {CommonService} from "../../../../provide/common.service";

@IonicPage()
@Component({
    selector: 'page-read-learn',
    templateUrl: 'read-learn.html',
})
export class ReadLearnPage {
    @ViewChild(Navbar) navbar: Navbar;
    @ViewChild(Slides) slides:Slides;

    testId;
    pageNumber = 1;
    pageSize = 100;
    index = 0;
    list = [];
    message;

    constructor(public navCtrl: NavController, public navParams: NavParams, public appService: AppService,
                public learnManSer: LearnManageService,public commonSer:CommonService) {
    }

    ionViewDidLoad() {
        this.testId = this.navParams.get("testId");
        const data = {
            testId: this.testId,
            pageNumber: this.pageNumber,
            pageSize: this.pageSize
        };
        this.learnManSer.getMarkList(data).subscribe(
            (res)=>{
                this.list = res.data;
            }
        )
        this.navbar.backButtonClick = () => {
            this.commonSer.alert("是否退出批阅，当前批阅不会保存？",()=>{
                this.navCtrl.pop()
            })
        }
    }

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

    //上一题
    prev() {
        if (this.index == 0) {
            this.commonSer.toast('已经是第一份试卷了！')
        } else {
            this.index -= 1;
            this.slides.slideTo(this.index)
        }
    }

    //下一题
    next() {
        if (this.index == this.list.length - 1) {
            this.commonSer.toast('已经是最后一份试卷了！')
        } else {
            this.index += 1;
            this.slides.slideTo(this.index)
        }
    }


    //
    select(test, boolean) {
        test.result = boolean;
    }

    //提交批阅结果
    submit(){
        if(!this.check()){
            return false;
        }
        let arr = [];
        this.list.forEach(l=>{
            if(l.unSubmit == 0){    //当前试卷均题目都批阅了
                l.vos.forEach(v=>{
                    arr.push({
                        recordId:v.recordId,
                        result:v.result,
                        answerId:v.answerId,
                        questionId:v.questionId,
                        score:v.score,
                    })
                })
            }
       });
        this.commonSer.alert(this.message,()=>{
            this.learnManSer.submitMarkResult(arr).subscribe(
                (res)=>{
                    this.commonSer.toast("批阅完成！")
                    this.navCtrl.pop();
                }
            )
        })
    }

    /**
     * unSubmit 是对每个试卷的未批阅数目，当未批阅数目大于0时，将该试卷的批阅不提交
     * @returns {boolean}
     */
    check(){
        let num = 0;
        this.list.forEach(l=>{
            l.unSubmit = 0;
            l.vos.forEach(v=>{
                if(v.result != 0 && v.result != 1){
                    l.unSubmit ++;
                    num ++;
                }
            })
        });
        if(num > 0){
            this.message = "有试卷的题目未批阅(有题目未批阅的试卷不会被提交)";
        }else{
           this.message = "确定提交当前批阅？"
        }
        return true;
    }
}
