import { Component } from '@angular/core';
import { NavController, NavParams, ActionSheetController, AlertController } from 'ionic-angular';

import { BaseListView } from "../../base/base-list-view";
import { ProductTemplate } from "../../odoo/models/product.template";
import { ProductPage } from "../product/product";

@Component({
    selector: 'page-products',
    templateUrl: 'products.html'
})
export class ProductsPage extends BaseListView<ProductTemplate> {

    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public actionSheetCtrl: ActionSheetController,
        public alertCtrl: AlertController
    ) { 
        super(ProductTemplate);
    }

    /**
     *
     */
    ionViewDidLoad() {
        console.log('ionViewDidLoad ProductsPage');
    }

    /**
     * 
     */
    ionViewDidEnter() {
        super.setSelectedIndex(-1);
    }

    /**
     * 
     * @param item
     */
    openOptions(item: ProductTemplate): void {
        super.setSelectedItem(item);
        
        this.actionSheetCtrl.create({
            title: "Opciones",
            buttons: [
                {
                    text: "Abrir",
                    icon: "open",
                    handler: () => {
                        this.goToDetails();
                    }
                },
                {
                    text: "Eliminar",
                    icon: "close",
                    role: "destructive",
                    handler: () => {
                        this.askIfDelete();
                    }
                },
                {
                    text: "Cancel",
                    role: "cancel",
                    handler: () => {
                        super.setSelectedIndex(-1);
                    }
                },
            ]
        }).present();
    }

    /**
     * 
     */
    goToDetails(): void {
        this.navCtrl.push(ProductPage, super.getSelectedItem());
    }

    /**
     * 
     */
    askIfDelete(): void {
        this.alertCtrl.create({
            title: "Confirmar",
            message: "Quieres eliminar este producto?",
            buttons: [
                {
                    text: "Cancelar"
                },
                {
                    text: "Aceptar",
                    handler: () => {
                        super.performDelete();
                    }
                }
            ]
        }).present();
    }
}
