import { Component,Injectable,  } from '@angular/core';
import { IonicPage,NavController,NavParams,MenuController } from 'ionic-angular';
import { LoadingController,AlertController  } from 'ionic-angular';  
import {HomePage} from '../home/home';
import { AuthServiceProvider } from '../../providers/auth-service/auth-service';
import {ReactiveFormsModule,FormBuilder,FormGroup,FormControl,Validators,FormsModule, NgForm} from '@angular/forms';
import {Md5} from 'ts-md5/dist/md5';

import {TabsPage} from'../tabs/tabs';
import { Network } from '@ionic-native/network';
@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
  
})
export class LoginPage {
  loading: any;
  loginData = { userMobile:'', password:'' };
  data: any;
  authForm: FormGroup;
  commonUrl : any;
 // console.log(document.cookie);
  constructor( private formBuilder:FormBuilder,public navCtrl: NavController,
    public navParams:NavParams ,
    public Menu:MenuController,
    public loadingCtrl: LoadingController,
    public authService: AuthServiceProvider,
    private alertCtrl: AlertController,
    private network: Network) {
    //console.log(document.cookie);
    this.authForm = formBuilder.group({
      userMobile: ['', Validators.compose([Validators.required])],
      password: ['', Validators.compose([Validators.required])]
  });
  this.commonUrl = this.authService.commonUrl;
  console.log(this.commonUrl);
  }
  
  doLogin(value: any) {
   // console.log(this.loginData);
    let password = Md5.hashStr(this.loginData.password);
    let login = {userMobile:this.loginData.userMobile,password: password};
    //console.log(login);
    this.authService.login(login).subscribe(res=>{
      //this.data = res;
     
      
      if(this.authForm.valid) {
        window.localStorage.setItem('userMobile', value.userMobile);
        window.localStorage.setItem('password', value.password);
        
        if(res.loginStatus == true){
             // console.log(res);
          if(res.details.ListCode =="R001"){
            
            this.navCtrl.setRoot(TabsPage,{});
            //this.navCtrl.pop();
            let Requested = {requestedBy:res.details.ID,requestedFrom:"2"}
            //console.log("req"+Requested);
              this.loading.dismiss();
            localStorage.setItem('ID', res.details.ID);
           // this.homepageAlert();
            //console.log(res.details.ID);
          
          }
          else{
            this.loading.dismiss();
            let message ='Access Provided to Students Only';
          console.log("login_error");
          this.studentAlert(message);
           }
         
        }
        else{
          this.loading.dismiss();
          //console.log(res.message);
          this.logintAlert(res.message);
          console.log("status_error");
         }
         
            
    }
     
    }, (err)=>{
      //alert('error');
     
    })
    
    this.presentLoading();}
  presentLoading() {
   
    this.loading = this.loadingCtrl.create({
      content: "Please wait...",
     // duration: 3000
     
    });
    
  this.loading.present();
  
  
  }
  ionViewDidEnter(){
    this.Menu.enable(false);
  }
  ionViewWillLeave(){
    this.Menu.enable(true);
  }
  
  logintAlert(message){
    let alert = this.alertCtrl.create({
      title: message,
      buttons: ['Ok']
    });
alert.present();
  }
studentAlert(message){
  let alert = this.alertCtrl.create({
    title: message,
    buttons :['ok']
  });
  alert.present();
}
  homepageAlert(){
    
    let alert = this.alertCtrl.create({
      title:'This is Home Page',
      buttons:['ok']
    });
    alert.present();
  }
  
}



