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

@IonicPage()
@Component({
  selector: 'page-outGoingReport',
  templateUrl: 'outGoingReport.html'
})

export class OutGoingReportPage {

  PremanagerList: Array<String> = [];
  type: number;

  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public appService: AppService,
    public storage: Storage
  ) {
    this.type = this.navParams.get("type");
  }

  //刷新
  doRefresh(refresher) {
    console.log('Begin async operation', refresher);
    setTimeout(() => {
      if (this.type == 1) {
        this.initPremanagerList();
      } else if (this.type == 2) {
        this.initPremanagerListAboutNot();
      }
      console.log('Async operation has ended');
      refresher.complete();
    }, 2000);
  }

  
  //报备
  goReport(item) {

    this.storage.remove("temp_userpre");
    this.storage.remove("managerId");
    this.storage.remove("city");
    this.storage.remove("cityList");

    this.storage.set('premanager', item);
    this.navCtrl.push('OutGoingReportEditPage', { premanager: item });
  }
  //修改报备
  // editReport(item){
  //   this.navCtrl.push('OutGoingReportEditPage');
  // }

  ionViewDidLoad() {
    if (this.type == 1) {
      this.initPremanagerList();
      this.addRecodings();
    } else if (this.type == 2) {
      this.initPremanagerListAboutNot();
    }
  }
  //初始化列表,全部报备
  initPremanagerList(): void {
    this.appService.ObserverHttpGet("/wisdomgroup/modules/premanager/findNeedPremanager", null)
      .subscribe((res: Response) => {
        let data = res.json();
        console.log(data);
        this.PremanagerList = data;
      }, error => {
        this.appService.alert('网络异常！');
      }
      );
  }

  //初始化列表，尚未报备
  initPremanagerListAboutNot(): void {
    this.appService.ObserverHttpGet("/wisdomgroup/modules/premanager/findNeedPremanagerAboutNot", null)
      .subscribe((res: Response) => {
        let data = res.json();
        this.PremanagerList = data;
      }, error => {
        this.appService.alert('网络异常！');
      }
      );
  }

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

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

  //增加阅读记录
  addRecodings(){
    this.appService.ObserverHttpGet("/wisdomgroup/modules/premanager/addRecodings", null)
    .subscribe((res: Response) => {
     
    }, error => {
      this.appService.alert('网络异常！');
    }
    );
  }
}
