import {Component, OnInit} from '@angular/core';
import {FundDataService} from '../funddata/funddata.service';

@Component({
	selector: 'app-fund-table',
	templateUrl: './fund-table.component.html',
	styleUrls: ['./fund-table.component.css']
})
export class FundTableComponent implements OnInit {

	private colShow: string[] = ['link_linkDisplay', 'fundResult_morningstarRating', 'oneQuarterAgoChange'];
	private columns: string[];
	private funds: Object[];
	private fundsShow: Object[];
	private error: any;

	constructor(private fundDataService: FundDataService) {
	}

	ngOnInit() {
		this.fundDataService.loadFunds().subscribe((datas) => {
			this.funds = datas;
			this.fundsShow = datas;
			this.columns = Object.keys(datas[0]);

			// console.log('columns: ' + this.columns);
			// console.log(JSON.stringify(datas));
		}, (err) => {// error
			console.log(err);
			this.error = err;
		}, () => {// complete
		});
	}

	toggleColumn(col: string) {
		if (this.colShow.indexOf(col) === -1) {
			this.colShow.push(col);
		} else {
			this.colShow.splice(this.colShow.indexOf(col), 1);
		}
	}
}
