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

import { BaseListView } from "../../base/base-list-view";
import { SaleOrder } from "../../odoo/models/sale.order";
import { OrderPage } from "../order/order";

@Component({
    selector: 'page-orders',
    templateUrl: 'orders.html'
})
export class OrdersPage extends BaseListView<SaleOrder> {

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

        super.setTitle(navParams.data.title);
        super.setFilters(navParams.data.params.filters);
    }

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

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

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

    /**
     * 
     */
    goToDetails(): void {
        this.navCtrl.push(OrderPage, {
            item: super.getSelectedItem(),
            kind: this.navParams.data.params.kind
        });
    }

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