import { Component } from '@angular/core';
import { NavController, LoadingController, ToastController } from 'ionic-angular';
import { INavigable } from "../../interfaces/navigable-interface";
import { DataProvider } from "../../providers/data-provider";
import { DefaultListable } from "../../defaults/default-listable";
import { SaleOrder } from "../../models/sale.order";
import { BudgetDetailsPage } from "../budget-details/budget-details";

@Component({
    selector: 'page-budget-list',
    templateUrl: 'budget-list.html'
})
export class BudgetListPage extends DefaultListable<SaleOrder> implements INavigable {

  constructor(
    public navCtrl: NavController,
    public loadingCtrl: LoadingController,
    public toastCtrl: ToastController,
    public db: DataProvider
  ) { 
      super(loadingCtrl);
  }

    ionViewDidLoad() {
        this.initialize();
    }

    /**
    *
    */
    initialize(): void {
        let loader = this.loadingCtrl.create({
            content: "Cargando presupuestos, espere..."
        });
        loader.present();

        this.db.getAll(DataProvider.DOCS.SALE_ORDER).then(orders => {
            this.elements = orders.filter(item => {
                return item.state === "draft" && item.doc_state !== "deleted"; 
            });

            loader.dismiss();
        }, e => { 
            console.log(e);

            loader.dismiss();

            this.toastCtrl.create({
                message: "No se ha podido cargar los presupuestos",
                duration: 3000
            }).present();
        });
    }

    /**
    *
    */
    goToPage(page: any) {
        this.navCtrl.push(BudgetDetailsPage, this);
    }

    /**
     *
     */
    showOptions(): void {
        
    }

    test() {
        console.log("ok");
        
    }
}
