import { Component, ViewChild } from '@angular/core';
import { IonicPage, NavController, NavParams, Slides } from 'ionic-angular';

import {  Response } from '@angular/http';

import { AppService } from '../../service/appHttpService';
import { OutGoingReportEditPage } from '../report/outGoingReportEdit/outGoingReportEdit';

import { ActivityStatisticService } from '../../service/activityStatisticService';

import { OutGoingReportViewPage } from '../report/outGoingReportView/outGoingReportView';

import { Storage } from '@ionic/storage';

declare var Swiper;
@IonicPage()
@Component({
  selector: 'page-myReport',
  templateUrl: 'myReport.html',
})


export class MyReportPage {

  swiper: any;
  allReport: Array<any> = [];
  processingReport: Array<any> = [];
  endReport: Array<any> = [];

  //ViewChild传入一个字符串contentSlides，变量contentSlides接收。其它不变  
  @ViewChild('contentSlides') contentSlides: Slides;
  menus: Array<string> = ['全部报备', '报备中', '已结束']

  constructor(
    public navCtrl: NavController,
    public appService: AppService,
    public activityStatisticService: ActivityStatisticService,
    public storage:Storage
  ) {

  }

  //初始化加载我的报备
  ngOnInit(): void {
    this.getAllMyReport();
    //this.getAllMyProcessingReport();
    //this.getAllMyEndReport();
  }

  //当页面加载的时候触发，只触发一次，当有缓存的的时候，打开页面时不在加载  
  ionViewDidLoad() {
    this.initSwiper();
  }
  //初始化swiper  
  initSwiper() {
    this.swiper = new Swiper('.pageMenuSlides .swiper-container', {
      //设置slider容器能够同时显示的slides数量(  
      slidesPreView: 3,
      //slide之间的距离（单位px）  
      spaceBetween: 0,
      //断点设定：根据屏幕宽度设置某参数为不同的值  
      breakpoints: {
        1024: {
          slidesPerView: 3,
          spaceBetween: 0
        },
        768: {
          slidesPerView: 3,
          spaceBetween: 0
        },
        640: {
          slidesPerView: 3,
          spaceBetween: 0
        },
        320: {
          slidesPerView: 3,
          spaceBetween: 0
        }
      }
    });
  }
  //选择菜单  
  selectPageMenu(index) {
    this.setStyle(index);
    //切换页面  
    this.contentSlides.slideTo(index);
  }
  //选择菜单之后设置菜单样式  
  setStyle(index) {
    //得到菜单的个数  
    var slides = document.getElementsByClassName('pageMenuSlides')[0].getElementsByClassName('swiper-slide');
    //给所有的菜单都设置上swiper-slide样式  
    if (index < slides.length) {
      for (var i = 0; i < slides.length; i++) {
        var s = slides[i];
        s.className = "swiper-slide";
      }
      //给当前菜单设置成这个样式  
      slides[index].className = "swiper-slide bottomLine";
    }
  }

  slideChanged() {
    //getActiveIndex()获得当前页面的index  
    let index = this.contentSlides.getActiveIndex();
    this.setStyle(index);
    //s切换菜单  
    this.swiper.slideTo(index, 300);

  }

  //获取所有我已结束的报备
  // getAllMyEndReport() {
  //   this.appService.ObserverHttpPost("/wisdomgroup/modules/premanager/getEndPremanagerByUser", null)
  //     .subscribe((res: Response) => {
  //       let data = res.json();
  //       this.endReport = data;
  //     }, error => {
  //       this.appService.alert('网络异常！');
  //     }
  //     );
  // }

  //获取所有我进行中的报备
  // getAllMyProcessingReport() {
  //   this.appService.ObserverHttpPost("/wisdomgroup/modules/premanager/getProcessingPremanagerByUser", null)
  //     .subscribe((res: Response) => {
  //       let data = res.json();
  //       this.processingReport = data;
  //     }, error => {
  //       this.appService.alert('网络异常！');
  //     }
  //     );
  // }

  //获取所有我的报备
  getAllMyReport() {
    this.appService.ObserverHttpPost("/wisdomgroup/modules/premanager/getPremanagerByUser", null)
      .subscribe((res: Response) => {
        let data = res.json();
        this.allReport = data;

        this.allReport.forEach(element => {       
            if (!element["isOverTimeFlag"]) { //报备中
                this.processingReport.push(element);
            }else{
                this.endReport.push(element);  //已结束
            }
        });
        }, error => {
            this.appService.alert('网络异常！');
        }
    );
  }

  //跳转到报备页面
  goReport(report){
    this.storage.set("premanager",report);
    this.navCtrl.push("OutGoingReportEditPage");
  }

  //跳转到报备查看详情页面
  goDetail(report){      
    this.navCtrl.push("OutGoingReportViewPage",{premanager:report});
  }

  goBack(){
    this.navCtrl.popToRoot();
  }

}
