import { Component } from '@angular/core';
import {  App, NavController, AlertController, NavParams } from 'ionic-angular';
import { LoginPage } from '../../login/login';
import { AppService } from '../../../service/appHttpService';
import { PersonInfoPage } from '../../personInfo/personInfo';
import { MyActivityListPage } from '../../myActivityList/myActivityList';
import { MyReportPage } from '../../myReport/myReport';
import { OperationListPage } from '../../operation-list/operation-list';
import { VersionPage } from '../../version/version';
import { Response } from '@angular/http';
import { Storage } from '@ionic/storage';

import { AppVersion } from '@ionic-native/app-version';
import { AppUpdateService } from '../../../service/appUpdateService';


@Component({
  selector: 'page-mine',
  templateUrl: 'mine.html',
})
export class MinePage {

  user: any;
  orgName: '';
  gender: object;
  version: string = '';
  isLatest:boolean = true;
  constructor(public navCtrl: NavController,
    private appCtrl: App,
    private alertCtrl: AlertController,
    public navParams: NavParams,
    public appService: AppService,
    public storage: Storage,
    private appUpdateService: AppUpdateService,
    private appVersion: AppVersion) {
    this.orgNameInfo();
  }

  ionViewWillEnter() {
    this.getversion();
  }

  orgNameInfo(): void {
    this.user = this.storage.get("user").then((value) => {
      this.user = value;
      console.log(this.user);
      console.log("id1:" + this.user.id);
      this.gender = this.user.userDetail.user.gender;
      console.log("gender:" + this.gender);
      this.getOrgName(this.user.id, result => {
        let data = result;
        this.orgName = data["orgName"];
      });
    });
  }
  getOrgName(id, callback?): any {
    this.appService.ObserverHttpPost("/wisdomgroup/app/getOrgName", { "userid": id })
      .toPromise()
      .then(res => {
        var data = res.json();
        callback(data == null ? "[]" : data);
      })
      .catch(error => {
      });
  }

  personInfo() {
    this.navCtrl.push("PersonInfoPage");
  }
  //我的活动
  myActivityList() {
    this.navCtrl.push("MyActivityListPage");
  }

  //我的报备
  myReport() {
    this.navCtrl.push("MyReportPage");
  }

  //操作记录
  myOperationList() {
    this.navCtrl.push("OperationListPage");
  }

  logout() {
    this.alertCtrl.create({
      message: "确认退出登录？",
      buttons: [
        {
          text: '取消',
          role: 'cancel'
        },
        {
          text: '确定',
          handler: () => {
            // this.appService.ObserverHttpGet("/wisdomgroup/app/logout",null)
            // .subscribe((res: Response) => {                
            //   this.appCtrl.getRootNav().setRoot(LoginPage)	                  
            //   }, error => {                    
            //       this.appService.alert('系统错误！');
            //   })
            //退出登陆，将缓存中的用户注销，跳转到登陆页面。
            this.storage.get("user").then((value) => {
              console.log(value);
            });
            this.storage.remove("user");
            this.storage.remove("userLoginInfo");
            this.logoutApp();
            this.appCtrl.getRootNav().setRoot(LoginPage)
          }

        }]
    }).present();
  }

  //后台退出
  logoutApp() {
    this.appService.ObserverHttpGet("/wisdomgroup/app/logout", null)
      .subscribe((res: Response) => {
        let data = res.json();
      }, error => {
        this.appService.alert('网络异常！');
      }
      );
  }

  //版本信息
  currentVersion() {
    this.navCtrl.push("VersionPage");
  }

  //获取最新版本信息
  getversion() {
    //检测是否需要更新
    this.appVersion.getVersionNumber().then((version: string) => {
      this.version = version;
      this.appUpdateService.compariVersion().subscribe(data => {
        if (data.code == 200) {
          if (data.latestVersion != null && data.latestVersion != version) {
            this.isLatest = false;
          }
        }
      });
    }).catch(err => {
      console.log('getVersionNumber:' + err);
    });
  }
}
