/**
 * Registos Online v2.0
 * Copyright(c) 2008, Rui Lourenço, INPI
 * rui.lourenco@inpi.pt
 *
 * http://www.inpi.pt
 */
Inpi.Pesquisas.DesignEpigrafe = Ext.extend(Ext.form.FormPanel, {

    frame: true,
    title: Inpi.lblEpigrafeTitulo,
    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.lblTituloReq,
            width: 280,
            name: 'titulo_patente'
        });

		this.locarno = new Inpi.Pesquisas.TextField({
            maxLength: 130,
            allowBlank: true,
            fieldLabel: Inpi.lblLinkLocarno,
            width: 280,
            name: 'locarno'
        });
		
		
		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: 'titulo'
	            }, {
	                name: 'data_pedido'
	            }, {
	                name: 'locarno'
	            }, {
	                name: 'nord'
	            }]
		    })			
		});
		

		
		this.pagbar = new Ext.PagingToolbar({
		    pageSize: 18,
		    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: 470,
            frame: 'true',
            enableColumnMove: true,
            enableColumnResize: true,
			/*
            loadMask: {
                msg: Inpi.lblCarregar,
                msgCls: 'x-mask-loading'
            },
            */
            columns: [{
                id: 'moda',
                align: 'left',
                header: Inpi.lblModalidade,
                width: 180,
                dataIndex: 'moda',
				sortable: true
            }, {
                id: 'nume_proc',
                align: 'center',
                header: Inpi.lblNProcesso,
                width: 90,
                dataIndex: 'nume_proc',
				sortable: true,
                renderer: function change(value, metada, record, rowIndex, colIndex, store){
                    return '<div text-align="left"><a target=_self href="javascript:Ext.getCmp(\''+self.getId()+'\').viewDetail('+record.get('nord')+')">'+value+'</a></div>';
                }				
            }, {
                id: 'titulo',
                align: 'left',
                header: Inpi.lblTitulo,
                width: 170,
                dataIndex: 'titulo',
				sortable: true
            }, {
                id: 'data_pedido',
                align: 'center',
                header: Inpi.lblDataPedido,
                width: 100,
                dataIndex: 'data_pedido',
				sortable: true
            }, {
                id: 'locarno',
                align: 'center',
                header: Inpi.lblClassLocarno,
                width: 150,
                dataIndex: 'locarno',
				sortable: true
            }],
            autoExpandColumn: 'titulo',
            sm: this.gridSm,
            stripeRows: true
        };
		
        config = Ext.apply({
			labelWidth: 180,
            url: Inpi.Pesquisas.Config.serverController,
            //defaultType: 'textfield',
            monitorValid: true,
        	items: [this.nome,{				
                xtype: 'label',
                text: Inpi.exemploTituloDesign,
				style: {
		            width: '95%',
		            marginLeft: '185px',
					marginbottom: '20px'
		        }

            }, this.locarno, {				
                xtype: 'label',
                text: Inpi.exemploLocarno,
				style: {
		            width: '95%',
		            marginLeft: '185px'
		        }

            }, 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.DesignEpigrafe.superclass.constructor.call(this, config);
		
    },
    
    doPesquisar: function(){
        if (this.validateForm()) {
			
			delete this.store.lastParams;
			//this.store.proxy.reset();
			this.store.baseParams = {
                cmd: 'PesquisaDesignEpigrafe',
				lang: language,
				nome: this.nome.getValue(),
				locarno: this.locarno.getValue()
			};
			this.getEl().mask(Inpi.lblCarregar, 'x-mask-loading');
			
			this.store.load({
				params: {
					start: 0,
					limit: 18
				},
				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();
		
	},
	
	viewDetail: function(nord){
		//Ext.Msg.alert('Detail', nord);
		Inpi.Pesquisas.Context.nord = nord;
		showEcran('design-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('design-epigrafe', Inpi.Pesquisas.DesignEpigrafe);

