import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Chart } from 'ng2-chartjs2';
import { DataProvider } from '../../providers/data-provider';

@Component({
    selector: 'page-home',
    templateUrl: 'home.html'
})
export class HomePage {

    labels: string[] = ["", "", "", "", ""];
    data: Chart.Dataset[] = [
        {
            label: "# de Ventas",
            data: [0, 0, 0, 0, 0]
        }
    ];

    constructor(
        public navCtrl: NavController,
        public dataProvider: DataProvider
    ) { }

    /**
     *
     */
    ionViewDidLoad() {
        console.log('Hello Home Page');
        this.draw();
    }

    /**
     *
     */
    draw(): void {
        this.dataProvider.getAll("product").then(products => {
            let items: Array<any> = products.filter(item => {
                return item.sales_count;
            }).sort((left, right) => {
                return left.sales_count - right.sales_count;
            }).slice(0, 10);

            for (let i = 0; i < items.length; i++) {
                this.labels[i] = items[i].name;
                this.data[0].data[i] = items[i].sales_count;
            }
            
        }).catch(e => {
            console.log(e);
        });
    }

    tap(e) {
        console.log(e);
        
    }
}
