import { Component, ViewChild , Renderer,ElementRef} from '@angular/core';
import { NavController, NavParams, AlertController, LoadingController,} from "@ionic/angular";
import { NgForm } from '@angular/forms';
import {TabsPage} from "../../tabs/tabs.page";
import { Http, Response } from '@angular/http';
import {Storage} from "@ionic/storage";
import {CommonService} from "../../../provide/common.service";
import {LoginService} from "./login.service";
import {AppService} from "../../../http/http.service";
import {Router} from "@angular/router";


@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: '',
    remember:true
  };
  tabflag:any;
  constructor(public navCtrl: NavController,
    public navParams: NavParams,
    public storage: Storage,
    public AppService:AppService,
    public alertCtrl: AlertController,
    private loadingCtrl: LoadingController,
    private router:Router,
    public http: Http,private loginSer:LoginService,
    public commonSer: CommonService,
    public render: Renderer,
    public elementRef:ElementRef) {

  }

  ionViewDidLoad(){

  }

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

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

  loseFocus() {
    this.tipFlag = false;
  }

  async login() {

    //验证手机号和密码是否输入
    if(this.user.mobile == null || this.user.mobile == ''){
      this.commonSer.toast('请输入手机号！');
      return ;
    }
    if(this.user.password == null || this.user.password == ''){
      this.commonSer.toast('请输入密码！');
      return ;
    }

    let loading = await this.loadingCtrl.create({
      message: '正在登录...',
      backdropDismiss: 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);
          // this.router.navigate(['/tabs/home'],{
          //   "user": this.user
          // })
          // return this.navCtrl.setRoot(TabsPage, { "user": this.user });

        } else {
          this.commonSer.alert('手机号或密码错误！');
          // return this.navCtrl.setRoot(LoginPage);
        }
      }, error => {
        if (loading) loading.dismiss();
      }
      );
  }
  //忘记密码
  forgetPassword():void{
    this.commonSer.alert('若忘记密码，请与人事处联系。');
  }
}
