import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { Product } from '../../models/product'
import { DataProvider } from '../../providers/data-provider'

@Component({
    selector: 'page-product-details',
    providers: [DataProvider],
    templateUrl: 'product-details.html'
})
export class ProductDetailsPage {

    product = {
        name: "",
        type: 0,
        salePrice: 0,
        ean13: "",
        reference: "",
        costPrice: 0,
        quantity: 0,
        salesCount: 0,
        active: true
    }

    constructor(
        public navCtrl: NavController,
        public params: NavParams,
        public data: DataProvider
    ) { }

    ionViewDidLoad() {
        console.log(this.params);
    }

    /**
     *
     */
    private isValidForm() {
        if (!this.product.name) {
            return false;
        }

        return true;
    }
    /**
     *
     */
    save(): void {
        if (!this.isValidForm()) {
            return;
        }

        this.data.save('product', this.product).then(result => {    
            this.params.data.push(result.products[0]);
            this.navCtrl.pop(this);
        });
    }
}
