import { Component } from '@angular/core';
import { NavController, ActionSheetController, ActionSheet } from 'ionic-angular';
import { DefaultListable } from '../../defaults/default-listable';
import { INavigable } from '../../interfaces/navigable-interface';
import { Product } from '../../models/product';
import { ProductDetailsPage } from '../product-details/product-details';
import { DataProvider } from '../../providers/data-provider'

@Component({
    selector: 'page-product-list',
    providers: [DataProvider],
    templateUrl: 'product-list.html'
})
export class ProductListPage extends DefaultListable<Product> implements INavigable {

    constructor(
        public navCtrl: NavController,
        public actionSheetCtrl: ActionSheetController,
        public data: DataProvider
    ){
        super();
        this.initialize();
    }

    /**
     * Initialize data list
     */
    initialize(): void {
        this.data.getAll('product').then(r => { 
            this.elements = r.products;
        });
    }

    /**
     * Goto page
     */
    goToPage(page: any) {
        this.navCtrl.push(ProductDetailsPage, this.elements);
    }

    /**
     *
     */
    createOptions(): ActionSheet {
        return this.actionSheetCtrl.create({
            title: "Opciones",
            buttons: [
                {
                    text: "Eliminar",
                    icon: "trash",
                    role: "destructive",
                    handler: () => {
                        console.log("Eliminar");
                    }
                },
                {
                    text: 'Cancel',
                    role: 'cancel',
                    handler: () => {
                        console.log('Cancel clicked');
                    }
                }
            ]
        });
    }

    /**
     *
     */
    openItem(product: any) {
        let options = this.createOptions();
        options.present();
    }
}
