import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, AlertController, LoadingController } from 'ionic-angular';

import { Http, Response } from '@angular/http';
import { Storage } from '@ionic/storage';

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


import { LoginPage } from '../../pages/login/login';

@IonicPage()
@Component({
  selector: 'page-modifyPassword',
  templateUrl: 'modifyPassword.html'
})
export class ModifyPasswordPage {

  myreg = /^(?![^a-zA-Z]+$)(?!\\D+$).{8,16}$/;  

  user= {
    mobile:'',
    password:''
  }
  passwordInfo = {
    formerPassword:'',
    modifyPassword:'',
    sureModifyPassword:''
  }

  constructor(public navCtrl: NavController,
    public navParams: NavParams,
    public storage: Storage,
    public alertCtrl: AlertController,
    private loadingCtrl: LoadingController,
    public http: Http,
    public appService: AppService,) {

      this.storage.get("userLoginInfo").then((value)=>{
         this.user = value;
      });
  }

  sureModifyPassword(){
    console.log("修改密码！");

    if(this.passwordInfo.formerPassword == ''||
       this.passwordInfo.modifyPassword == ''||
       this.passwordInfo.sureModifyPassword == ''){
        console.log("请输入完整信息");
        this.appService.popToastView("请输入完整信息！",'top',2000);
        return;
    } 
    if(this.passwordInfo.formerPassword != ''){           
      if(this.user.password != this.passwordInfo.formerPassword){
        console.log("原密码输入有误！");
        this.appService.popToastView("原密码输入有误！",'top',2000);
        return ;
      }  
    }
    if(!this.myreg.test(this.passwordInfo.modifyPassword)){  
        console.log('新密码由8到16位数字和字母组成！');  
        this.appService.popToastView('新密码由8到16位数字和字母组成！','top',2000);
        return false;  
    }  
    if(this.passwordInfo.modifyPassword != this.passwordInfo.sureModifyPassword){
          console.log("两次输入的密码不一致");        
          this.appService.popToastView("两次输入的密码不一致",'top',2000);
          return;
    }else{
          console.log("到后台！");
          this.appService.ObserverHttpPost("/wisdomgroup/manager/updatePassword",{"password":this.passwordInfo.modifyPassword})
          .subscribe((res: Response) => {   
              this.appService.popToastView("修改成功！",'top',2000);                           
              //将缓存中密码密码修改
              this.user.password = this.passwordInfo.modifyPassword;
              console.log(this.user);            
              this.storage.set('userLoginInfo',this.user);
              //退出后台
              this.logoutApp();
              //退出到登录页
              this.navCtrl.setRoot("LoginPage");              
            }, error => {        
              this.appService.alert('网络异常！');    
            }   
          );
    }  
  }

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