
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { AppService } from '../../service/appHttpService';
import { Response } from '@angular/http';

@IonicPage()
@Component({
  selector: 'page-operation-list',
  templateUrl: 'operation-list.html',
})
export class OperationListPage {

  //默认第一页数据
  operations:Array<string>;
  pageNumber = 0;
  pageSize = 10;
  hasmore = true;
  //操作记录总数
  totalNum = 0;
  //加载页数据
  pageOperations:Array<string> = [];
  //临时页数据
  tempOperations:Array<string> = [];

  constructor(
    public navCtrl: NavController, 
    public navParams: NavParams,
    public appService: AppService,
  ) {
    this.initMineOperationInfo(0);
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad OperationListPage');
    //this.initMineOperationInfo();
  }

  ///操作记录  （首页）
 initMineOperationInfo(pageNumber):Array<string>{
    this.appService.ObserverHttpPost("/wisdomgroup/opertion/searchOperationInfo", { "P_pageNumber": pageNumber*this.pageSize ,"P_pageSize":this.pageSize})
      .subscribe((res: Response) => {
        let data = res.json();
        this.operations = data.list;
        this.totalNum = data.total;
        
        this.operations.forEach(element => {
          let timegap = Date.parse(new Date().toString()) - Date.parse(element["opertionTime"].toString());
          let timegapvalue = this.timeshift(timegap);
          element["timegapvalue"] = timegapvalue;
        });
        //数据不足一页的时候隐藏 ion-infinate-scroll
        if (this.operations.length < this.pageSize) {
          this.hasmore = false;
        }
        //数据第一页即为最后一页时
        if(this.operations.length == this.totalNum){
          this.hasmore = false;
        }
      }, error => {
        this.appService.alert('网络异常！');  
      }
      );
      return this.operations;
  }

  timeshift(timegap:number):string{
      let result:string;
      let nd = 1000*24*60*60;//一天的毫秒数
      let nh = 1000*60*60;//一小时的毫秒数
      let nm = 1000*60;//一分钟的毫秒数
      let ns = 1000;//一秒钟的毫秒数
     
      let day = Math.round(timegap/nd);//计算差多少天
      let hour = Math.round(timegap%nd/nh);//计算差多少小时
      let min = Math.round(timegap%nd%nh/nm);//计算差多少分钟
      let sec = Math.round(timegap%nd%nh%nm/ns);//计算差多少秒//输出结果
      console.log("时间相差："+day+"天"+hour+"小时"+min+"分钟"+sec+"秒。");

      if(day>0){
        result = day+"天前";
      }else if(hour>0){
        result = hour+"小时前";
      }else if(min>0){
        result = min+"分钟前";
      }else{
        result = sec+"几秒前"; 
      }
      //result = day+"天"+hour+"小时"+min+"分钟"+sec+"秒"+"前";
      return result;
  }
  
  //操作记录（下拉产生的数据）
  doInfinite(infiniteScroll){

    this.scrollPage(result=>{     
        this.tempOperations = result.list; 
        this.totalNum = result.total;
        if(this.tempOperations.length>0){
          //计算间隔时间
          this.tempOperations.forEach(element => {
              let timegap = Date.parse(new Date().toString()) - Date.parse(element["opertionTime"].toString());
              let timegapvalue = this.timeshift(timegap);
              element["timegapvalue"] = timegapvalue;
          });
          
          this.pageOperations = this.pageOperations.concat(this.tempOperations);
          console.log("scroll:"+JSON.stringify(this.pageOperations));
          infiniteScroll.complete();   //当数据请求完成调用
        }
        //数据少于一页时  隐藏 ion-infinate-scroll
        if (this.tempOperations.length < this.pageSize) {
            this.hasmore = false;
            infiniteScroll.enable(false);   
        } 
        //最后一页数据时   隐藏 ion-infinate-scroll
        if(this.pageOperations.length+this.operations.length == this.totalNum){
            this.hasmore = false;
            infiniteScroll.enable(false);  
        }
      });

  }

  scrollPage(callback?):Array<string>{
    
    this.appService.ObserverHttpPost("/wisdomgroup/opertion/searchOperationInfo", { "P_pageNumber": ++this.pageNumber*this.pageSize ,"P_pageSize":this.pageSize})
      .subscribe((res: Response) => {
        let data = res.json();
        
        callback(data == null ? "[]" : data); 
      }, error => {
        this.appService.alert('网络异常！');  
      }
      );
      return this.tempOperations;
    }

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

}
