Ext.define('TradePortal.view.products.Grid', {
	extend: 'Ext.grid.Panel',
	alias: 'widget.products_grid',
	id: 'products_grid',
	width: 720,
	height: window.innerHeight / 2 - 5,
	border: false,
	selType: 'cellmodel',
	requires: [
		'Ext.grid.plugin.CellEditing'
	],
	plugins: [{
		ptype: 'cellediting',
		clicksToEdit: 1
	}],
	store: 'Products',
	columns: {
		items: [
			{
				header: 'Product',
				dataIndex: 'productID',
				flex: 4
			},
			{
				header: 'Description',
				dataIndex: 'productLongDesc',
				flex: 8,
				renderer: function renderTip(val, meta, rec, rowIndex, colIndex, store) {
					meta.tdAttr = 'data-qtip="' + val + '"';
					return val;
				}
			},
			{
				header: 'Retail Price<br/>Inc GST',
				dataIndex: 'RRP',
				align: 'right',
				renderer: Ext.util.Format.usMoney,
				flex: 4
			},
			{
				header: 'Trade Price<br/>Inc GST',
				dataIndex: 'TradePrice',
				align: 'right',
				renderer: Ext.util.Format.usMoney,
				flex: 4
			},
			{
				header: 'Customer Price<br/>Inc GST',
				dataIndex: 'BestPrice',
				align: 'right',
				renderer: Ext.util.Format.usMoney,
				flex: 5
			},
			{
				header: 'Qty',
				dataIndex: 'prodQty',
				flex: 2,
				editor: {
					xtype: 'numberfield',
					value: 1,
					minValue: 1
				}
			},
			{
				header: 'UOM',
				dataIndex: 'UOMList',
				flex: 3,
				editor: {
					xtype: 'combobox',
					store: 'UOMList',
					displayField: 'UOM',
					valueField: 'UOM',
					editable: false,
					queryMode: 'local',
					triggerAction: 'all',
					listeners: {
						focus: function (thisCmb, newValue, oldValue, scope) {
							var index = Ext.getCmp('products_grid').getSelectionModel().lastSelected.index;
							Ext.getStore('UOMList').removeAll();
							Ext.getStore('UOMList').loadRawData(UOMStore[index]);
						}
					}
				},
				renderer: function (v, meta, r) {

					if (r.data.UOM != '') {
						var v = {UOM: r.data.UOM};
					}
					UOMStore.push(v);
					if (r.data.UOM != '') {
						var v = r.data.UOM;
					}
					if (typeof v !== 'object') {
						return v;
					}
					else {
						if (v[0] == undefined) {
							return v['UOM'];
						}
						else {
							return v[0]['UOM'];
						}
					}

				}
			}, {
				header: '+',
				xtype: 'actioncolumn',
				items: [{
					icon: 'resources/css/images/add.png',
					tooltip: 'Add',
					handler: function (grid, rowIndex, colIndex) {
						var rec = grid.getStore().getAt(rowIndex);
						if (rec.data.isTally || rec.data.Tally) {
							Ext.getCmp('viewport').setLoading('Loading...');
							Ext.getStore('TallyProducts').setExtraParam('lineNo', 0);
							Ext.getStore('getProductDetails').load({
								params: {
									deviceID: Ext.getStore('Config').data.last().data.device_id,
									ProductID: rec.get('productID'),
									customerID: Ext.getStore('HomeStatistic').last().data.id_cust,
									incGST: Ext.getStore('getPriceViewOptionsTP').last().data.GSTInc
								}
							});

							Ext.getStore('ProductLengthTally').load({
								params: {
									companyID: Ext.getStore('Config').data.last().data.company_id,
									branchID: 1,
									productID: rec.get('productID')
								}
							});
						}
						else {
							Ext.getStore('CartTotal').removeAll();
							if (rec.get('UOM') != '') {
								var v = rec.get('UOM');
							}
							else {
								var v = rec.get('UOMList');
								if (v[0]['UOM'] != undefined) {
									v = v[0]['UOM'];
								}
							}
							Ext.getStore('addCartProduct').load({
								params: {
									sessionID: Ext.getStore('createCart').last().data.sessionID,
									roomTypeID: '',
									customerID: Ext.getStore('HomeStatistic').last().data.id_cust,
									productID: rec.get('productID'),
									quoteQty: rec.get('prodQty'),
									unitSell: 0,
									quoteComments: '',
									UOM: v
								}
							});
						}
					}
				}],
				flex: 1
			}
		],
		defaults: {
			draggable: false,
			hideable: false,
			sortable: false,
			resizable: false
		}
	}
});
