import { Component, OnInit } from '@angular/core';
import {NavController} from "@ionic/angular";
import {Router} from "@angular/router";
import { WrenchService } from 'src/app/services/wrench.service';
import {SessionDataProviderService} from "../../store/session-data-provider.service";
import {BlogDataService} from "../../store/blog-data.service";
import { AlertController } from "@ionic/angular";

@Component({
  selector: 'app-security',
  templateUrl: './security.page.html',
  styleUrls: ['./security.page.scss'],
})
export class SecurityPage implements OnInit {

  constructor(
    private navctr: NavController,
    private router: Router,
    public sessionDataProviderService:SessionDataProviderService,
    private alertController:AlertController,
    private wrenchService: WrenchService
  ) { }

  ngOnInit() {
  }

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

  usrData: {
    action:number,
    member_id: number,
    uid: string,
    sessionid: string
  };
  requestDeleteResult:string = '';
  reqDelResult:any;
  finilizeRequestDelete(){

    this.usrData = {
      action:99999,
      member_id: this.sessionDataProviderService.member_id,
      uid: this.sessionDataProviderService.member_uid,
      sessionid: this.sessionDataProviderService.session
    };
    this.wrenchService.requestDelete(this.usrData).subscribe(
        reqDelResult => {
          this.reqDelResult = reqDelResult;
          console.log("requestDelete RETURN DATA->", this.reqDelResult);
        }
    );
    this.requestDeleteResult = "Received";
    this.onLogout();
  }

  //requestDelete

  onLogout() {
    this.sessionDataProviderService.DestroySessionOnLogout();
    this.router.navigate(['login']);
  }

  onResetPass(){
    //
    this.router.navigate(['resetpass']);
  }
  async requestDelete() {
    const alert = await this.alertController.create({
      header: "Account Delete",
      message: "You will receive an email with instructions to complete the process. Your account will log out now.",
      buttons: [
        {
          text: "Decline",
          role: "cancel",
          handler: () => {
            console.log("Declined the offer");
          },
        },
        {
          text: "Accept",
          handler: () => {
            this.finilizeRequestDelete();
          },
        },
      ],
    });

    await alert.present();
  }

}
