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

import { BaseListView } from "../../base/base-list-view";
import { Lead } from "../../odoo/models/crm.lead";
import { LeadPage } from "../lead/lead";

@Component({
    selector: 'page-leads',
    templateUrl: 'leads.html'
})
export class LeadsPage extends BaseListView<Lead>  {

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

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

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

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

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

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

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