/**
 * Registos Online v2.0
 * Copyright(c) 2008, Rui Lourenço, INPI
 * rui.lourenco@inpi.pt
 *
 * http://www.inpi.pt
 */
Inpi.Pesquisas.MarcasIdentidade = Ext.extend(Ext.form.FormPanel, {

    frame: true,
    title: Inpi.lblIdentidadeTitle1,
    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.nice = new Ext.ux.form.LovField(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_ide', type: 'float'
	            }, {
	                name: 'tipo_sinal'
	            }, {
	                name: 'fase'
	            }, {
	                name: 'nord'
	            }, {
	                name: 'proc_moda'
	            }]
		    })			
		});
		
		
		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_ide',
                align: 'left',
                header: Inpi.lblPercIdentidade,
                width: 100,
                dataIndex: 'grau_ide',
				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.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.MarcasIdentidade.superclass.constructor.call(this, config);
		
    },
    
    doPesquisar: function(){
        if (this.validateForm()) {
			
			delete this.store.lastParams;
			//this.store.proxy.reset();
			this.store.baseParams = {
                cmd: 'PesquisaMarcasIdentidade',
				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-identidade', Inpi.Pesquisas.MarcasIdentidade);

