Ext.BLANK_IMAGE_URL = "/medias/img/pix.gif";

var articles = function() {
    return {
        init: function() {

            Ext.QuickTips.init();

            var store = new Ext.data.JsonStore ({
                /*url: '/main/recherche_article_resultats.process.php?strQueryLucene=' + (typeof(strQueryLucene) != "undefined" ? strQueryLucene : ""),*/
		  url: '/main/vente_agence_resultats.process.php?strQueryLucene=',
                totalProperty: 'totals',
                root: 'articles',
                fields:['line','code_modele', 'model', 'year', 'km','immat', 'color', 'center', {name:'price',name:'price',type: 'int'}],
                remoteSort: true
            });
            store.load({
            	callback: function (record) {
            		if(store.getCount()==0){
            			//window.location.href="core.php?pag_id=466";
            		}
            	}
            });
            	
                function formatPrice (value, p, record) {
                    return value + " &euro;";
                }
                
                function formatKilo (value, p, record) {
                    return value + " km";
                }
                
		var textField = new Ext.form.TextField({
 			id: 'textFieldID',
  			name: 'name',
  			width: 100,
  			allowBlank: false
		});

		
		var columns = [ 
                    	{ header: 'Modèle', sortable: false, dataIndex: 'model', width: 200, resizable: false, css: 'white-space:normal;' },
                    	{ header: 'Année', sortable: false, dataIndex: 'year', width: 90, resizable: false },
                    	{ header: 'Kilométrage', sortable: false, dataIndex: 'km', width: 90, resizable: false, renderer: formatKilo },
			{ header: 'Immatriculation', sortable: false, dataIndex: 'immat', width: 90, resizable: false },
                    	{ header: 'Couleur', sortable: false, dataIndex: 'color', width: 90, resizable: false },
                    	{ header: 'Agence', sortable: false, dataIndex: 'center', width: 120, resizable: false },
                    	{ header: 'Prix', sortable: false, dataIndex: 'price', width: 86, resizable: false, renderer: formatPrice }
                ]

            var articleGrid = new Ext.grid.GridPanel({
                store: store,
                el: 'resultPanel',
                width: 890,
                height: 470,
                loadMask: true,
                stripeRows:false,
                frame: true,
                columns:columns,
		  sm: new Ext.grid.RowSelectionModel(),
                bbar: new Ext.PagingToolbar({
                        pageSize: 20,
                        store: store,
                        emptyMsg: "Aucun article"
                    })
            });
            articleGrid.on('rowclick', viewArticle );

            function viewArticle( grid, row, event )
            {
		  
				var store = grid.getStore();
				var record = store.getAt( row );

				if (record.get('center') != "")
					new Ext.Window({
						title    : 'Hertz Occasion',
						closable : true,
						width    : 550,
						height   : 450,
						border : false,
						plain    : true,
						layout   : 'fit',
						html: '<iframe src="../main/fiche_detail.php?line=' + record.get('line') + '" style="overflow: hidden;width: 550px; height: 450px; border: 0;padding:0;margin:0;"/>'
					}).show();
   
            }
	
	     function getArticle(id){
	    		Ext.Ajax.request({
			url: '../main/fiche_detail.php?line='+ id,
			method: 'GET',
			success: function(objServerResponse) {
				viewArticleDetail(objServerResponse.responseText);
			}
		});
            }

	     function viewArticleDetail(textHtml){
		var win;
		var win = new Ext.Window({
            		title    : 'Hertz Occasion',
            		closable : true,
            		width    : 550,
            		height   : 450,
            		border : false,
            		plain    : true,
            		layout   : 'fit',
			items:[{
				html: textHtml
				}]
        	});

        	win.show();
	     
	     }
            
            function renderTitle (value, p, record) {
                return String.format('<p style="width: 500px; font-weight: bold;cursor:hand">{0}</p><p style="cursor:hand">{1}</p>', value, record.data['description'] );
            }
            
            articleGrid.render();
           
            var view = articleGrid.getView();
            view.showPreview = true;
            view.refresh();
            
        }
    };
}();
Ext.EventManager.onDocumentReady( articles.init, articles, true );
