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

import { BaseListView } from "../../base/base-list-view";
import { Phonecall } from "../../odoo/models/crm.phonecall";
import { PhonecallPage } from "../phonecall/phonecall"

@Component({
    selector: 'page-phonecalls',
    templateUrl: 'phonecalls.html'
})
export class PhonecallsPage extends BaseListView<Phonecall> {

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

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

    /**
     * 
     */
    ionViewDidEnter() {
        super.deselectItem();
    }

    /**
     * 
     * @param item
     */
    openOptions(item: Phonecall): 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: "Cancelar",
                    role: "cancel",
                    handler: () => {
                        super.deselectItem();
                    }
                },
            ]
        }).present();
    }

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

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