Laverna.NaborWindow = Ext.extend(Ext.Window, {

	load : function(record) {
		this.record = record;
		this.store.reload({params: {articul: record.get('articul')}});
		this.show();
		Ext.getCmp('u3naborimage').setTitle(record.get('part_title'));
		Ext.getCmp('u3naborimage').body.update(
			'<img src="/images/u/' + record.get('img2file') + '" style="text-align: center; width: ' +  record.get('img2width') + 'px; height: ' + record.get('img2height') + 'px;">'
		);
		//Ext.getCmp('u3naborimage').setWidth(record.get('img2width') );
		//Ext.getCmp('u3naborgrid').setWidth( this.getWidth - record.get('img2width') - 50 );
		this.doLayout();
	},
	
	CaddToBasket : function(event) {
  		if (typeof event == 'undefined') {
    	event = window.event;
  		}
		if (event.keyCode == 13) {
			this.addToBasket();
		}
	},
	
	addToBasket : function () {
		this.hide();
		//Laverna.basket.show();
		var stamp = (new Date()).getTime();
		var a = new Array();
		this.store.each(function(r){
			a.unshift(r);
		});
		for ( var i = 0; i<a.length; i++) {
			Laverna.basket.addItem(a[i], document.getElementById('qtynabor').value, stamp);
		}
	
	},
	
	float2Renderer : function (data, metadata, record, rowIndex, columnIndex, store) {
		return (0 + data).toFixed(2);
	},
	
	initComponent:function() {
	
		this.dbRecord = Ext.data.Record.create([
			{name: 'articul', type: 'string'},
        	{name: 'part_articul', type: 'string'},
        	{name: 'code', type: 'string'},
            {name: 'tm', type: 'string'},
        	{name: 'collection', type: 'string'},
			{name: 'description', type: 'string'},
            {name: 'oldprice', type: 'float'},
            {name: 'price', type: 'float'}
        ]);

	this.store = new Ext.data.Store({
            proxy: new Ext.data.HttpProxy({
                //where to retrieve data
                url: '/ajax.pl', //url to data object (server side script)
                method: 'POST'
            }),
            reader: new Ext.data.JsonReader(
    			{ //creates array from JSON response
            		root: 'items', // name of the property that is container for an Array of row objects
            		id: 'articul' //the property within each row object that provides an ID for the record (optional)
        		},
        		this.dbRecord //instead of defining inline just pass a reference to the object 
    		),
            baseParams: { '_function' : 'TVKNabor' },
            sortInfo:{field: 'articul', direction: "ASC"},
            autoLoad: false
		});
		this.total = new Ext.Toolbar.TextItem({text: ' '});
		this.add2basket = new Ext.Toolbar.TextItem({
			text: '<input type="text" id="qtynabor" class="x-form-text x-form-field" autocomplete="off" onclick="fixFocus(this)" size="2" title="Добавить к заказу" onKeyPress="Laverna.nabor.CaddToBasket(event)"><button title="Добавить к заказу" class="x-btn-image" style="padding: 0px; vertical-align: middle;" type="button" onclick="Laverna.nabor.addToBasket()"><img src="/images/korzina.gif" style="vertical-align: middle"></button>'
		});
		
		this.store.on({
		    'load':{
        		fn: function(store, records, options){
        			allowPageUnload = false;
        			var t = 0.00;
					store.each(function(r) {t = t + parseFloat(r.get('price'))});
					this.total.getEl().innerHTML = t.toFixed(2);
        		},
        		scope:this
			},	
			'loadexception': {
      			fn: function (th, options,  response,  e ) {
      				allowPageUnload = false;
      				Ext.MessageBox.alert('Ошибка загрузки ТВК!', e);
      			},
      			scope: this 
      		}
      	});
      	
		Ext.apply( this, {
                    minHeight: 30,
                    minWidth: 300,
                    width: 790,
                    height: 500,
                    title:'Набор товаров',
                    layout: 'column',
                    modal: true,
					closeAction: 'hide',
                    closable:true,
					items : 
					[
						{
							xtype: 'panel',
							title: 'Image',
							id : 'u3naborimage',
							width: 200
						},
						{
						xtype: 'grid',
						store: this.store,
						id: 'u3naborgrid',
						autoExpandColumn: 'description',
						frame: false,
						columnWidth: .99,
						autoScroll: true,
						autoHeight: true,
						
            			columns: [
        					{header: "ТМ", width: 100, sortable: true, dataIndex: 'tm'},
        					{header: "Коллекция", width: 100, sortable: true, dataIndex: 'collection'},
        					{header: "Описание", id: 'description', width: 200, sortable: true, dataIndex: 'description', renderer: this.qtipRenderer},
        					{header: "Артикул", width: 80, sortable: true, dataIndex: 'articul'},
        					{header: "Код", width: 80, sortable: true, dataIndex: 'code'},
        					{header: "Старая Цена<br>у.е.", width: 65, sortable: true, dataIndex: 'oldprice', renderer: this.float2Renderer},
        					{header: "Новая Цена<br>у.е.", width: 65, sortable: true, dataIndex: 'price', renderer: this.float2Renderer}
    					],

            			selModel: new Ext.grid.RowSelectionModel({singleSelect:true}),//true to limit row selection to 1 row})

			            stripeRows: false,
			            viewConfig: {
        					//forceFit: true
        				},
        				bbar : [
        					'  ',
        					{xtype: 'tbtext', text: 'Добавить в корзину: '},
        					this.add2basket,
        					'->',
							{xtype: 'tbtext', text: 'Итого: '},
							this.total,
							{xtype: 'tbtext', text: '&nbsp;у.е.'}
						]
						}
					],
					buttons: [
					{
						text: 'Закрыть',
						handler: function(){ this.hide(); },
						scope: this
					}],
					keys: {
						key: 27, // Esc key
						handler: function(){ this.hide(); },
						scope: this
					}
		});
		this.loadingMask = new Ext.LoadMask(Ext.getBody(), {msg:"Пожалуйста подождите, идёт загрузка информации...", store: this.store});
		Laverna.NaborWindow.superclass.initComponent.call(this, arguments);
	}
	
});
	
