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

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

    show_password = false;

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

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

    ngOnInit() {
    }

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

    login() {
        this.authService.authenticateUser(this.credentials).then((data: any) => {
            localStorage.userInfo = JSON.stringify(data);
            localStorage.access_Token = data.access_Token;
            this.router.navigate(['/malls']);
            document.querySelector('.menu-icon-holder').classList.remove('hide');
        }, (err) => {
            console.log(err);
            this.toastService.presentToast('Please check your credentials', 'danger');
        })
    }

}
