Ext.define("Ext.ux.itemreference.Form", {

	extend: "Ext.panel.Panel",

	alias: [
		"widget.ip2ItemReferenceForm",
		"widget.ip2itemReferenceForm",
		"widget.ip2itemreferenceform"
	],

	requires: [
		"Ext.ux.itemreference.EntityProperties",
		"Ext.ux.itemreference.Button"
	],

	border: false,

	margin: 10,

	layout: "anchor",

	initComponent: function() {
		var me = this;

		me.items = [];

		me.callParent();

		// Create dummy names combo instance, and create data object using the combo store
		// The data object is used for all coming names combo instances as "local" store for better performance.
		me.names = Ext.create("Ext.ux.itemreference.EntityProperties", {
			dmd: "webshop.dmd",
			entity: "WSHNAMET00",
			serviceUrl: "/ip2-services/ip2dmdproxy.aspx",
			cid: "1",
			listProperties: [
			{ property: "name" }
			]
		});

		me.names.store.on("load", function() {
			var storeData = me.names.store.data.items;

			// build data object
			me.names.data = [];
			for (var i = 0; i < storeData.length; i++) {
				var data = [];
				for (var key in storeData[i].data) {
					data.push(storeData[i].data[key]);
				}
				me.names.data.push(data);
			}

			me.buildForm();
		});

		me.enableBubble("dirtychange");
	},

	/* ----------------------------------------------------------------------------------------------------
	    Build Form
	   ---------------------------------------------------------------------------------------------------- */
	buildForm: function() {
		var me = this;

		var a_items = [];

		var i_fieldcontainer = Ext.widget("fieldcontainer", {
			layout: {
				type: "hbox",
				align: "streach"
			},
			defaults: {
				margin: "0 5 0 0"
			}
		});

		var i_properties = Ext.widget("ip2entityPropertiesCombo", {
			entity: "WSHNAMET00",
			flex: 1,
			listProperties: [
				{
					property: "name"
				}
			],
			name: "IRFNAMTKN",
			itemId: "IRFNAMTKN",
			data: me.names.data,
			allowBlank: true,
			isFormField: false,
			hideLabel: true,
			emptyText: ip2.i18n(me.labels.name),
			listeners: {
				afterrender: function(field) {
					field.focus();
				}
			},
			tabIndex: 1
		});

		var i_button = Ext.widget("button", {
			iconCls: "pf-add",
			text: "Add",
			handler: function() {
				me.doAdd();
			},
			margin: "0 0 0 0"
		});

		var i_textfield = Ext.widget("textfield", {
			name: "IRFITMNO",
			itemId: "IRFITMNO",
			allowBlank: true,
			isFormField: false,
			hideLabel: true,
			emptyText: ip2.i18n(me.labels.alternativItemNumber),
			tabIndex: 2,
			enableKeyEvents: true,
			anchor: "100%",
			listeners: {
				keypress: function(field, e) {
					if (e.getCharCode() == Ext.event.Event.ENTER) {
						me.doAdd();
					}
				}
			}
		});

		i_fieldcontainer.add(i_properties);
		i_fieldcontainer.add(i_button);

		a_items.push(i_fieldcontainer);
		a_items.push(i_textfield);

		me.add(a_items);
	},

	/* ----------------------------------------------------------------------------------------------------
	    Add a row to the grid
	   ---------------------------------------------------------------------------------------------------- */
	doAdd: function() {
		var me = this;

		var data = {
			IRFIRFTKN: 0,
			IRFGRPTKN: 0
		};

		var IRFNAMTKN = me.down("#IRFNAMTKN");
		var IRFITMNO = me.down("#IRFITMNO");
		data.IRFNAMTKN = IRFNAMTKN.getValue();
		data.IRFITMNO = IRFITMNO.getValue();

		if (Ext.isEmpty(data.IRFNAMTKN) || Ext.isEmpty(data.IRFITMNO)) {
			if (Ext.isEmpty(data.IRFNAMTKN)) {
				IRFNAMTKN.markInvalid(IRFNAMTKN.blankText);
			}
			if (Ext.isEmpty(data.IRFITMNO)) {
				IRFITMNO.markInvalid(IRFITMNO.blankText);
			}
			return;
		}

		data.NAME = IRFNAMTKN.rawValue;

		Ext.Ajax.request({
			url: "/wsh-services/partsProxy.aspx",
			params: {
				action: "getWebshopItemnumber",
				manufacturer: data.IRFNAMTKN,
				manufactureritemnumber: data.IRFITMNO
			},
			data: data,
			success: function (response, request) {
				var result = {};
				eval("result =" + response.responseText);
				if (result.totalRows && result.totalRows > 0 && result.rows[0]) {
					request.data.WEBITEMNO = result.rows[0].ITEMNO;
				}

				var store = me.wrapper.grid.getStore();
				var model = store.proxy.getModel();

				var record = Ext.create(model, request.data);
				record.set("dirty", true);
				store.add(record);
			}
		});

		// clear form input fields
		IRFNAMTKN.reset();
		IRFITMNO.reset();
		IRFNAMTKN.focus();
	}
});
