import {Component, ElementRef, ViewChild} from '@angular/core';
import {IonicPage, NavController, NavParams, Slides} from 'ionic-angular';
import {LearnDetailPage} from "../learn-detail/learn-detail";
import {LearnManageService} from "../learnManage.service";
import {DatePipe} from "@angular/common";

@IonicPage()
@Component({
  selector: 'page-learn-manage',
  templateUrl: 'learn-manage.html',
})
export class LearnManagePage {

    @ViewChild(Slides) slides: Slides;
    @ViewChild('tips') tips:ElementRef;

    index = 0;
    link = false;
    tabsList = [
        {name:"全部",},
        {name:"待发布",},
        {name:"已发布",},
        {name:"已过期",}
    ];
    noList = [];    //待发布
    doList = [];    //已发布
    edList = [];   //已过期
    list = [];

    pageNumber = 1;
    pageSize = 250;
    constructor(public navCtrl: NavController, public navParams: NavParams,
                public learnManageSer:LearnManageService,public datePipe:DatePipe) {
    }

    ionViewDidLoad() {
        let itemWidth = window.screen.width /4;
        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:'0',   //全部状态
            queryTime:nowDate,
        };
        this.learnManageSer.getLearnTestList(data).subscribe(
            (res)=>{
                this.list = res.data.list;
                this.noList = res.data.list.filter((e)=>e.testResult == 1);
                this.doList = res.data.list.filter((e)=>e.testResult == 2);
                this.edList = res.data.list.filter((e)=>e.testResult == 3);
                this.link = true;
            }
        )
    }

    change(index) {

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

    //下拉刷新
    doRefresh(refresher){
        refresher.complete();
    }

    //上滑
    doInfinite(infiniteScroll){
        infiniteScroll.complete()
    }

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


}
