/*
  Authors : initappz (Rahul Jograna)
  Website : https://initappz.com/
  App Name : E-Learning App Template
  This App Template Source code is licensed as per the
  terms found in the Website https://initappz.com/license
  Copyright and Good Faith Purchasers © 2021-present initappz.
*/
import { Router } from '@angular/router';
import { NavController } from '@ionic/angular';
import {Component, OnInit, ViewChild} from '@angular/core';
import {WrenchService} from "../../services/wrench.service";
import {SessionDataProviderService} from "../../store/session-data-provider.service";

@Component({
  selector: 'app-new-password',
  templateUrl: './new-password.page.html',
  styleUrls: ['./new-password.page.scss'],
})
export class NewPasswordPage implements OnInit {
@ViewChild('confirm_new_password')  confirm_new_password;
@ViewChild('new_password')  new_password;

  resetResult;any;
  constructor(
    private navctr: NavController,
    private router: Router,
    private wrenchService: WrenchService,
    public sessionDataProviderService:SessionDataProviderService
  ) {
    this.resetResult  =  this.router.getCurrentNavigation().extras.state;
    if ( this.resetResult != null
        &&  this.resetResult.internal_return == 0 &&
        this.resetResult.lostpass_id > 0 &&
        this.resetResult.reset_uid !='' &&
        this.resetResult.m_uid != '' ) {
    }
    else
    {
      this.onLogin();
    }
  }

  ngOnInit() {
  }

  onBack() {
    this.navctr.back();
  }

  onLogin() {
    this.router.navigate(['login']);
  }

  postData: {
    action:number,
    step: number,
    m_uid: string,
    reset_uid: string,
    sessionid: string,
    reset_link: string,
    channel:'MOBILE',
    newpass: string
  };
  completeResetResults:any;

  onCompleteNewReset(){

    if (this.new_password.length < 8 || (this.new_password != this.confirm_new_password) ){
      this.showAlert('Error', 'Enter new password to continue');
      return;
    }
    this.postData = {
      action:730,
      step: 300,
      m_uid: this.resetResult.m_uid,
      reset_uid: this.resetResult.reset_uid,
      sessionid: 'DUMMY-CANNOT_BE_EMPTY',
      reset_link: 'DUMMY-CANNOT_BE_EMPTY',
      channel:'MOBILE',
      newpass: this.new_password
    };
    this.wrenchService.verifyPassReset(this.postData).subscribe(
        completeResetResults => {
          this.completeResetResults = completeResetResults;
          console.log("completeResetResults RETURN->", this.completeResetResults);
          this.showAlert('Completed', 'Login with new password now');
          this.router.navigate(['login']);
        }
    );
  }
  showAlert(mtitle: string, amessage: string) {
    /* let alert = this.alertCtrl.create({
       title: mtitle,
       subTitle: amessage,
       buttons: ['OK']
     });
     alert.present();*/
    alert(amessage);
  }
}
