import {Component, ElementRef, ViewChild} from '@angular/core';
import {IonicPage, NavController, NavParams, Slides} from 'ionic-angular';
import {LearningExplainPage} from "../learning-explain/learning-explain";
import {LearnService} from "../learn.service";
import {DatePipe} from "@angular/common";
import {LearningResultPage} from "../learning-result/learning-result";


@IonicPage()
@Component({
    selector: 'page-learning-list',
    templateUrl: 'learning-list.html',
})
export class LearningListPage {
    @ViewChild(Slides) slides: Slides;
    @ViewChild('tips') tips: ElementRef;

    index = 0;
    tabsList = [{name: "全部",}, {name: "未完成",}, {name: "已完成",}];
    noList = [];
    doList = [];
    list = [];  //全部测试
    link = false;   //是否请求完成
    pageNumber = 1;   //页码
    pageSize = 25;   //每页显示条数

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

    ionViewDidEnter() {
        let itemWidth = window.screen.width / 3;
        this.tips.nativeElement.style.left = itemWidth / 2 - this.tips.nativeElement.offsetWidth / 2 + 'px';
        this.getList();
    }

    getList() {
        const nowDate = this.datePipe.transform(new Date(), 'yyyy/MM/dd HH:mm:ss')
        const data = {
            pageNumber:this.pageNumber,
            pageSize:this.pageSize,
            selectType:'1',   //全部状态
            queryTime:nowDate,
        };
        this.learnSer.getLearnTestList(data).subscribe(
            (res) => {
                if(res.data){
                    this.list = res.data.list;
                    this.noList = res.data.list.filter((e)=>e.testResult == 0);
                    this.doList = res.data.list.filter((e)=>e.testResult == 2);
                }
                this.link = true;
            }
        )
    }

    // 下拉
    doRefresh(refresher){
        this.pageNumber = 1;
        const nowDate = this.datePipe.transform(new Date(), 'yyyy/MM/dd HH:hh:ss')
        const data = {
            pageNumber:this.pageNumber,
            pageSize:this.pageSize,
            selectType:'1',   //全部状态
            queryTime:nowDate,
        };
        this.learnSer.getLearnTestList(data).subscribe(
            (res) => {
                if(res.data){
                    this.list = res.data.list;
                    this.noList = res.data.list.filter((e)=>e.testResult == 0);
                    this.doList = res.data.list.filter((e)=>e.testResult == 2);
                }
                refresher.complete()
                this.link = true;
            }
        )
    }

    //上拉
    doInfinite(infiniteScroll){
        this.pageNumber ++;
        const nowDate = this.datePipe.transform(new Date(), 'yyyy/MM/dd HH:hh:ss')
        const data = {
            pageNumber:this.pageNumber,
            pageSize:this.pageSize,
            selectType:'1',   //全部状态
            queryTime:nowDate,
        };
        this.learnSer.getLearnTestList(data).subscribe(
            (res) => {
                if(res.data){
                    this.list = this.list.concat( res.data.list );
                    this.noList = this.list.filter((e)=>e.testResult == 0);
                    this.doList = this.list.filter((e)=>e.testResult == 2);
                }
                infiniteScroll.complete();
                this.link = true;
            }
        )
    }

    change(index) {
        let itemWidth = window.screen.width / 3;
        // 自身div的一半 - 滑块的一半
        this.tips.nativeElement.style.left = itemWidth * index + itemWidth / 2 - this.tips.nativeElement.offsetWidth / 2 + 'px';
        this.index = index;
    }

    goToResult(item) {
        if(item.testResult == 0){
            this.navCtrl.push("LearningExplainPage", {
                item: item
            })
        }else{
            this.navCtrl.push("LearningResultPage", {
                testId: item.testId,
                title:item.title
            })
        }


    }


}
