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

import { HomePage } from '../home/home';
import { Pata } from '../../pata';
import {AuthService} from "../../services/auth.service";

/**
 * Generated class for the LoginCodePage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */

@Component({
  selector: 'page-lostpassword',
  templateUrl: 'lostpassword.html',
})
export class LostPasswordPage  {

  requestInProgress: boolean = false;
  rut: string = '';
  appInvite: boolean = true;

  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public service: Pata,
    public storage: Storage,
    private auth: AuthService
  ){
    this.appInvite = this.service.appInvite;
  }


  accept() {
    /*let EMAIL_REGEXP = /^[a-z0-9!#$%&'*+\/=?^_`{|}~.-]+@[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/i;*/

    if (this.rut.trim() == '') {
      if (this.appInvite) {
        this.service.logError({}, "RUT no puede estar vacio");
      } else {
        this.service.logError({}, "Email no puede estar vacio");
      }
      
    }
    /*else if (!EMAIL_REGEXP.test(this.email.trim())) {
      this.service.logError({}, "Email es inválido");
    }
    */
    else {
      this.requestInProgress = true;
      this.auth.recoverPassword(this.rut.trim()).subscribe((data:any) => {
        if (data.res == 'OK') {
          this.requestInProgress = false;
          this.service.showOk(data.msg);
          this.navCtrl.pop();
        }
        else {
          this.requestInProgress = false;
          this.service.logError({}, data.msg);
        }
      });

    }

  }

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

}
