ip2.declare("Portfolio.widget.ProductLabel", {

	border: 0,

	bodyBorder: 0,

	bodyPadding: 15,

	form: null,

	dmdPath: "",

	itemToken: 0,

	autoScroll: true,

	onInitialize: function (i_cmp, i_owner) {

		var i_dmdForm = i_cmp.up("entityform");
		var o_dmd = ip2.resource.get("webshop.dmd");
		var s_path = i_cmp.dmdPath = o_dmd._path;
		var n_token = i_cmp.itemToken = i_dmdForm.getRecord().get("ITMITMTKN");

		Ext.Ajax.request({
			url: "ip2-services/ip2dmdProxy.aspx",
			params: {
				type: "transaction",
				payload: JSON.stringify({
					batch: [
						{
							type: "get",
							dmd: s_path,
							entity: "WSHPLBET00",
							uiName: "portfolio",
							context: "grid"
						},
						{
							type: "get",
							dmd: s_path,
							entity: "WSHIPLRL00",
							uiName: "portfolio",
							context: "grid",
							key: {
								IPLITMTKN: n_token
							}
						}
					]
				})
			},
			success: function (o_response) {

				var o_json = ip2.JSON.parse(o_response.responseText, true);

				var a_labels = o_json.batch[0].rows || [];
				var a_selected = o_json.batch[1].rows || [];

				a_labels.sort(function (a, b) {

					var textA = a.PLBTITLE || "";
					var textB = b.PLBTITLE || "";

					textA = textA.toUpperCase();
					textB = textB.toUpperCase();

					return (textA < textB) ? -1 : (textA > textB) ? 1 : 0;
				});

				var o_fieldsets = {};

				ip2.each(a_labels, function (o_label) {

					var b_checked = false;

					Ext.Array.some(a_selected, function (o_selected) {
						if (o_selected.IPLPLBTKN == o_label.PLBPLBTKN) {
							b_checked = true;
							return true;
						}
					});
					var o_group = o_fieldsets[o_label.PLBTYPE];
					var s_lang = ip2.session.getLanguage();
					var o_enum = ip2.resource.get("adm.ns");
					var i_enum = new ip2.DynamicNS(o_enum);
					var a_enumKeys = i_enum.getEnumLayoutKeys("productlabel", /-text$/);
					var s_enumKey = [o_enum.ns, "productlabel", o_label.PLBTYPE].join("~");
					var o_enumValue = i_enum.getEnumValue(s_enumKey);

					if (!o_group) {
						o_group = o_fieldsets[o_label.PLBTYPE] = {
							xtype: "fieldset",
							title: i_enum.getEnumValue(s_enumKey, a_enumKeys)[s_lang],
							padding: 10,
							collapsible: true,
							sequence: o_enumValue.sequence || -1,
							items: [
								{
									xtype: "checkboxgroup",
									columns: 5,
									vertical: true,
									isFormField: false,
									items: []
								}
							]
						};
					}

					o_group.items[0].items.push({
						xtype: "checkbox",
						name: o_label.PLBPLBTKN,
						boxLabel: ip2.i18n(o_label.PLBDESC),
						inputValue: n_token,
						uncheckedValue: n_token,
						checked: b_checked,
						productLabelRelation: true
					});
				});

				var a_items = [];

				ip2.each(o_fieldsets, function (o_fieldset) {
					a_items.push(o_fieldset);
				});

				a_items.sort(function(a, b) {
					return (a.sequence || -1) - (b.sequence || -1);
				});

				i_cmp.form = i_cmp.add({
					xtype: "form",
					border: 0,
					bodyBorder: 0,
					itemId: "productlabelForm",
					items: a_items
				});
			}
		});
	},

	getForm: function () {

		var i_cmp = this;
		var i_form = i_cmp.down("#productlabelForm");

		return i_form;
	},

	doSave: function (f_callback) {

		f_callback = f_callback || Ext.emptyFn;

		var i_cmp = this;
		var i_form = i_cmp.getForm();

		var o_create = {};
		var o_destroy = {};

		var i_dmdForm = i_cmp.up("entityform");
		var i_record = i_dmdForm.getRecord();

		if (i_form) {
			i_form.getForm().getFields().each(function (i_field) {
				if (i_field.isDirty() && i_field.productLabelRelation) {

					var value = i_field.getSubmitValue();
					var b_checked = i_field.checked;

					if (b_checked) {
						o_create[i_field.name] = value;
					}
					else {
						o_destroy[i_field.name] = value;
					}
				}
			});
		}

		var a_batch = [];

		// Create the upsert batch
		ip2.each(o_create, function (n_itemToken, s_labelToken) {

			var n_labelToken = Number(s_labelToken);

			a_batch.push({
				type: "upsert",
				entity: "WSHIPLRL00",
				dmd: i_cmp.dmdPath,
				row: {
					IPLIPLTKN: 0,
					IPLITMTKN: i_record.get(i_record.idProperty),
					IPLPLBTKN: n_labelToken
				}
			});
		});

		// Create the destroy batch
		ip2.each(o_destroy, function (n_itemToken, s_labelToken) {

			var n_labelToken = Number(s_labelToken);

			a_batch.push({
				type: "delete",
				entity: "WSHIPLRL00",
				dmd: i_cmp.dmdPath,
				key: {
					IPLITMTKN: i_record.get(i_record.idProperty),
					IPLPLBTKN: n_labelToken
				}
			});
		});

		Ext.Ajax.request({
			url: "ip2-services/ip2dmdProxy.aspx",
			params: {
				type: "transaction",
				payload: JSON.stringify({
					batch: a_batch
				})
			},
			success: function (o_response) {

				var i_form = i_cmp.getForm();

				if (i_form) {
					i_form.getForm().getFields().each(function (i_field) {
						i_field.resetOriginalValue && i_field.resetOriginalValue();
					});
				}

				f_callback();
			}
		});
	},

	isDirty: function () {
		if (this.form) {
			return this.form.getForm().isDirty();
		}
		else {
			return false;
		}
	}
});
