package com.info08.billing.callcenter.client.content;

import com.info08.billing.callcenter.client.dialogs.correction.DlgAddEditAbonent;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.ExportDisplay;
import com.smartgwt.client.types.ExportFormat;
import com.smartgwt.client.util.EnumUtil;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.IButton;
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.TextItem;
import com.smartgwt.client.widgets.form.fields.events.KeyPressEvent;
import com.smartgwt.client.widgets.form.fields.events.KeyPressHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.toolbar.ToolStrip;
import com.smartgwt.client.widgets.toolbar.ToolStripButton;

public class TabAbonent extends Tab {

	private VLayout mainLayout;
	private DataSource abonentsDS;
	private DynamicForm searchForm;
	private TextItem firstNameItem;
	private TextItem lastNameItem;
	private TextItem adressItem;
	private TextItem phoneItem;
	private IButton findButton;
	private IButton clearButton;
	private ToolStripButton addPersonBtn;
	private ToolStripButton editPersonBtn;
	private ToolStripButton deletePersonBtn;
	private ToolStripButton exportButton;

	public TabAbonent() {
		setTitle("აბონენტების მართვა");
		setCanClose(true);

		abonentsDS = DataSource.get("AbonentsDS");

		mainLayout = new VLayout(5);
		mainLayout.setWidth100();
		mainLayout.setHeight100();

		searchForm = new DynamicForm();
		searchForm.setPadding(10);
		searchForm.setAutoFocus(true);
		searchForm.setWidth(780);
		searchForm.setTitleWidth(170);
		searchForm.setNumCols(4);
		mainLayout.addMember(searchForm);

		firstNameItem = new TextItem();
		firstNameItem.setTitle("სახელი");
		firstNameItem.setWidth("100%");
		firstNameItem.setName("firstNameItem");

		lastNameItem = new TextItem();
		lastNameItem.setTitle("გვარი");
		lastNameItem.setWidth("100%");
		lastNameItem.setName("lastNameItem");

		adressItem = new TextItem();
		adressItem.setTitle("მისამართი");
		adressItem.setWidth("100%");
		adressItem.setName("adressItem");

		phoneItem = new TextItem();
		phoneItem.setTitle("ტელეფონის ნომერი");
		phoneItem.setWidth("100%");
		phoneItem.setName("phoneItem");

		searchForm
				.setFields(firstNameItem, lastNameItem, adressItem, phoneItem);

		HLayout buttonLayout = new HLayout(5);
		buttonLayout.setWidth(750);
		buttonLayout.setHeight(30);
		buttonLayout.setAlign(Alignment.RIGHT);
		buttonLayout.setStyleName("paddingRight");
		mainLayout.addMember(buttonLayout);

		clearButton = new IButton();
		clearButton.setTitle("გასუფთავება");

		findButton = new IButton();
		findButton.setTitle("ძებნა");

		buttonLayout.setMembers(findButton, clearButton);

		ToolStrip toolStrip = new ToolStrip();
		toolStrip.setWidth(750);
		toolStrip.setPadding(5);
		mainLayout.addMember(toolStrip);

		addPersonBtn = new ToolStripButton("დამატება", "person_add.png");
		addPersonBtn.setLayoutAlign(Alignment.LEFT);
		addPersonBtn.setWidth(50);
		toolStrip.addButton(addPersonBtn);

		editPersonBtn = new ToolStripButton("შეცვლა", "person_edit.png");
		editPersonBtn.setLayoutAlign(Alignment.LEFT);
		editPersonBtn.setWidth(50);
		toolStrip.addButton(editPersonBtn);

		deletePersonBtn = new ToolStripButton("წაშლა", "person_delete.png");
		deletePersonBtn.setLayoutAlign(Alignment.LEFT);
		deletePersonBtn.setWidth(50);
		toolStrip.addButton(deletePersonBtn);

		toolStrip.addSeparator();

		exportButton = new ToolStripButton("Excel - ში გადატანა", "excel.gif");
		exportButton.setLayoutAlign(Alignment.LEFT);
		exportButton.setWidth(50);
		toolStrip.addButton(exportButton);

		ListGridField firstName = new ListGridField("firstname", "სახელი", 100);
		ListGridField lastName = new ListGridField("lastname", "გვარი", 100);
		ListGridField phone = new ListGridField("phone", "ნომერი", 80);
		ListGridField phone_state = new ListGridField("phone_state", "სტატუსი",
				60);
		// ListGridField country = new ListGridField("country", "ქვეყანა", 80);
		ListGridField city = new ListGridField("city", "ქალაქი", 80);
		ListGridField street = new ListGridField("street", "ქუჩა", 260);
		ListGridField address = new ListGridField("address", "მისამ.", 70);
		ListGridField upd_date = new ListGridField("upd_date", "განახლება", 150);
		ListGridField upd_user = new ListGridField("upd_user", "მომხმარ.", 60);

		upd_date.setAlign(Alignment.CENTER);

		final ListGrid abonentsGrid = new ListGrid();
		abonentsGrid.setWidth100();
		abonentsGrid.setHeight100();
		abonentsGrid.setAlternateRecordStyles(true);
		abonentsGrid.setDataSource(abonentsDS);
		abonentsGrid.setAutoFetchData(false);
		abonentsGrid.setShowFilterEditor(false);
		abonentsGrid.setCanEdit(false);
		abonentsGrid.setCanRemoveRecords(false);
		abonentsGrid.setFetchOperation("customSearch");
		abonentsGrid.setFields(firstName, lastName, phone, phone_state, city,
				street, address, upd_date, upd_user);

		mainLayout.addMember(abonentsGrid);

		findButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				search(abonentsGrid);
			}
		});

		clearButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				searchForm.clearValues();
				abonentsGrid.clearCriteria();
				Criteria criteria = abonentsGrid.getCriteria();
				if (criteria == null) {
					criteria = new Criteria();
				}
				Criteria formCriteria = searchForm.getValuesAsCriteria();
				criteria.addCriteria(formCriteria);
			}
		});

		exportButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				com.smartgwt.client.data.DSRequest dsRequestProperties = new com.smartgwt.client.data.DSRequest();
				dsRequestProperties.setExportAs((ExportFormat) EnumUtil
						.getEnum(ExportFormat.values(), "xls"));
				dsRequestProperties.setExportDisplay(ExportDisplay.DOWNLOAD);
				dsRequestProperties.setOperationId("customSearch");
				abonentsGrid.exportData(dsRequestProperties);
			}
		});
		firstNameItem.addKeyPressHandler(new KeyPressHandler() {
			@Override
			public void onKeyPress(KeyPressEvent event) {
				if (event.getKeyName().equals("Enter")) {
					search(abonentsGrid);
				}
			}
		});
		lastNameItem.addKeyPressHandler(new KeyPressHandler() {
			@Override
			public void onKeyPress(KeyPressEvent event) {
				if (event.getKeyName().equals("Enter")) {
					search(abonentsGrid);
				}
			}
		});
		adressItem.addKeyPressHandler(new KeyPressHandler() {
			@Override
			public void onKeyPress(KeyPressEvent event) {
				if (event.getKeyName().equals("Enter")) {
					search(abonentsGrid);
				}
			}
		});
		phoneItem.addKeyPressHandler(new KeyPressHandler() {
			@Override
			public void onKeyPress(KeyPressEvent event) {
				if (event.getKeyName().equals("Enter")) {
					search(abonentsGrid);
				}
			}
		});

		addPersonBtn.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				DlgAddEditAbonent dlgAddEditAbonent = new DlgAddEditAbonent(
						null, abonentsDS);				
				dlgAddEditAbonent.show();
			}
		});

		editPersonBtn.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				ListGridRecord listGridRecord = abonentsGrid
						.getSelectedRecord();
				if (listGridRecord == null) {
					SC.say("მონიშნეთ ჩანაწერი ცხრილში");
					return;
				}
				
				SC.say(listGridRecord.getAttribute("phones"));				
			}
		});

		setPane(mainLayout);
	}

	private void search(final ListGrid abonentsGrid) {

		Criteria criteria = abonentsGrid.getCriteria();
		if (criteria == null) {
			criteria = new Criteria();
		}
		Criteria formCriteria = searchForm.getValuesAsCriteria();
		criteria.addCriteria(formCriteria);

		abonentsGrid.invalidateCache();
		abonentsGrid.filterData(criteria);
	}
}
