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";


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

    index;
    tabsList = [{name: "全部",}, {name: "未完成",}, {name: "已完成",}];
    noList = [];
    doList = [];
    list = [];  //全部测试
    link = false;   //是否请求完成

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

    ionViewDidLoad() {
        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:hh:ss')
        const data = {
            pageNumber:'1',
            pageSize:'15',
            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;
            }
        )
    }

    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.slides.slideTo(index)
        this.index = index;
    }

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

    goToResult(item) {
        this.navCtrl.push("LearningExplainPage", {
            item: item
        })

    }


}
