/**
 * Registos Online v2.0
 * Copyright(c) 2008, Rui Lourenço, INPI
 * rui.lourenco@inpi.pt
 *
 * http://www.inpi.pt
 */
Inpi.Pesquisas.MarcasSemelhanca = Ext.extend(Ext.form.FormPanel, {

    frame: true,
    title: Inpi.lblSemeTitle1,
    width: 894,
    bodyStyle: 'padding-top: 5px; padding-left: 10px; padding-right: 10px;',
    
    constructor: function(config){
		
		var self = this;

        this.nome = new Inpi.Pesquisas.TextField({
            maxLength: 130,
            allowBlank: false,
            fieldLabel: Inpi.lblNomeMarcaReq,
            width: 280,
            name: 'nome_marca'
        });

		this.lovNiceStore = new Ext.data.JsonStore({
		    url: '/pesquisas/LovNice',
		    method: 'POST',
			baseParams: {lang: language},		
		    root: 'items',
		    totalProperty: 'totalCount',
		    fields: ['id', 'desi']
		});

		this.lovNiceGridSm = new Ext.grid.CheckboxSelectionModel();

		this.lovNiceGrid = new Ext.grid.GridPanel({
			id: 'lov-nice-grid-panel-marcas-semelhanca',
		    store: this.lovNiceStore,
		    columns: [this.lovNiceGridSm, {
		        id: 'id',
		        header: Inpi.lblClasse,
		        width: 50,
		        sortable: true,
		        dataIndex: 'id'
		    }, {
		        header: Inpi.lblDesignacao,
		        width: 470,
		        sortable: true,
		        dataIndex: 'desi',
		        renderer: function(v, p){
		            //v : value , p : cell 
		            //p.attr =  'ext:qtitle="' + "Classe " + v + '"'; 
		            p.attr += ' ext:qtip="' + v + '"';
		            return v;
		        }
		    }],
		    sm: this.lovNiceGridSm,
		    stripeRows: true
		});

		this.lovNiceConf = {
		    xtype: 'xlovfield',
		    id: 'lovNiceGrid-marcas-semelhanca',
		    width: 280,
		    fieldLabel: Inpi.lblClassProdServ,
		    allowBlank: true,
		    multiSelect: true,
		    lovTitle: Inpi.lblClassProdServ,
		    lovHeight: 400,
		    lovWidth: 575,
		    displayField: 'id',
		    displaySeparator: ' ',
			valueSeparator: ' ',
		    textarea: false,
		    showOnFocus: false,
		    view: this.lovNiceGrid
		};
		
		this.nice = new Ext.ux.form.LovField(this.lovNiceConf);
		
		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: Inpi.Pesquisas.Config.serverController,
		        method: 'POST'
		    }),
			lastOptions: {params:{start: 0,limit: 19}},			
		    reader: new Ext.data.JsonReader({
				successProperty: 'success',
	            root: 'items',
	            totalProperty: 'total',				
	            fields: [{
	                name: 'moda'
	            }, {
	                name: 'nume_proc', type: 'float'
	            }, {
	                name: 'sinal'
	            }, {
	                name: 'grau_sem', type: 'float'
	            }, {
	                name: 'tipo_sinal'
	            }, {
	                name: 'fase'
	            }, {
	                name: 'nord'
	            }, {
	                name: 'proc_moda'
	            }]
		    })			
		});
		
		
		/*
        this.store = new Ext.data.Store({			
		    proxy: new Ext.ux.data.PagingMemoryHttpProxy({
		        url: Inpi.Pesquisas.Config.serverController,
		        method: 'POST'
		    }),
			remoteSort: true,
		    reader: new Ext.data.JsonReader({
	            root: 'items',
	            totalProperty: 'total',
	            fields: [{
	                name: 'moda'
	            }, {
	                name: 'nume_proc'
	            }, {
	                name: 'sinal'
	            }, {
	                name: 'data_pedido'
	            }, {
	                name: 'clas_nice'
	            }, {
	                name: 'nord'
	            }, {
	                name: 'proc_moda'
	            }, {
					name: 'tipo_sinal'
				}]
		    })			
        });
        */
		
		this.pagbar = new Ext.PagingToolbar({
		    pageSize: 19,
		    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',
            id: this.gridId,		
            store: this.store,
			bbar: this.pagbar, 
            width: 860,
            height: 485,
            frame: 'true',
            enableColumnMove: true,
            enableColumnResize: true,
			/*
            loadMask: {
                msg: Inpi.lblCarregar,
                msgCls: 'x-mask-loading'
            },
            */
            columns: [{
                id: 'moda',
                align: 'left',
                header: Inpi.lblModalidade,
                width: 160,
                dataIndex: 'moda',
				sortable: true
            }, {
                id: 'nume_proc',
                align: 'left',
                header: Inpi.lblNProcesso,
                width: 100,
                dataIndex: 'nume_proc',
				sortable: true,
                renderer: function change(value, metada, record, rowIndex, colIndex, store){
                    if(record.get('proc_moda') != '18')
						return 	'<div text-align="left"><a target=_self href="javascript:Ext.getCmp(\''+self.getId()+'\').viewDeail('+record.get('nord')+')">'+value+'</a></div>';
                    else return '<div text-align="left"><a href="http://oami.europa.eu/CTMOnline/RequestManager/en_Result?transition=ResultsDetailed&ntmark=&application=CTMOnline&bAdvanced=0&language=en&deno=&source=search_basic.jsp&idappli='+value+'" target="_blank">'+value+'</a></div>';
                }				
            }, {
                id: 'grau_sem',
                align: 'left',
                header: Inpi.lblSemePercentagem,
                width: 100,
                dataIndex: 'grau_sem',
				sortable: true
            }, {
                id: 'sinal',
                align: 'left',
                header: Inpi.lblNomeMarca,
                width: 180,
                dataIndex: 'sinal',
				sortable: true
            }, {
                id: 'tipo_sinal',
                align: 'left',
                header: Inpi.lblTipoSinal,
                width: 90,
                dataIndex: 'tipo_sinal',
				sortable: true
            }, {
                id: 'fase',
                align: 'left',
                header: Inpi.lblFaseActual,
                width: 210,
                dataIndex: 'fase',
				sortable: true
            }],
            autoExpandColumn: 'sinal',
            sm: this.gridSm,
            stripeRows: true
        };
		
        config = Ext.apply({
			labelWidth: 200,
            url: Inpi.Pesquisas.Config.serverController,
            //defaultType: 'textfield',
            monitorValid: true,
            items: [this.nome, this.nice, this.pesquisar, {
				xtype: 'panel',
				height: 10
			}, this.gridCf],
            buttonAlign: 'left',
            buttons: [{
                text: Inpi.lblAnterior,
                iconCls: 'registos-online-back-icn',
                handler: this.previousForm,
                scope: this
            }]
        }, config);
        Inpi.Pesquisas.MarcasSemelhanca.superclass.constructor.call(this, config);
		
    },
    
    doPesquisar: function(){
        if (this.validateForm()) {
			
			delete this.store.lastParams;
			//this.store.proxy.reset();
			this.store.baseParams = {
                cmd: 'PesquisaMarcasSemelhanca',
				lang: language,
				nome: this.nome.getValue(),
				nice: this.nice.getValue()
			};
			this.getEl().mask(Inpi.lblCarregar, 'x-mask-loading');
			
			this.store.load({
				params: {
					start: 0,
					limit: 19
				},
				callback: this.storeclb,
				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.lblPesquisaFail);
		}
		this.getEl().unmask();
		
	},
	
	viewDeail: function(nord){
		//Ext.Msg.alert('Detail', nord);
		Inpi.Pesquisas.Context.nord = nord;
		showEcran('marcas-detalhe', this.id);
	},
	
    showNext: function(){
		this.getForm().reset();
		this.store.removeAll();
		this.pagbar.doRefresh();
	},	
    
    resetForm: function(){
        this.getForm().reset();
    },
	
    previousForm: function(){
        showPreviousEcran(this.id);
    },	
    
    validateForm: function(){

        if (!this.getForm().isValid()) {
            Ext.Msg.alert(Inpi.lblAtencao, Inpi.lblAviso1);
            return false;
        }
		
        return true;
    }
    
});

Ext.reg('marcas-semelhanca', Inpi.Pesquisas.MarcasSemelhanca);

