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

import { Lead } from "../../odoo/models/crm.lead";
import { BaseDetailsView } from "../../base/base-details-view";

import { CustomersPage } from "../customers/customers";

import { EventsManager } from "../../base/events/event-manager";

@Component({
    selector: 'page-lead',
    templateUrl: 'lead.html'
})
export class LeadPage extends BaseDetailsView<Lead> {

    lead: any;

    constructor(
        public navCtrl: NavController,
        public navParams: NavParams
    ) { 
        super(Lead, navParams.data.item, navParams.data.kind);

        let titleKind = super.getKind() === "lead" ? "Iniciativa" : "Oportunidad";
        super.setTitle(super.itemExists() ? "Editar " + titleKind : "Crear " + titleKind);

        this.lead = {}
    }

    /**
     * 
     */
    ionViewDidLoad() {
        EventsManager.subscribe("app:select", data => { 
            this.lead.partner_id = data.item;

            this.lead.street = this.lead.partner_id.street;
            this.lead.city = this.lead.partner_id.city;
            this.lead.contact_name = this.lead.partner_id.name;
            this.lead.email = this.lead.partner_id.email;
            this.lead.phone = this.lead.partner_id.phone;
            this.lead.mobile = this.lead.partner_id.mobile;
        });
    }

    /**
     * 
     */
    selectCustomer(): void {
        this.navCtrl.push(CustomersPage, { 
            params: {
                mode: "selection"
            }
        });
    }

    /**
     * 
     * @param e 
     */
    changeCustomer(customer: string): void {
        this.lead.partner_id = customer;
    }

    /**
     * 
     */
    formIsValid(): boolean {
        return !!this.lead.name && !!this.lead.partner_id;
    }

    /**
     * 
     */
    submit(): void {
        super.setItem(this.lead);
        super.performSave();
    }
}
