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

import { BaseListView } from "../../base/base-list-view";
import { Partner } from "../../odoo/models/res.partner";
import { CustomerPage } from "../customer/customer";

@Component({
    selector: 'page-customers',
    templateUrl: 'customers.html'
})
export class CustomersPage extends BaseListView<Partner> {

    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public actionSheetCtrl: ActionSheetController,
        public alertCtrl: AlertController
    ) {
        super(Partner, ["customer", "=", true]);
    }

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

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

    /**
     * 
     * @param item
     */
    openOptions(item: Partner): void {
        super.setSelectedItem(item);
        
        this.actionSheetCtrl.create({
            title: "Opciones",
            buttons: [
                {
                    text: "Abrir",
                    icon: "open",
                    handler: () => {
                        this.goToDetails();
                    }
                },
                {
                    text: "Ir a ubicación",
                    icon: "navigate",
                    handler: () => {

                    }
                },
                {
                    text: "Llamar",
                    icon: "call",
                    handler: () => {

                    }
                },
                {
                    text: "Eliminar",
                    icon: "close",
                    role: "destructive",
                    handler: () => {
                        this.askIfDelete();
                    }
                },
                {
                    text: "Cancelar",
                    role: "cancel",
                    handler: () => {
                        super.deselectItem();
                    }
                },
            ]
        }).present();
    }

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

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