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

@Component({
    selector: 'app-profile',
    templateUrl: './profile.page.html',
    styleUrls: ['./profile.page.scss'],
})
export class ProfilePage implements OnInit {
    selected_tab: string = 'MY ORDERS';
    userInfo: any;
    showEdit: boolean = false;

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

    ngOnInit() {
        this.userInfo = JSON.parse(localStorage.userInfo)['User Info'];
    }
    
    updateUserInfo() {
        let tempUserInfo = {
            id: this.userInfo.id,
            name: this.userInfo.name,
            username: this.userInfo.username,
            email: this.userInfo.email,
            mobile: this.userInfo.mobile,
            address: []
        }
        
        this.authService.updateUser(tempUserInfo).then((data) => {
            console.log(data);
            this.toastService.presentToast("Updated!", "success");
        }, (err) => {
            console.log(err);
            this.toastService.presentToast("Failed to update", "danger");
        });
    }

	ionViewDidEnter() {
		this.selected_tab = '';
		setTimeout(() => {
			this.selected_tab = 'MY ORDERS';
		}, 100);
	}

    back() {
        this.location.back();
    }

	logout() {
		localStorage.clear();
		this.router.navigate(['login']);
	}

}
