/*
 * Registos Online v2.0
 * Copyright(c) 2008, Rui Lourenço, INPI
 * rui.lourenco@inpi.pt
 * 
 * http://www.inpi.pt
 */

Inpi.Pesquisas.MarcasDominiosPT = Ext.extend(Ext.form.FormPanel, {

    frame: false,
	border: false,
	autoWidth: true,
	autoHeight: true,
    bodyStyle: 'padding-top: 20px; padding-left: 15px; padding-right: 15px; padding-bottom: 10px;',
    
    constructor: function(config){
		
		var self = this;

        this.nome = new Inpi.Pesquisas.TextField({
            maxLength: 130,
            allowBlank: false,
            fieldLabel: Inpi.lblMarcaDominioPTReq,
            width: 280,
            name: 'nome_marca'
        });

		this.pesquisar = new Ext.Button({
                text: Inpi.lblPesquisar,
                iconCls: 'registos-online-button-search-icn',
                handler: this.doPesquisar,
                scope: this
        });
		
		this.store = new Ext.ux.data.PagingStore({
		    proxy: new Ext.data.HttpProxy({
		        url: '/pesquisas/MarcasNome',
		        method: 'POST'
		    }),
			lastOptions: {params:{start: 0,limit: 10}},

			reader: new Ext.data.XmlReader({
			   //totalRecords: 'results',
			   record: 'PROCESSO',
			   //id: 'NUMERO'
	            fields: [{
	                name: 'SITUACAO'
	            }, {
	                name: 'NUMERO', type: 'float'
	            }, {
	                name: 'SINAL'
	            }, {
	                name: 'DATA_PEDIDO', type: 'date', dateFormat: 'd-m-Y'
	            }, {
	                name: 'URL'
	            },{
	            		name: 'NORD'
	            }]

			})
		});	

		this.store2 = new Ext.ux.data.PagingStore({
		    proxy: new Ext.data.HttpProxy({
		        //url: '/pesquisas/DominiosNome',
				url: 'http://servicosonline.inpi.pt/pesquisas/DominiosNome',
		        method: 'POST'
		    }),
			lastOptions: {params:{start: 0,limit: 10}},

			reader: new Ext.data.XmlReader({
			   //totalRecords: 'results',
			   record: 'PROCESSO',
			   //id: 'NUMERO'
	            fields: [{
	                name: 'NOME'
	            }, {
	                name: 'HIERARQUIA'
	            }, {
	                name: 'SITUACAO'
	            }, {
	                name: 'DATA', type: 'date', dateFormat: 'd-m-Y'
	            }, {
	                name: 'URL'
	            }]

			})
		});	
		
		this.pagbar = new Ext.PagingToolbar({
		    pageSize: 10,
		    store: this.store,
		    displayInfo: true,
		    displayMsg: Inpi.lblResultados,
		    emptyMsg: Inpi.lblNenhumProcesso
		});		
        
        this.gridSm = new Ext.grid.RowSelectionModel({
            singleSelect: 'true'
        });
        
        this.gridId = Ext.id();
        this.gridCf = {
            xtype: 'grid',
			title: Inpi.lblMarcas,
            id: this.gridId,		
            store: this.store,
			bbar: this.pagbar,
			autoWidth: true,
			viewConfig: {
				autoFill: true,
				forceFit: true
			},
			width: 730,
            height: 308,
            frame: 'true',
            enableColumnMove: true,
            enableColumnResize: true,
			
            loadMask: {
                msg: Inpi.lblCarregar,
                msgCls: 'x-mask-loading'
            },
            
            columns: [{
                id: 'numero_marca',
                align: 'left',
                header: Inpi.lblNProcesso,
                width: 60,
                dataIndex: 'NUMERO',
				sortable: true
                //
				//renderer: function change(value, metada, record, rowIndex, colIndex, store){
                //    return '<div text-align="left"><a target=_blank href="'+record.get('URL')+'">'+value+'</a></div>';
                //}
								
            }, {
                id: 'sinal_marca',
                align: 'left',
                header: Inpi.lblNomeMarca,
                width: 160,
                dataIndex: 'SINAL',
				sortable: true,
                renderer: function change(value, metada, record, rowIndex, colIndex, store){
                //return '<div text-align="left"><a target=_blank href="'+record.get('URL')+'">'+value+'</a></div>';
                	return '<div text-align="left"><a target=_blank href="../main/marcasdirecto.jsp?nord='+record.get('NORD')+'">'+value+'</a></div>';
                	
                }				
            }, {
                id: 'situacao_marca',
                align: 'left',
                header: Inpi.lblSituacao,
                width: 120,
                dataIndex: 'SITUACAO',
				sortable: true
            }, {
                id: 'data_pedido_marca',
                align: 'left',
                header: Inpi.lblDataPedido,
                width: 60,
                dataIndex: 'DATA_PEDIDO',
				sortable: true,
				renderer: Ext.util.Format.dateRenderer('d-m-Y')	
            }],
            autoExpandColumn: 'sinal_marca',
            sm: this.gridSm,
            stripeRows: true,
			listeners: {
				afterrender: {
					scope: this,
					fn: function(component){
						component.getBottomToolbar().refresh.hideParent = true;
						component.getBottomToolbar().refresh.hide();
					}
				}
			}			
        };

		this.pagbar2 = new Ext.PagingToolbar({
		    pageSize: 10,
		    store: this.store2,
		    displayInfo: true,
		    displayMsg: Inpi.lblResultados,
		    emptyMsg: Inpi.lblNenhumProcesso
		});		
        
        this.gridSm2 = new Ext.grid.RowSelectionModel({
            singleSelect: 'true'
        });
        
        this.gridId2 = Ext.id();
        this.gridCf2 = {
            xtype: 'grid',
			title: Inpi.lblNomeDominioPT,
            id: this.gridId2,		
            store: this.store2,
			bbar: this.pagbar2,
			autoWidth: true,
			viewConfig: {
				autoFill: true,
				forceFit: true
			},
			width: 730,
            height: 308,
            frame: 'true',
            enableColumnMove: true,
            enableColumnResize: true,
            loadMask: {
                msg: Inpi.lblCarregar,
                msgCls: 'x-mask-loading'
            },
            columns: [ {
                id: 'hierarquia_nome',
                align: 'left',
                header: Inpi.lblHierarquia,
                width: 60,
                dataIndex: 'HIERARQUIA',
				sortable: true
            }, {
                id: 'nome_dominio',
                align: 'left',
                header: Inpi.lblNomeDominio	,
                width: 160,
                dataIndex: 'NOME',
				sortable: true,
                renderer: function change(value, metada, record, rowIndex, colIndex, store){
                    return '<div text-align="left"><a target=_blank href="'+record.get('URL')+'">'+value+'</a></div>';
                }
								
            },{
                id: 'situacao_nome',
                align: 'left',
                header: Inpi.lblSituacao,
                width: 120,
                dataIndex: 'SITUACAO',
				sortable: true
            }, {
                id: 'data_pedido_nome',
                align: 'left',
                header: Inpi.lblDataPedido,
                width: 60,
                dataIndex: 'DATA',
				sortable: true,
				renderer: Ext.util.Format.dateRenderer('d-m-Y')	
            }],
            autoExpandColumn: 'nome_dominio',
            sm: this.gridSm2,
            stripeRows: true,
			listeners: {
				afterrender: {
					scope: this,
					fn: function(component){
						component.getBottomToolbar().refresh.hideParent = true;
						component.getBottomToolbar().refresh.hide();
					}
				}
			}			
        };
		
        config = Ext.apply({
			labelWidth: 200,
            url: Inpi.Pesquisas.Config.serverController,
            //defaultType: 'textfield',
            monitorValid: true,
            items: [
				{
					xtype: 'panel',
					layout: 'column',
					border: false,
					items: [
						{
							xtype: 'fieldset',
							border: false,
							items: this.nome,
							bodyStyle: 'padding-right: 20px;'						
						}
					,this.pesquisar]
				},			
				{
					xtype: 'panel',
					border: false,
					frame: false,
					height: 5
				}, this.gridCf, {
					xtype: 'panel',
					border: false,
					frame: false,
					height: 10
				},
				this.gridCf2],
            buttonAlign: 'left'
        }, config);
        Inpi.Pesquisas.MarcasDominiosPT.superclass.constructor.call(this, config);
		
    },
    
    doPesquisar: function(){
        if (this.validateForm()) {
			
			delete this.store.lastParams;
			this.store.baseParams = {
				nome: this.nome.getValue()
			};		

			delete this.store2.lastParams;
			this.store2.baseParams = {
				nome: this.nome.getValue()
				//nome: 'anacom'
			};		

			//this.getEl().mask(Inpi.lblCarregar, 'x-mask-loading');			
			this.store.load({
				params: {
					start: 0,
					limit: 10
				},
				callback: this.storeclb,
				scope: this				
			});
		
			this.store2.load({
				params: {
					start: 0,
					limit: 10
				},
				callback: this.store2clb,
				scope: this				
			});
						
        }
    },
	
	storeclb: function(r, option, success){
		if (success) {
			// all ok			
		} else {
			Ext.getCmp(this.gridId).getEl().unmask();
			this.store.removeAll();
			Ext.Msg.alert(Inpi.lblAtencao, Inpi.lblPesquisaMarcasFail);
		}
		//this.getEl().unmask();		
	},

	store2clb: function(r, option, success){
		if (success) {
			// all ok			
		} else {
			Ext.getCmp(this.gridId2).getEl().unmask();
			this.store2.removeAll();
			Ext.Msg.alert(Inpi.lblAtencao, Inpi.lblPesquisaDominiosFail);
		}
		//this.getEl().unmask();		
	},
	   
    validateForm: function(){

        if (!this.getForm().isValid()) {
            Ext.Msg.alert(Inpi.lblAtencao, Inpi.lblAviso1);
            return false;
        }
		
        return true;
    }
    
});
Ext.reg('marcas-dominiospt', Inpi.Pesquisas.MarcasDominiosPT);

var Main = new Inpi.Pesquisas.MarcasDominiosPT({id: 'marcas-dominios', height: 664});

// application main entry point
Ext.onReady(function() {
 
    Ext.QuickTips.init();
	//Ext.state.Manager.setProvider(new Ext.state.CookieProvider());

	// Apply a set of config properties to the singleton
	Ext.apply(Ext.QuickTips.getQuickTip(), {
	    maxWidth: 200,
	    minWidth: 100,
	    showDelay: 50,
		dismissDelay: 15000,
		hideDelay: 1,
	    trackMouse: true
	});
	
    // turn on validation errors beside the field globally
    Ext.form.Field.prototype.msgTarget = 'side';

	limpaContexto();
	Main.render('ropanel');
		
}); 
// eo function onReady
		
