Ext.define('Ext.ux.folderorganizer.FolderProductLocation', {
	extend: 'Ext.grid.Panel',
	alias: ['widget.folderorganizerFolderProductLocation'],
	border: false,
	autoScoll: true,
	viewConfig: {
		loadMask: true
	},
	hideHeaders: true,
	initComponent: function() {
		var me = this;

		me.radioButtonClicked = false;

		var modelName = 'folderProductLocationModel';

		if (!Ext.ClassManager.isCreated(modelName)) {
			Ext.define(modelName, {
				extend: 'Ext.data.Model',
				proxy: {
					type: 'ajax',
					url: me.wrapper.proxy,
					reader: {
						type: 'json',
						rootProperty: 'rows',
						totalProperty: 'totalRows'
					},
					extraParams: {
						action: 'getProductCatalogLocation'
					}
				}
			});
		}

		me.columns = [
		{ xtype: 'rownumberer' },
		{ header: ip2.i18n(me.getI18N('primary')), dataIndex: 'PRILOC', width: 24,
                renderer: function(value, meta, record, rowIndex, colIndex, store) {
	var html = me.createPrimaryLocationRadioButton(value, record, store.getCount());

	return html;
                }
            },
            { header: ip2.i18n(me.getI18N('breadcrumb')), dataIndex: 'TITLE', flex: 1 }
        ];

		me.store = Ext.create('Ext.data.Store', {
			model: modelName,
			autoLoad: true,
			remoteSort: false
		});

		me.listeners = me.listeners || {};
		Ext.apply(me.listeners, {
			itemclick: function(view, record, item, index, e) {
				if (!me.radioButtonClicked) {
					var a_split = record.data.BREADCRUMB.split(',');
					if (!a_split[0] == "") {
						var i_selected = me.wrapper.treegrid.getSelectionModel().getSelection()[0];
						me.wrapper.treegrid.expandTreeToNode(a_split, i_selected, 'PROPERTIES', 'itemnumber');
					}
				}
				me.radioButtonClicked = false;
			}
		});

		me.callParent();
	},

	/* ----------------------------------------------------------------------------------------------------
	    Create primary location radio button
	   ---------------------------------------------------------------------------------------------------- */
	createPrimaryLocationRadioButton: function(value, record, count) {
		var me = this;

		if (count < 2) {
			return '';
		}

		var id = [
		'primaryLocation',
		record.internalId,
		ip2.unique()
		].join('-');
		var checked = value ? 'CHECKED' : '';
		var html = new Ext.XTemplate('',
		'<table ',
		'class="x-ux-gallery-cell" ',
		'style="text-align: center; ',
		'width: 100%;"',
		'>',
		'<tr>',
		'<td valign="middle">',
		'<input ',
		'id="' + id + '" ',
		'type="radio" ',
		'class="primaryLocation" ',
		'name="primaryLocation" ',
		'value="' + record.data.FLDFLDTKN + '" ',
		checked,
		'/>',
		'</td>',
		'</tr>',
		'</table>',
		'').html;

		setTimeout(function () {
			var el = Ext.get(id);

			if (el) {
				el.on('change', function (e, rb) {
					me.radioButtonClicked = true;
					Ext.Ajax.request({
						url: me.wrapper.proxy,
						params: {
							action: 'setProductCatalogLocation',
							folderToken: rb.value
						}
					});
				});
			}
		}, 4);

		return html;
	},

	/* ----------------------------------------------------------------------------------------------------
	    Get i18n text by language
	   ---------------------------------------------------------------------------------------------------- */
	getI18N: function(key) {
		return ip2.folderorganizer.getTextValue(key);
	}

});
