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

import { Partner } from "../../odoo/models/res.partner";
import { BaseDetailsView } from "../../base/base-details-view";

@Component({
    selector: 'page-customer',
    templateUrl: 'customer.html'
})
export class CustomerPage extends BaseDetailsView<Partner>{

    customerForm: FormGroup;

    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public formBuilder: FormBuilder
    ) {
        super(Partner, navParams.data); 
        super.setTitle(super.itemExists() ? "Editar Cliente" : "Crear Cliente");

        this.customerForm = this.formBuilder.group({
            name: ["", Validators.required],
            city: ["", Validators.maxLength(35)],
            street: ["", Validators.maxLength(35)],
            // mobile: ["", Validators.pattern("^\\d+$")],
            // phone: ["", Validators.pattern("^\\d+$")],
            mobile: ["", Validators.maxLength(13)],
            phone: ["", Validators.maxLength(13)],
            email: ["", Validators.pattern("^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$")]
        });

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

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

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

        console.log(super.getItem());
        
        super.setItem(Object.assign(super.getItem(), data, { customer: true }));
        super.performSave();
        this.navCtrl.pop();
    }
}
