import { Component } from '@angular/core';
import { Validators, FormGroup, FormBuilder } from "@angular/forms";
import { NavController, NavParams } from 'ionic-angular';

import { ProductTemplate } from "../../odoo/models/product.template";
import { BaseDetailsView } from "../../base/base-details-view";

@Component({
    selector: 'page-product',
    templateUrl: 'product.html'
})
export class ProductPage extends BaseDetailsView<ProductTemplate>{

    productForm: FormGroup;

    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public formBuilder: FormBuilder
    ) { 
        super(ProductTemplate, navParams.data);
        super.setTitle(super.itemExists() ? "Editar Producto" : "Nuevo Producto");

        this.productForm = this.formBuilder.group({
            name: ["", Validators.required],
            ean13: ["", Validators.maxLength(35)],
            defaultCode: ["", Validators.maxLength(35)],
        });

        if (super.itemExists()) {
            this.productForm.patchValue(super.getItem());
        }
    }

    ionViewDidLoad() {
        console.log('ionViewDidLoad ProductPage');
    }

     /**
     * 
     * @param data 
     */
    submit(data: any) {
        if (this.productForm.invalid) {
            return;
        }

        super.setItem(Object.assign(this.getItem(), data));
        super.performSave();
        this.navCtrl.pop();
    }
}
