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

import { Storage } from '@ionic/storage';
import { AppService } from "../../../../service/http.service";

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

export class EditPersonInfoPage {

  // @ViewChild(Content) content: Content;

  orgName = '';
  user: any;
  gender:object;
  //userInfo: object ;
  userid: '';
  //传入后台的信息
  userinfo = {
    userId : '',
    abbreviation : '',
    telephone : '',
    email : ''
  };

  code:string ;
  //myregMobile = /^((\d)\s*,\s*)*(\d)/;
  //  /(\d*\s*,\s*)*(\d*)/
  myregMobile = /^([0-9]|[-])+$/g; 
  //myregMobile = /^0\d{2,3}-?\d{7,8}$/;

  myregEamil = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
  //页面控制
  edit11:boolean = true;
  edit12:boolean = false;
  edit21:boolean = true;
  edit22:boolean = false;
  edit31:boolean = true;
  edit32:boolean = false;

  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public storage: Storage,
    public appService: AppService,
    public toast: ToastController,
  ) { 

  }

  ionViewDidLoad():void{ 
    this.user = this.storage.get("user").then((value)=>{
      this.user = value;
      this.gender = this.user.userDetail.user.gender;
      this.userinfo = this.user.userDetail.user;
      this.userinfo.userId = this.user.id;
      this.getOrgName(this.user.id,result=>{ 
        let data = result;
        this.orgName = data["orgName"];
      });
    });

    this.code = this.navParams.get("code");
    if(this.code == '1'){
      this.edit12 = true;
      this.edit11 = false;
    }
    if(this.code == '2'){   
      this.edit22 = true;
      this.edit21 = false;
    }
    if(this.code == '3'){
      this.edit32 = true;
      this.edit31 = false;

    }
 
  }

  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 => {  
    });
  }


   //修改信息：房间号，座机号，邮箱
   updateUserInfo(): void {


    if(this.code == '2'){
      if(!this.myregMobile.test(this.userinfo.telephone)){  
        this.appService.popToastView('请输入规则的电话号码！','top',2000);
        return ;   
      } 
    }
    if(this.code == '3'){
      if(!this.myregEamil.test(this.userinfo.email)){  
        this.appService.popToastView('请输入规则的邮箱！','top',2000);
        return ;   
      } 
    }
    
    this.appService.ObserverHttpPost("/wisdomgroup/auth4/updateUserInfo",this.userinfo)
    .toPromise() 
    .then(res => { 
      let data = res.json();
      this.user.userDetail.user = data;

      this.storage.remove("user");
      this.storage.set("user",this.user);
      this.navCtrl.pop();
    }) 
    .catch(error => {   
    });
  }

  // scrollTo(){
  //   this.appService.popToastView('scroll start1！','top',2000);
  //   window.addEventListener('native.keyboardshow',(e:any) =>{
  //     this.appService.popToastView('scroll start2！','top',2000);  
  //     　　　　this.content.scrollTo(0,e.keyboardHeight);
  //     });
  // }
}
