import {Component, OnInit} from '@angular/core';
import {Router, ActivatedRoute, NavigationExtras} from '@angular/router';
import {HttpClient} from '@angular/common/http';
import {AppConfig} from '../config/app.config';

@Component({
    templateUrl: 'lists.component.html',
    styleUrls: ['lists.component.scss']
})

export class ListsComponent implements OnInit {

    currentUser: any;
    userData: any;
    lists = [];

    constructor(private route: ActivatedRoute,
                private router: Router,
                private http: HttpClient) {

        this.currentUser = JSON.parse(sessionStorage.getItem(AppConfig.relativeURL + 'currentUser'));
        this.userData = JSON.parse(sessionStorage.getItem(AppConfig.relativeURL + 'userData'));
    }

    ngOnInit() {
        this.getLists(this.currentUser.sessionID).subscribe(
            data => {
                this.lists = data.dataset.CustomLists;
            },
            error => {
                console.log(error); // Error if any
            });
    }

    openList(list) {
        this.router.navigate([AppConfig.routes.list + '/' + list.SessionID]);
    }

    private getLists(sessionID: string) {
        return this.http.post<any>(AppConfig.endpointURL() + AppConfig.endpoints.getCustomLists + sessionID, {
            'tpaID': 'tportal',
            'custID': this.userData.id_cust
        }, {headers: {'Content-Type': 'text/plain'}}).map(data => {
            return data;
        });
    }
}
