import { Component, ViewChild , Renderer,ElementRef} from '@angular/core';
import { IonicPage, NavController, NavParams, AlertController, LoadingController } from 'ionic-angular';
import { NgForm } from '@angular/forms';
import { TabsPage } from '../tabs/tabs';
import { Http, Response } from '@angular/http';
import { Storage } from '@ionic/storage';

import { AppService } from '../../service/appHttpService';


@IonicPage()
@Component({
  selector: 'page-login',
  templateUrl: 'login.html'
})
export class LoginPage {
  @ViewChild('loginForm') currentForm: NgForm;	//实时表单
  loginForm: NgForm;								//初始表单
  submitted: boolean = false;								//表单是否已经提交
  tipFlag: boolean = false;            // 输入时隐藏输入框以下信息
  user = {
    userid: '',							//登录数据
    mobile: '',
    password: ''
  };
  tabflag:any;
  constructor(public navCtrl: NavController,
    public navParams: NavParams,
    public storage: Storage,
    public alertCtrl: AlertController,
    private loadingCtrl: LoadingController,
    public http: Http,
    public appService: AppService,
    public render: Renderer,
    public elementRef:ElementRef) {

  }

  ionViewDidLoad(){
    this.storage.get("userLoginInfo").then((value)=>{
      if(value!=null && value != ''){        
        this.user = value;
        //return this.navCtrl.setRoot(TabsPage, { "user": this.user });

        this.appService.ObserverHttpPost("/wisdomgroup/app/loginpost", this.user)
          .subscribe((res: Response) => {
              let data = res.json();
              if (data["code"] == '200') {
                //将当前登录用户存进缓存，便于后期逻辑操作
                this.storage.set('user', data.data);
                this.user.userid = data.data.id;
                this.storage.set('userLoginInfo',this.user);
                return this.navCtrl.setRoot(TabsPage, { "user": this.user });
              } else {
                this.appService.alert('手机号或密码错误！');
                return this.navCtrl.setRoot(LoginPage);
              }
          }, error => {
              this.appService.alert('网络异常！');
          }
        );
      }    
    });

  }

  ionViewDidEnter() {
     //默认显示
     this.storage.get("userLoginInfo").then((value)=>{
      if(value!=null && value != ''){        
        this.user = value;
      }    
    });
  }

  //输入框获取焦点：隐藏，失去焦点：显示
  getFocus() {
    this.tipFlag = true;
  }
  loseFocus() {
    this.tipFlag = false;
  }

  login() {
    
    //验证手机号和密码是否输入
    if(this.user.mobile == null || this.user.mobile == ''){
      this.appService.popToastView('请输入手机号！','top',2000);
      return ;
    }
    if(this.user.password == null || this.user.password == ''){
      this.appService.popToastView('请输入密码！','top',2000);
      return ;
    }

    let loading = this.loadingCtrl.create({
      content: '正在登录...',
      dismissOnPageChange: true,		//打开新的页面则关闭 正在登录 
      enableBackdropDismiss: true		//点击页面其他地方则关闭 正在登录
    });
    loading.present();
    this.appService.ObserverHttpPost("/wisdomgroup/app/loginpost", this.user)
      .subscribe((res: Response) => {
        loading.dismiss();
        this.submitted = true;
        let data = res.json();
        if (data["code"] == '200') {
          //将当前登录用户存进缓存，便于后期逻辑操作
          this.storage.set('user', data.data);
          this.user.userid = data.data.id;
          this.storage.set('userLoginInfo',this.user);
          return this.navCtrl.setRoot(TabsPage, { "user": this.user });
        } else {
          this.appService.alert('手机号或密码错误！');
          return this.navCtrl.setRoot(LoginPage);
        }
      }, error => {
        if (loading) loading.dismiss();
        //this.appService.alert('系统错误！');
        this.appService.alert('网络异常！');
      }
      );
  }
  //忘记密码
  forgetPassword():void{
    this.appService.alert('若忘记密码，请与人事处联系。');
  }
}
