/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.icentricApp.client.pages.widgets;

import com.icentricApp.client.ServerServiceAsync;
import com.icentricApp.client.data.CustomerDataSource;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
import com.smartgwt.client.widgets.form.fields.HeaderItem;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;

/**
 *
 * @author jeni
 */
public class CustomerTab extends Tab {

    private ServerServiceAsync service;
    private VLayout layout = new VLayout();
    private DynamicForm form = new DynamicForm();

    public CustomerTab(String title) {
        setTitle(title);
        createLayout();

        setPane(layout);
    }

    private void createLayout() {

        form.setDataSource(CustomerDataSource.getInstance());
        form.setUseAllDataSourceFields(true);

        HeaderItem header = new HeaderItem();
        header.setDefaultValue("Customer Details");

        final ComboBoxItem cbRef = new ComboBoxItem("customerRef", "Ref");
        cbRef.setHint("<nobr>Select / Enter Customer Reference</nobr>");
        cbRef.setValueField("customerRef");
        cbRef.setOptionDataSource(CustomerDataSource.getInstance());

        cbRef.addChangedHandler(new ChangedHandler() {
            public void onChanged(ChangedEvent event) {
                ListGridRecord record = cbRef.getSelectedRecord();
                if(record != null){
                    fillFields(record);
                }
            }
        });

        final ComboBoxItem cbName = new ComboBoxItem("customerName", "Name");
        cbName.setHint("<nobr>Select / Enter Customer name</nobr>");
        cbName.setValueField("customerName");
        cbName.setOptionDataSource(CustomerDataSource.getInstance());

        cbName.addChangedHandler(new ChangedHandler() {
            public void onChanged(ChangedEvent event) {
                ListGridRecord record = cbName.getSelectedRecord();
                if(record != null){
                    fillFields(record);
                }
            }
        });

        final ComboBoxItem cbAddress = new ComboBoxItem("customerAddress", "Address");
        cbAddress.setHint("<nobr>Select / Enter Customer Address</nobr>");
        cbAddress.setValueField("customerAddress");
        cbAddress.setOptionDataSource(CustomerDataSource.getInstance());

        cbAddress.addChangedHandler(new ChangedHandler() {
            public void onChanged(ChangedEvent event) {
                ListGridRecord record = cbAddress.getSelectedRecord();
                if(record != null){
                    fillFields(record);
                }
            }
        });

        final ComboBoxItem cbPostcode = new ComboBoxItem("customerPostcode", "Postcode");
        cbPostcode.setHint("<nobr>Select / Enter Customer Postcode</nobr>");
        cbPostcode.setValueField("customerPostcode");
        cbPostcode.setOptionDataSource(CustomerDataSource.getInstance());

        cbPostcode.addChangedHandler(new ChangedHandler() {
            public void onChanged(ChangedEvent event) {
                ListGridRecord record = cbPostcode.getSelectedRecord();
                if(record != null){
                    fillFields(record);
                }
            }
        });

        final ComboBoxItem cbPhoneNo = new ComboBoxItem("custPhoneNo", "Phone Number");
        cbPhoneNo.setHint("<nobr>Select / Enter Customer Phone No</nobr>");
        cbPhoneNo.setValueField("custPhoneNo");
        cbPhoneNo.setOptionDataSource(CustomerDataSource.getInstance());

        cbPhoneNo.addChangedHandler(new ChangedHandler() {
            public void onChanged(ChangedEvent event) {
                ListGridRecord record = cbPhoneNo.getSelectedRecord();
                if(record != null){
                    fillFields(record);
                }
            }
        });

        final ComboBoxItem cbMobileNo = new ComboBoxItem("custMobileNo", "Mobile Number");
        cbMobileNo.setHint("<nobr>Select / Enter Customer Mobile No</nobr>");
        cbMobileNo.setValueField("custMobileNo");
        cbMobileNo.setOptionDataSource(CustomerDataSource.getInstance());

        cbMobileNo.addChangedHandler(new ChangedHandler() {
            public void onChanged(ChangedEvent event) {
                ListGridRecord record = cbMobileNo.getSelectedRecord();
                if(record != null){
                    fillFields(record);
                }
            }
        });

        final ComboBoxItem cbContactName = new ComboBoxItem("custContactName", "Customer Contact");
        cbContactName.setHint("<nobr>Select / Enter Customer Contact</nobr>");
        cbContactName.setValueField("custContactName");
        cbContactName.setOptionDataSource(CustomerDataSource.getInstance());

        cbContactName.addChangedHandler(new ChangedHandler() {
            public void onChanged(ChangedEvent event) {
                ListGridRecord record = cbContactName.getSelectedRecord();
                if(record != null){
                    fillFields(record);
                }
            }
        });

        final ComboBoxItem cbEmail = new ComboBoxItem("custEmailAddress", "Email");
        cbEmail.setHint("<nobr>Select / Enter Customer Email Address</nobr>");
        cbEmail.setValueField("custEmailAddress");
        cbEmail.setOptionDataSource(CustomerDataSource.getInstance());

        cbEmail.addChangedHandler(new ChangedHandler() {
            public void onChanged(ChangedEvent event) {
                ListGridRecord record = cbEmail.getSelectedRecord();
                if(record != null){
                    fillFields(record);
                }
            }
        });

        form.setFields(header, cbName, cbAddress, cbPostcode, cbPhoneNo, cbMobileNo, cbContactName, cbEmail, cbRef);

        Button nextButton = new Button("Next");
        nextButton.addClickHandler(new ClickHandler() {

            public void onClick(ClickEvent event) {
                
            }
        });

        layout.addMember(form);
        layout.addMember(nextButton);
    }

    private void fillFields(ListGridRecord record){
        for(int i = 0; i < record.getAttributes().length; i++){
            if(form.getField(record.getAttributes()[i]) != null){
                System.out.println(record.getAttributes()[i]);
                form.getField(record.getAttributes()[i]).setValue(record.getAttribute(record.getAttributes()[i]));
            }
        }
        
    }
}
