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

import { SignupPage2 } from '../signup/signup2';
import { Pata } from '../../pata';

import { AuthService } from '../../services/auth.service';

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

export class SignupPage {

  public login: any = null;
  public loading: any;

  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public service: Pata,
    public loadingCtrl: LoadingController,
    public storage: Storage,
    private alertCtrl: AlertController,
    private auth: AuthService
  ){
  	this.login = {
  		rut: ''
  	};
  }


  back1() {
    this.navCtrl.pop();
  }

  next() {
 
    if (this.login.rut == "") {
  		this.service.logError({}, "Rut no puede estar vacío o es incorrecto");
  	}
  	else {

      this.loading = this.loadingCtrl.create();
      this.loading.present();

  	  this.auth.evaluate(this.login).subscribe((data: any) => {
          this.loading.dismiss();
          if(data.res == "OK"){
            this.navCtrl.push(SignupPage2, {
              rut: this.login.rut
            });
          }
          else {
            this.service.logError({}, data.msg);
          }
        },
        err => {
          this.loading.dismiss();
          this.service.logError({});
        }
      );
  	}
  }


  gotoLogin(){
    this.navCtrl.pop();
  }
}