import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { AuthService } from '../services/auth.service';
import { ToastService } from '../services/toast.service';
import { LoadingController } from '@ionic/angular';

@Component({
    selector: 'app-login',
    templateUrl: './login.page.html',
    styleUrls: ['./login.page.scss'],
})
export class LoginPage implements OnInit {
    credentials = {
        username: 'prahalad1',
        password: 'Reset@123',
        login_type: 'user'
    };

    show_password = false;

    loader: any;

    constructor(
        private router: Router,
        private authService: AuthService,
        public toastService: ToastService,
        public loadingController: LoadingController
    ) { }

    ionViewDidEnter() {
        document.querySelector('.menu-icon-holder').classList.add('hide');
    }

    async presentLoading() {
        this.loader = await this.loadingController.create({
          message: 'Please wait...',
          spinner: 'dots',
          mode: 'ios'
        });
        await this.loader.present();
    }

    ngOnInit() {
    }

    signup() {
        this.router.navigate(['/onboarding']);
    }

    login() {
        this.presentLoading();
        
        this.authService.authenticateUser(this.credentials).then((data: any) => {
            localStorage.userInfo = JSON.stringify(data);
            localStorage.access_Token = data.access_Token;

            this.loader.dismiss();

            this.router.navigate(['/malls']);
            document.querySelector('.menu-icon-holder').classList.remove('hide');
        }, (err) => {
            this.loader.dismiss();

            this.toastService.presentToast('Please check your credentials', 'danger');
        })
    }

}
